vordm-admin/src/stores/userInfo.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-03-29 14:04:51 +08:00
import { defineStore } from 'pinia';
import { Session } from '/@/utils/storage';
2023-03-30 16:45:56 +08:00
import { getUserInfo, getButtons } from '/@/api/system/user.js'
2023-03-29 14:04:51 +08:00
/**
* 用户信息
* @methods setUserInfos 设置用户信息
*/
export const useUserInfo = defineStore('userInfo', {
state: () => ({
userInfos: {
userName: '',
photo: '',
time: 0,
roles: [],
authBtnList: [],
},
}),
actions: {
async setUserInfos() {
// 存储用户信息到浏览器缓存
if (Session.get('userInfo')) {
this.userInfos = Session.get('userInfo');
} else {
const userInfos = await this.getApiUserInfo();
this.userInfos = userInfos;
}
},
2023-03-30 16:45:56 +08:00
async Buttons() {
return new Promise((resolve) => {
getButtons().then(data => {
console.log(data)
resolve(this.formatBtns(data));
})
})
},
formatBtns(data) {
let map = [];
data.forEach(item => {
map.push(item.code)
//判断是否存在子菜单
if (item.children) {
map = map.concat(this.formatBtns(item.children));
}
})
return map;
},
2023-03-29 14:04:51 +08:00
// 模拟接口数据
// https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
async getApiUserInfo() {
return new Promise((resolve) => {
2023-03-30 16:45:56 +08:00
// 获取用户信息
getUserInfo().then((user) => {
const arr = [];
arr.push(user.roleName);
2023-03-29 14:04:51 +08:00
const userInfos = {
2023-03-30 16:45:56 +08:00
userName: user.account,
photo: user.account === 'admin'
? 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500'
: 'https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
2023-03-29 14:04:51 +08:00
time: new Date().getTime(),
2023-03-30 16:45:56 +08:00
roles: arr,
authBtnList: [],
...user
2023-03-29 14:04:51 +08:00
};
2023-03-30 16:45:56 +08:00
// 存储用户信息到浏览器缓存
2023-03-29 14:04:51 +08:00
Session.set('userInfo', userInfos);
2023-03-30 16:45:56 +08:00
resolve(userInfos)
// 获取权限按钮
// this.Buttons().then((res2) => {
// // 用户信息模拟数据
// const userInfos = {
// userName: user.account,
// photo: user.account === 'admin'
// ? 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500'
// : 'https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
// time: new Date().getTime(),
// roles: arr,
// authBtnList: res2,
// ...user
// };
// // 存储用户信息到浏览器缓存
// Session.set('userInfo', userInfos);
// resolve(userInfos)
// })
});
2023-03-29 14:04:51 +08:00
});
2023-03-30 16:45:56 +08:00
2023-03-29 14:04:51 +08:00
},
},
});