chenyc
2024-12-23 000a89a294e2cdb493bb3ce178d686e58bd40196
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
import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { Session } from '/@/utils/storage';
import { getuserinfo } from '../api/login';
 
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
    state: (): UserInfosState => ({
        userInfos: {
            userName: '',
            code:'',
            photo: '',
            time: 0,
            roles: [],
            authBtnList: [],
            clientCode:'',
            clientName:'',
            clientInfos:[],
        },
    }),
    actions: {
        async setUserInfos() {
            // 存储用户信息到浏览器缓存
            if (Session.get('userInfo')) {
                this.userInfos = Session.get('userInfo');
            } else {
                console.log('Session里没有userInfo')
                const userInfos = <UserInfos>await this.getApiUserInfo();
                this.userInfos = userInfos;
                console.log('完成了')
            }
        },
        // 模拟接口数据
        // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
        async getApiUserInfo() {
            const userinfoRes = await getuserinfo()
            console.log('-----------用token 换用户信息---------------')
            console.log(userinfoRes.data)
            const userRet=userinfoRes.data
            return new Promise((resolve) => {
                setTimeout(() => {
                    // 模拟数据,请求接口时,记得删除多余代码及对应依赖的引入
                    const userName = userRet.userName
                    const userCode = userRet.code
                    // 模拟数据
                    let defaultRoles: Array<string> = [];
                    let defaultAuthBtnList: Array<string> = [];
                    // admin 页面权限标识,对应路由 meta.roles,用于控制路由的显示/隐藏
                    let adminRoles: Array<string> = ['admin'];
                    // admin 按钮权限标识
                    let adminAuthBtnList: Array<string> = ['btn.add', 'btn.del', 'btn.edit', 'btn.link'];
                    // test 页面权限标识,对应路由 meta.roles,用于控制路由的显示/隐藏
                    let testRoles: Array<string> = ['common'];
                    // test 按钮权限标识
                    let testAuthBtnList: Array<string> = ['btn.add', 'btn.link'];
                    // 不同用户模拟不同的用户权限
                    let isadmin=false;
                    if(userRet.roles.length > 0){
                        const x=userRet.roles.findIndex(e=>{return e.roleName==='admin'})
                        if(x>=0){
                            isadmin=true
                        }
                        else{
                            isadmin=false
                        }
                    }
                    if (isadmin) {
                        defaultRoles = adminRoles;
                        defaultAuthBtnList = adminAuthBtnList;
                    } else {
                        defaultRoles = testRoles;
                        defaultAuthBtnList = testAuthBtnList;
                    }
                    // 用户信息模拟数据
                    const userInfos = {
                        userName: userName,
                        code:userCode,
                        photo:userRet.userAvatar===''
                                ? 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500'
                                : userRet.userAvatar,
                        time: new Date().getTime(),
                        roles: defaultRoles,
                        authBtnList: defaultAuthBtnList,
                        clientCode:userRet.currentClientInfo.code,
                        clientName:userRet.currentClientInfo.clientName,
                        clientInfos:userRet.clientInfos
                    };
                    console.log('-----------------2222------------------------------')
                    console.log(userInfos)
                    // Session.set('userInfo', userInfos);
                    resolve(userInfos);
                }, 0);
            });
        },
        async setUserCilent(client:clientInfo){
            this.userInfos.clientCode=client.code
            this.userInfos.clientName=client.clientName
        }    
    },
});