import { defineStore } from 'pinia'; import { Session } from '/@/utils/storage'; import { getUserInfo, getButtons } from '/@/api/system/user.js' /** * 用户信息 * @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; } }, 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; }, // 模拟接口数据 // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP async getApiUserInfo() { return new Promise((resolve) => { // 获取用户信息 getUserInfo().then((user) => { const arr = []; arr.push(user.roleName); 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: [], ...user }; // 存储用户信息到浏览器缓存 Session.set('userInfo', userInfos); 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) // }) }); }); }, }, });