wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
import { defineStore } from 'pinia';
import { computed, reactive, toRaw } from 'vue';
import { service } from '/@/cool';
import { deepTree } from '/@/cool/utils';
import { isDev } from '/@/config';
import { assign, isArray, orderBy } from 'lodash-es';
import { deepFind, isEmpty } from '../utils';
 
const useDictStore = defineStore('dict', () => {
    // 对象数据
    const data = reactive<Dict.Data>({});
 
    // 获取
    function get(name: Dict.Key, sort?: 'desc' | 'asc') {
        return computed(() => orderBy(data[name] || [], 'orderNum', sort));
    }
 
    // 查找
    function find(name: Dict.Key, value: any | any[]) {
        const arr = isArray(value) ? value : [value];
        return arr.filter(e => e !== undefined).map(v => deepFind(v, get(name).value));
    }
 
    // 刷新
    async function refresh(types?: Dict.Key[]) {
        return service.dict.info
            .data({
                types: types?.filter(e => !isEmpty(e))
            })
            .then((res: Dict.Data) => {
                const d = {};
 
                for (const [i, arr] of Object.entries(res)) {
                    arr.forEach(e => {
                        e.label = e.name;
 
                        if (isEmpty(e.value)) {
                            e.value = e.id;
                        }
                    });
 
                    d[i] = deepTree(arr, 'desc');
                }
 
                assign(data, d);
 
                if (isDev) {
                    console.group('字典数据');
                    console.log(toRaw(data));
                    console.groupEnd();
                }
 
                return data;
            });
    }
 
    return {
        data,
        get,
        find,
        refresh
    };
});
 
export { useDictStore };