chengsc
2025-04-14 d2588d8d1305171e7716055b23a6b5706e9cc04b
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { defineStore } from 'pinia';
import { Local, Session } from '/@/utils/storage';
import Watermark from '/@/utils/watermark';
import { useThemeConfig } from '/@/stores/themeConfig';
 
import { getAPI } from '/@/utils/axios-utils';
import { SysAuthApi, SysConstApi, SysDictTypeApi } from '/@/api-services/api';
 
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
    state: (): UserInfosState => ({
        userInfos: {} as any,
        constList: [] as any,
        dictList: {} as any,
        dictListInt: {} as any,
    }),
    getters: {
        // // 获取系统常量列表
        // async getSysConstList(): Promise<any[]> {
        //     var res = await getAPI(SysConstApi).apiSysConstListGet();
        //     this.constList = res.data.result ?? [];
        //     return this.constList;
        // },
    },
    actions: {
        // 存储用户信息到浏览器缓存
        async setUserInfos() {
            if (Session.get('userInfo')) {
                this.userInfos = Session.get('userInfo');
            } else {
                const userInfos = <UserInfos>await this.getApiUserInfo();
                this.userInfos = userInfos;
            }
        },
 
        // 存储常量信息到浏览器缓存
        async setConstList() {
            if (Session.get('constList')) {
                this.constList = Session.get('constList');
            } else {
                const constList = <any[]>await this.getSysConstList();
                Session.set('constList', constList);
                this.constList = constList;
            }
        },
 
        // 存储字典信息到浏览器缓存
        async setDictList() {
            var res = await getAPI(SysDictTypeApi).apiSysDictTypeAllDictListGet();
            this.dictList = res.data.result;
            // if (Session.get('dictList')) {
            //     this.dictList = Session.get('dictList');
            // } else {
            //    const dictList = <any[]>await this.getAllDictList();
            //    Session.set('dictList', dictList);
            //    this.dictList = dictList;
            // }
        },
 
        // 获取当前用户信息
        getApiUserInfo() {
            return new Promise((resolve) => {
                getAPI(SysAuthApi)
                    .apiSysAuthUserInfoGet()
                    .then(async (res: any) => {
                        if (res.data.result == null) return;
                        var d = res.data.result;
                        const userInfos = {
                            id: d.id,
                            account: d.account,
                            realName: d.realName,
                            phone: d.phone,
                            idCardNum: d.idCardNum,
                            email: d.email,
                            accountType: d.accountType,
                            avatar: d.avatar ?? '/Upload/logo.png',
                            address: d.address,
                            signature: d.signature,
                            orgId: d.orgId,
                            orgName: d.orgName,
                            posName: d.posName,
                            roles: d.roleIds,
                            authBtnList: d.buttons,
                            time: new Date().getTime(),
                        };
                        // vue-next-admin 提交Id:225bce7 提交消息:admin-23.03.26:发布v2.4.32版本
                        // 增加了下面代码,引起当前会话的用户信息不会刷新,如:重新提交的头像不更新,需要新开一个页面才能正确显示
                        // Session.set('userInfo', userInfos);
 
                        // 用户水印
                        const storesThemeConfig = useThemeConfig();
                        storesThemeConfig.themeConfig.watermarkText = d.watermarkText ?? '';
                        if (storesThemeConfig.themeConfig.isWatermark) Watermark.set(storesThemeConfig.themeConfig.watermarkText);
                        else Watermark.del();
 
                        Local.remove('themeConfig');
                        Local.set('themeConfig', storesThemeConfig.themeConfig);
 
                        resolve(userInfos);
                    });
            });
        },
 
        // 获取常量集合
        getSysConstList() {
            return new Promise((resolve) => {
                getAPI(SysConstApi)
                    .apiSysConstListGet()
                    .then(async (res: any) => {
                        resolve(res.data.result ?? []);
                    });
            });
        },
 
        // 获取字典集合
        getAllDictList() {
            return new Promise((resolve) => {
                if (this.dictList) {
                    resolve(this.dictList);
                } else {
                    getAPI(SysDictTypeApi)
                        .apiSysDictTypeAllDictListGet()
                        .then((res: any) => {
                            resolve(res.data.result ?? []);
                        });
                }
            });
        },
 
        // 根据字典类型和代码取字典项
        getDictItemByCode(typePCode: string, val: string) {
            if (val != undefined && val !== '') {
                const _val = val.toString();
                const ds = this.getDictDatasByCode(typePCode);
                for (const element of ds) {
                    if (element.code === _val) {
                        return element;
                    }
                }
            }
            return {};
        },
 
        // 根据字典类型和值取描述
        getDictLabelByVal(typePCode: string, val: string) {
            if (val != undefined && val !== '') {
                const _val = val.toString();
                const ds = this.getDictDatasByCode(typePCode);
                for (const element of ds) {
                    if (element.value === _val) {
                        return element;
                    }
                }
            }
            return {};
        },
 
        // 根据字典类型和描述取值
        getDictValByLabel(typePCode: string, label: string) {
            if (!label) return '';
            const ds = this.getDictDatasByCode(typePCode);
            for (const element of ds) {
                if (element.name === label) {
                    return element;
                }
            }
            return ''; // 明确返回空字符串
        },
 
        // 根据字典类型获取字典数据
        getDictDatasByCode(dictTypeCode: string) {
            return this.dictList[dictTypeCode] || [];
        },
 
        // 根据字典类型获取字典数据(值转为数字类型)
        getDictIntDatasByCode(dictTypeCode: string) {
            let ds = this.dictListInt[dictTypeCode];
            if (ds) {
                return ds;
            }
 
            const dictList = this.dictList[dictTypeCode];
            if (dictList) {
                ds = dictList.map((element: { code: any }) => {
                    return { ...element, code: Number(element.code) };
                });
                this.dictListInt[dictTypeCode] = ds;
            }
 
            return ds;
        },
    },
});