dict.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. export const useDictStore = defineStore('dict', () => {
  2. const dict = ref<
  3. Array<{
  4. key: string;
  5. value: DictDataOption[];
  6. }>
  7. >([]);
  8. /**
  9. * 获取字典
  10. * @param _key 字典key
  11. */
  12. const getDict = (_key: string): DictDataOption[] | null => {
  13. if (_key == null && _key == '') {
  14. return null;
  15. }
  16. try {
  17. for (let i = 0; i < dict.value.length; i++) {
  18. if (dict.value[i].key == _key) {
  19. return dict.value[i].value;
  20. }
  21. }
  22. } catch (e) {
  23. return null;
  24. }
  25. return null;
  26. };
  27. /**
  28. * 设置字典
  29. * @param _key 字典key
  30. * @param _value 字典value
  31. */
  32. const setDict = (_key: string, _value: DictDataOption[]) => {
  33. if (_key !== null && _key !== '') {
  34. dict.value.push({
  35. key: _key,
  36. value: _value
  37. });
  38. }
  39. };
  40. /**
  41. * 删除字典
  42. * @param _key
  43. */
  44. const removeDict = (_key: string): boolean => {
  45. let bln = false;
  46. try {
  47. for (let i = 0; i < dict.value.length; i++) {
  48. if (dict.value[i].key == _key) {
  49. dict.value.splice(i, 1);
  50. return true;
  51. }
  52. }
  53. } catch (e) {
  54. bln = false;
  55. }
  56. return bln;
  57. };
  58. /**
  59. * 清空字典
  60. */
  61. const cleanDict = (): void => {
  62. dict.value = [];
  63. };
  64. return {
  65. dict,
  66. getDict,
  67. setDict,
  68. removeDict,
  69. cleanDict
  70. };
  71. });
  72. export default useDictStore;