From 928c61ccddebc8d2c697b86ee9bee0c207330a8c Mon Sep 17 00:00:00 2001 From: schangxiang@126.com <schangxiang@126.com> Date: 周二, 06 5月 2025 07:18:22 +0800 Subject: [PATCH] 222 --- HIAWms/web/src/libs/Provider/Provider.ts | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 91 insertions(+), 0 deletions(-) diff --git a/HIAWms/web/src/libs/Provider/Provider.ts b/HIAWms/web/src/libs/Provider/Provider.ts new file mode 100644 index 0000000..676363f --- /dev/null +++ b/HIAWms/web/src/libs/Provider/Provider.ts @@ -0,0 +1,91 @@ +import { onBeforeMount, onUnmounted, reactive, ref, toRefs, Ref } from 'vue' +import set from 'lodash/set' +import get from 'lodash/get' + +interface ControllerType { + Models: Record<string, Function> +} + +let Modules = {} +/** + * 鑾峰彇鏂囦欢鍚� + * @param filePath + * @returns + */ +const extractNameFromPath = (filePath: string) => { + const regex = /\/([^\/0-9][A-Za-z-0-9]+)\.ts$/ + const match = filePath.match(regex) + + if (match && match[1]) { + return match[1] + } else { + return null + } +} + +/** + * 杞崲鎴愬悕绉板嚱鏁版暟缁� + * @param fileMap + */ +const getFunctionByName = (fileMap: Record<string, Function>) => { + const entries = Object.entries(fileMap) + return entries.map(([filePath, fn]) => { + const name = extractNameFromPath(filePath) + if (name) { + return [name, fn] + } else { + throw new Error(`${filePath} 鏂囦欢鍚嶆牸寮忎笉姝g‘锛岃妫�鏌) + } + }) +} + +/** + * 瀛樺叆modules + * @param data [[a,b]] + * @param namespace + * @param bool 鏄惁瀹炰緥鍖栫被锛岄粯璁や负false + */ +const saveModules = (data: (string | Function)[][]) => { + for (let i = 0; i < data.length; i++) { + const [name, Module]: any[] = data[i] + + Object.entries(Module).map(([hookName, fn]: any) => { + const fnKey = hookName.toLocaleLowerCase() + try { + set(Modules, fnKey, new fn()) + } catch (error) { + console.error(error) + throw new Error(`${name} 绫讳腑 ${fnKey} 鏂规硶鏍煎紡涓嶆纭紝璇锋鏌) + } + }) + } +} + +/** + * 鍒濆鍖杕odels + * @param param + */ +export const createModels = ({ Models }: ControllerType) => { + const models = getFunctionByName(Models) + saveModules(models) +} + +/** + * 鑾峰彇model + * @param modelName + * @returns + */ +export const injectModel = <T>(modelName: string): T => { + const key = modelName.toLocaleLowerCase() + const InstanceModel = get(Modules, key) + return InstanceModel +} + +/** + * 鑾峰彇鍏ㄥ眬models + * @param modelName + * @returns + */ +export const injectModels = () => { + return Modules +} -- Gitblit v1.9.3