forked from mengyxu/noob-components
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.2 KiB
44 lines
1.2 KiB
|
6 months ago
|
import { computed } from "vue";
|
||
|
|
import { useStore } from "vuex";
|
||
|
|
|
||
|
|
export function useSysDict() {
|
||
|
|
type Callback = () => Record<string, string> | Promise<Record<string, string>>;
|
||
|
|
const store = useStore();
|
||
|
|
const callbacks: Record<string, Callback> = {};
|
||
|
|
|
||
|
|
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,
|
||
|
|
};
|
||
|
|
}
|