From 73b35ee49d52a883d5cf1269e72abd4f6a8c2732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=A4=9A=E7=9B=8A?= Date: Thu, 12 May 2022 10:25:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20replaceText=20?= =?UTF-8?q?=E4=B8=8D=E6=94=AF=E6=8C=81=20schemaApi=20=E9=97=AE=E9=A2=98=20?= =?UTF-8?q?(#4300)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock/cfc/mock/service/schema.js | 9 ++++++--- src/factory.tsx | 20 ++------------------ src/store/service.ts | 4 ++++ src/utils/replaceText.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 src/utils/replaceText.ts diff --git a/mock/cfc/mock/service/schema.js b/mock/cfc/mock/service/schema.js index c7b7ab5ae..8ce42e032 100755 --- a/mock/cfc/mock/service/schema.js +++ b/mock/cfc/mock/service/schema.js @@ -263,9 +263,8 @@ const predefined = { label: '记住登录' }, { - type: 'submit', - btnClassName: 'btn-default', - label: '登录' + type: 'static', + value: 'AMIS_HOST' } ] }, @@ -303,6 +302,10 @@ const predefined = { type: 'datetime', labelClassName: 'text-muted', name: 'date' + }, + { + type: 'static', + value: 'AMIS_HOST' } ] } diff --git a/src/factory.tsx b/src/factory.tsx index 110b8d713..3ce099e57 100644 --- a/src/factory.tsx +++ b/src/factory.tsx @@ -35,6 +35,7 @@ import {OnEventProps} from './utils/renderer-event'; import {enableDebug} from './utils/debug'; import type {ToastLevel, ToastConf} from './components/Toast'; +import {replaceText} from './utils/replaceText'; export interface TestFunc { ( @@ -447,24 +448,7 @@ export function render( props.useMobileUI = true; } - // 进行文本替换 - if (env.replaceText && isObject(env.replaceText)) { - const replaceKeys = Object.keys(env.replaceText); - replaceKeys.sort((a, b) => b.length - a.length); // 避免用户将短的放前面 - const replaceTextIgnoreKeys = new Set(env.replaceTextIgnoreKeys || []); - JSONTraverse(schema, (value: any, key: string, object: any) => { - if (typeof value === 'string' && !replaceTextIgnoreKeys.has(key)) { - for (const replaceKey of replaceKeys) { - if (~value.indexOf(replaceKey)) { - value = object[key] = value.replaceAll( - replaceKey, - env.replaceText[replaceKey] - ); - } - } - } - }); - } + replaceText(schema, env.replaceText, env.replaceTextIgnoreKeys); return ( diff --git a/src/store/service.ts b/src/store/service.ts index ed2e3f1a4..45c32eada 100644 --- a/src/store/service.ts +++ b/src/store/service.ts @@ -4,6 +4,7 @@ import {Api, ApiObject, Payload, fetchOptions} from '../types'; import {extendObject, isEmpty, isObject} from '../utils/helper'; import {ServerError} from '../utils/errors'; import {normalizeApiResponseData} from '../utils/api'; +import {replaceText} from '../utils/replaceText'; export const ServiceStore = iRendererStore .named('ServiceStore') @@ -402,6 +403,9 @@ export const ServiceStore = iRendererStore ); } else { if (json.data) { + const env = getEnv(self); + replaceText(json.data, env.replaceText, env.replaceTextIgnoreKeys); + self.schema = Array.isArray(json.data) ? json.data : { diff --git a/src/utils/replaceText.ts b/src/utils/replaceText.ts new file mode 100644 index 000000000..2df066994 --- /dev/null +++ b/src/utils/replaceText.ts @@ -0,0 +1,30 @@ +/** + * 对文本进行替换 + */ + +import {isObject, JSONTraverse} from './helper'; + +export function replaceText( + schema: any, + replaceText: {[propName: string]: string}, + replaceTextIgnoreKeys: String[] +) { + // 进行文本替换 + if (replaceText && isObject(replaceText)) { + const replaceKeys = Object.keys(replaceText); + replaceKeys.sort((a, b) => b.length - a.length); // 避免用户将短的放前面 + const IgnoreKeys = new Set(replaceTextIgnoreKeys || []); + JSONTraverse(schema, (value: any, key: string, object: any) => { + if (typeof value === 'string' && !IgnoreKeys.has(key)) { + for (const replaceKey of replaceKeys) { + if (~value.indexOf(replaceKey)) { + value = object[key] = value.replaceAll( + replaceKey, + replaceText[replaceKey] + ); + } + } + } + }); + } +}