基于vue3.0和element-plus的组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

44 lines
1.1 KiB

import { computed } from "vue";
import { useStore } from "vuex";
type Callback = () => Record<string, string> | Promise<Record<string, string>>;
const callbacks: Record<string, Callback> = {};
export function useSysDict() {
const store = useStore();
const normalize = (content: Record<string, string>) => {
if (content == null) return {};
return Object.fromEntries(
Object.entries(content).map(([k, v]) => {
const parsed = parseInt(v);
return [k, isNaN(parsed) ? v : parsed];
})
);
};
const updateDict = async (dictIds: string[], registerCallbacks: Record<string, Callback> = {}) => {
await store.dispatch("getDictMap", dictIds);
Object.assign(callbacks, registerCallbacks);
for (const id of dictIds) {
const cb = callbacks[id];
const resp = await cb?.();
if (resp) {
store.commit("updateDict", [id, normalize(resp)]);
}
}
};
const unregister = (ids: string[]) => {
for (const id of ids) {
delete callbacks[id];
}
};
return {
sysDict: computed<Record<string, Record<string, string>>>(() => store.state.dict),
updateDict,
unregister,
};
}