刘光辉
5 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
134
135
136
137
138
139
140
141
142
import type { SysConfigInfo } from '@vben/types';
 
import { merge } from '@jnpf/utils';
 
import { defineStore } from 'pinia';
 
import { getDictionaryAll, getDictionaryDataSelector } 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(force = false): Promise<DicItem[]> {
      try {
        if (!force && this.dictionaryList.length > 0) {
          return this.dictionaryList;
        }
        const res = await getDictionaryAll();
        if (!res) return force ? this.dictionaryList : [];
        this.dictionaryList = res.data.list;
        return res.data.list;
      } catch {
        return this.dictionaryList || [];
      }
    },
    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) {
          let arr = list.filter((o) => o.enCode === encode);
          // 登录后新增的字典不在持久化缓存中,按编码未命中时强制拉一次全量
          if (arr.length === 0) {
            list = await this.getDictionaryAll(true);
            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 {
        if (!value) return [];
        const list = this.dictionaryList.length === 0 ? await this.getDictionaryAll() : this.dictionaryList;
        const cachedIndex = list.findIndex((item) => item[key] === value);
        const cached = cachedIndex === -1 ? undefined : list[cachedIndex];
        if (cached?.dictionaryList?.length) return cached.dictionaryList;
 
        // The persisted aggregate cache does not include dictionaries created after login.
        const res = await getDictionaryDataSelector(value);
        const options = res?.data?.list;
        if (!Array.isArray(options)) return cached?.dictionaryList || [];
 
        if (cached) {
          this.dictionaryList.splice(cachedIndex, 1, { ...cached, dictionaryList: options });
        } else if (key === 'id') {
          this.dictionaryList.push({ id: value, enCode: '', fullName: '', dictionaryList: options });
        }
        return options;
      } catch {
        return [];
      }
    },
    $reset() {
      this.dictionaryList = [];
    },
  },
  persist: {
    // 持久化
    pick: ['dictionaryList', 'sysConfigInfo'],
  },
  state: (): BaseState => ({
    dictionaryList: [],
    sysConfigInfo: {},
  }),
});