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] + ); + } + } + } + }); + } +}