2025-03-12 12:08:23 +08:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
2023-04-02 01:01:56 +08:00
|
|
|
export const useDictStore = defineStore('dict', () => {
|
2024-11-11 15:10:24 +08:00
|
|
|
const dict = ref<Map<string, DictDataOption[]>>(new Map());
|
2023-04-02 01:01:56 +08:00
|
|
|
|
2023-04-03 00:05:09 +08:00
|
|
|
/**
|
|
|
|
* 获取字典
|
|
|
|
* @param _key 字典key
|
|
|
|
*/
|
|
|
|
const getDict = (_key: string): DictDataOption[] | null => {
|
2024-11-11 15:10:24 +08:00
|
|
|
if (!_key) {
|
2023-04-03 00:05:09 +08:00
|
|
|
return null;
|
|
|
|
}
|
2024-11-11 15:10:24 +08:00
|
|
|
return dict.value.get(_key) || null;
|
2023-04-03 00:05:09 +08:00
|
|
|
};
|
2023-04-02 01:01:56 +08:00
|
|
|
|
2023-04-03 00:05:09 +08:00
|
|
|
/**
|
|
|
|
* 设置字典
|
|
|
|
* @param _key 字典key
|
|
|
|
* @param _value 字典value
|
|
|
|
*/
|
|
|
|
const setDict = (_key: string, _value: DictDataOption[]) => {
|
2024-11-11 15:10:24 +08:00
|
|
|
if (!_key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
dict.value.set(_key, _value);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error in setDict:', e);
|
|
|
|
return false;
|
2023-04-03 00:05:09 +08:00
|
|
|
}
|
|
|
|
};
|
2023-04-02 01:01:56 +08:00
|
|
|
|
2023-04-03 00:05:09 +08:00
|
|
|
/**
|
|
|
|
* 删除字典
|
|
|
|
* @param _key
|
|
|
|
*/
|
|
|
|
const removeDict = (_key: string): boolean => {
|
2024-11-11 15:10:24 +08:00
|
|
|
if (!_key) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-03 00:05:09 +08:00
|
|
|
try {
|
2024-11-11 15:10:24 +08:00
|
|
|
return dict.value.delete(_key);
|
2023-04-03 00:05:09 +08:00
|
|
|
} catch (e) {
|
2024-11-11 15:10:24 +08:00
|
|
|
console.error('Error in removeDict:', e);
|
|
|
|
return false;
|
2023-04-03 00:05:09 +08:00
|
|
|
}
|
|
|
|
};
|
2023-04-02 01:01:56 +08:00
|
|
|
|
2023-04-03 00:05:09 +08:00
|
|
|
/**
|
|
|
|
* 清空字典
|
|
|
|
*/
|
|
|
|
const cleanDict = (): void => {
|
2024-11-11 15:10:24 +08:00
|
|
|
dict.value.clear();
|
2023-04-03 00:05:09 +08:00
|
|
|
};
|
2023-04-02 01:01:56 +08:00
|
|
|
|
2023-04-03 00:05:09 +08:00
|
|
|
return {
|
|
|
|
dict,
|
|
|
|
getDict,
|
|
|
|
setDict,
|
|
|
|
removeDict,
|
|
|
|
cleanDict
|
|
|
|
};
|
2023-04-02 01:01:56 +08:00
|
|
|
});
|