dict.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { debug } from "console";
  2. export const useDictStore = defineStore('dict', () => {
  3. const dict = ref<Map<string, DictDataOption[]>>(new Map());
  4. /**
  5. * 获取字典
  6. * @param _key 字典key
  7. */
  8. const getDict = (_key: string): DictDataOption[] | null => {
  9. if (!_key) {
  10. return null;
  11. }
  12. return dict.value.get(_key) || null;
  13. };
  14. /**
  15. * 设置字典
  16. * @param _key 字典key
  17. * @param _value 字典value
  18. */
  19. const setDict = (_key: string, _value: DictDataOption[]) => {
  20. if (!_key) {
  21. return false;
  22. }
  23. try {
  24. dict.value.set(_key, _value);
  25. return true;
  26. } catch (e) {
  27. console.error('Error in setDict:', e);
  28. return false;
  29. }
  30. };
  31. /**
  32. * 删除字典
  33. * @param _key
  34. */
  35. const removeDict = (_key: string): boolean => {
  36. if (!_key) {
  37. return false;
  38. }
  39. try {
  40. return dict.value.delete(_key);
  41. } catch (e) {
  42. console.error('Error in removeDict:', e);
  43. return false;
  44. }
  45. };
  46. /**
  47. * 清空字典
  48. */
  49. const cleanDict = (): void => {
  50. dict.value.clear();
  51. };
  52. return {
  53. dict,
  54. getDict,
  55. setDict,
  56. removeDict,
  57. cleanDict
  58. };
  59. });
  60. export default useDictStore;