|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|