UNIVPLMDataIntegration/Web/src/stores/userInfo.ts

131 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-06-15 13:02:35 +08:00
import { defineStore } from 'pinia';
import { Local, Session } from '/@/utils/storage';
import Watermark from '/@/utils/watermark';
import { useThemeConfig } from '/@/stores/themeConfig';
2024-06-15 13:02:35 +08:00
import { getAPI } from '/@/utils/axios-utils';
import { SysAuthApi, SysConstApi, SysDictTypeApi, SysRoleApi } from '/@/api-services/api';
2024-06-15 13:02:35 +08:00
/**
*
* @methods setUserInfos
*/
export const useUserInfo = defineStore('userInfo', {
state: (): UserInfosState => ({
userInfos: {} as any,
constList: [] as any,
dictList: {} as any,
userTableList: [] as any, // 用户表格字段数据
2024-06-15 13:02:35 +08:00
}),
2024-11-25 16:27:14 +08:00
getters: {},
2024-06-15 13:02:35 +08:00
actions: {
// 存储用户信息到浏览器缓存
async setUserInfos() {
2024-11-25 16:27:14 +08:00
this.userInfos = Session.get('userInfo') ?? <UserInfos>await this.getApiUserInfo();
2024-06-15 13:02:35 +08:00
},
// 存储常量信息到浏览器缓存
async setConstList() {
2024-11-25 16:27:14 +08:00
this.constList = Session.get('constList') ?? <any[]>await this.getSysConstList();
if (!Session.get('constList')) Session.set('constList', this.constList);
2024-06-15 13:02:35 +08:00
},
// 存储字典信息到浏览器缓存
async setDictList() {
2024-11-25 16:27:14 +08:00
this.dictList = await getAPI(SysDictTypeApi)
.apiSysDictTypeAllDictListGet()
.then((res) => res.data.result ?? {});
for (const key in this.dictList) if (key.endsWith('Enum')) this.dictList[key].forEach((e: any) => (e.code = Number(e.code)));
2024-06-15 13:02:35 +08:00
},
// 存储用户表格列到浏览器缓存
async setUserTables() {
2024-11-25 16:27:14 +08:00
this.userTableList = Session.get('userTable') ?? <any[]>await this.getUserTableList();
if (!Session.get('userTable')) Session.set('userTable', this.userTableList);
},
2024-06-15 13:02:35 +08:00
// 获取当前用户信息
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,
2024-08-12 21:14:38 +08:00
avatar: d.avatar ?? '/upload/logo.png',
2024-06-15 13:02:35 +08:00
address: d.address,
signature: d.signature,
orgId: d.orgId,
orgName: d.orgName,
posName: d.posName,
roles: d.roleIds,
2024-07-22 02:54:41 +08:00
authApiList: d.apis,
2024-09-01 13:07:42 +08:00
lastChangePasswordTime: d.lastChangePasswordTime,
2024-06-15 13:02:35 +08:00
time: new Date().getTime(),
};
// vue-next-admin 提交Id225bce7 提交消息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);
2024-06-15 13:02:35 +08:00
resolve(userInfos);
});
});
},
2024-11-25 16:27:14 +08:00
// 获取当前用户表格列集合
getUserTableList() {
2024-06-15 13:02:35 +08:00
return new Promise((resolve) => {
2024-11-25 16:27:14 +08:00
getAPI(SysRoleApi)
.apiSysRoleUserRoleTableListGet()
2024-06-15 13:02:35 +08:00
.then(async (res: any) => {
resolve(res.data.result ?? []);
});
});
},
2024-11-25 16:27:14 +08:00
// 获取常量集合
getSysConstList() {
2024-06-15 13:02:35 +08:00
return new Promise((resolve) => {
2024-11-25 16:27:14 +08:00
getAPI(SysConstApi)
.apiSysConstListGet()
.then(async (res: any) => {
resolve(res.data.result ?? []);
});
2024-06-15 13:02:35 +08:00
});
},
2024-11-25 16:27:14 +08:00
// 根据常量类名获取常量数据
getConstDataByTypeCode(typeCode: string) {
return this.constList.find((item: any) => item.code === typeCode)?.data?.result || [];
2024-06-15 13:02:35 +08:00
},
2024-11-25 16:27:14 +08:00
// 根据常量类名和编码获取常量值
getConstItemNameByType(typeCode: string, itemCode: string) {
const data = this.getConstDataByTypeCode(typeCode);
return data.find((item: any) => item.code === itemCode)?.name;
2024-06-15 13:02:35 +08:00
},
// 根据字典类型获取字典数据
2024-11-25 16:27:14 +08:00
getDictDataByCode(dictTypeCode: string) {
2024-06-15 13:02:35 +08:00
return this.dictList[dictTypeCode] || [];
},
},
});