ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type { SysConfigInfo } from '@vben/types';
 
import { merge } from '@jnpf/utils';
 
import { defineStore } from 'pinia';
 
import { getDictionaryAll } from '#/api/systemData/dictionary';
 
interface DicChildItem {
  isTree?: number;
  id: string;
  enCode: string;
  fullName: string;
}
interface DicItem extends DicChildItem {
  dictionaryList: DicChildItem[];
}
interface BaseState {
  // 系统配置
  sysConfigInfo: Partial<SysConfigInfo>;
  dictionaryList: DicItem[];
}
 
/**
 * 系统配置相关
 */
export const useBaseStore = defineStore('base', {
  getters: {
    getDicList(): DicItem[] {
      return this.dictionaryList;
    },
    getSysConfig(): Partial<SysConfigInfo> {
      return this.sysConfigInfo;
    },
  },
  actions: {
    setSysConfig(config: Partial<SysConfigInfo>) {
      this.sysConfigInfo = merge(config as any, this.sysConfigInfo || {});
    },
    setDictionaryList(list: DicItem[] = []) {
      this.dictionaryList = list;
    },
    async getDictionaryAll(): Promise<DicItem[]> {
      try {
        if (this.dictionaryList.length > 0) {
          return this.dictionaryList;
        } else {
          const res = await getDictionaryAll();
          if (!res) return [];
          this.dictionaryList = res.data.list;
          return res.data.list;
        }
      } catch {
        return [];
      }
    },
    async getDictionaryData(encode: string, id: string = ''): Promise<DicChildItem | DicChildItem[]> {
      try {
        let data: Partial<DicItem> = {};
        let json: DicChildItem | DicChildItem[] = [];
        let list: DicItem[] = [];
        list = this.dictionaryList.length === 0 ? await this.getDictionaryAll() : this.dictionaryList;
        if (encode) {
          const arr = list.filter((o) => o.enCode === encode);
          if (arr.length === 0) return [];
          data = arr[0] as DicItem;
          if (id) {
            let rowData: DicChildItem[] = [];
            if (data.isTree) {
              function findData(list) {
                for (const e of list) {
                  if (e.id === id) {
                    rowData[0] = e;
                    break;
                  }
                  if (e.children && e.children.length > 0) {
                    findData(e.children);
                  }
                }
              }
              findData(data.dictionaryList);
            } else {
              rowData = (data.dictionaryList as DicChildItem[]).filter((o) => o.id === id);
            }
            json =
              rowData.length > 0
                ? (rowData[0] as DicItem)
                : {
                    id: '',
                    fullName: '',
                    enCode: '',
                  };
          } else {
            json = data.dictionaryList as DicChildItem[];
          }
          return json;
        }
        return json;
      } catch {
        return [];
      }
    },
    async getDicDataSelector(value: string, key: string = 'id'): Promise<DicChildItem[]> {
      try {
        let data: Partial<DicItem> = {};
        let json: DicChildItem[] = [];
        let list: DicItem[] = [];
        list = this.dictionaryList.length === 0 ? await this.getDictionaryAll() : this.dictionaryList;
        if (!value) return [];
        const arr = list.filter((o) => {
          return o[key] === value;
        });
        if (arr.length === 0) return [];
        data = arr[0] as DicItem;
        json = data.dictionaryList as DicChildItem[];
        return json;
      } catch {
        return [];
      }
    },
    $reset() {
      this.dictionaryList = [];
    },
  },
  persist: {
    // 持久化
    pick: ['dictionaryList', 'sysConfigInfo'],
  },
  state: (): BaseState => ({
    dictionaryList: [],
    sysConfigInfo: {},
  }),
});