yuanhao
2025-06-04 35dab133f3085a6bce2f045d2d28605aa64e8e61
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
import {
    ACCESSTOKEN,
    config
} from '@/common/config.js'
class Dict {
    constructor(dict) {
        this.dict = dict;
    }
    async init(names, vm) {
        const ps = [];
        names.forEach((name) => {
            if (vm.prototype.$u.vuex('dictList')) {
                vm.set(this.dict, name, vm.prototype.$u.vuex('dictList'));
            } else {
                vm.set(this.dict, name, []);
                ps.push(
                    uni.request({
                        url: config.baseUrl + '/system/data/list',
                        header: {
                            'Accept': 'application/json, text/plain, */*',
                            [ACCESSTOKEN]: 'Bearer' + " " + uni.getStorageSync("sessionToken")
                        },
                        data: {
                            dictType: name,
                            status: 0
                        },
                        complete: (res) => {
                            if (res.statusCode === 200) {
                                if (res.data.code === 401) {
                                    // vm.prototype.$u.toast(res.data.msg || "未知错误");
                                    uni.removeStorageSync("sessionToken")
                                    setTimeout(() => {
                                        uni.reLaunch({
                                            url: '/pages/index/index'
                                        })
                                    }, 500)
                                } else if (res.data.code === 200) {
                                    this.dict[name] = Object.freeze(res.data.rows);
                                    vm.prototype.$u.vuex('dictList', {
                                        [name]: Object.freeze(res.data.rows)
                                    })
                                } else {
                                    vm.$u.toast(res.data.msg || "未知错误");
                                }
                            }
                        }
                    })
                );
            }
        });
        await Promise.all(ps);
    }
}
 
const install = function(Vue) {
    Vue.mixin({
        data() {
            if (
                this.$options.dicts instanceof Array &&
                this.$options.dicts.length > 0
            ) {
                return {
                    dict: {}
                };
            } else {
                return {};
            }
        },
        created() {
            if (
                this.$options.dicts instanceof Array &&
                this.$options.dicts.length > 0
            ) {
                new Dict(this.dict).init(this.$options.dicts, Vue);
            }
        },
        methods: {
            getDicLabel(key, value) {
                let cur = this.dict[key]?.find(item => item.dictValue == value);
                if (!cur) {
                    return '';
                }
                return cur.dictLabel
            },
            getDict(key, value) {
                let cur = this.dict[key]?.find(item => item.dictValue == value);
                if (!cur) {
                    return {
                        dictLabel: ''
                    };
                }
                return cur
            }
        }
    });
};
 
export default {
    install
};