wujingjing
2025-03-27 90c54c9212866cc5f2568ac950716e5cadf826d3
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
import { ElMessage } from 'element-plus';
import { storeToRefs } from 'pinia';
import { useRoute } from 'vue-router';
import router from '../router';
import { initBackEndControlRoutes } from '../router/backEnd';
import { initFrontEndControlRoutes } from '../router/frontEnd';
import { useUserInfo } from '../stores/userInfo';
import { NextLoading } from '../utils/loading';
import { accessSessionKey, clearAccessTokens } from '../utils/request';
import { Local } from '../utils/storage';
import profileMan from '/@/assets/profile/man.svg';
import { useThemeConfig } from '../stores/themeConfig';
import { formatAxis } from '/@/utils/formatTime';
import { nextTick } from 'vue';
 
const getQueryParams = (params: any) => {
    const paramsObj = JSON.parse(params) ?? {};
    const wechatKeys = ['isWxLogin', 'wxcode', 'wxstate'];
 
    const queryParams = {};
    for (const key in paramsObj) {
        if (Object.prototype.hasOwnProperty.call(paramsObj, key)) {
            const element = paramsObj[key];
            if (!wechatKeys.includes(key)) {
                queryParams[key] = element;
            }
        }
    }
    return queryParams;
};
export const useLogin = () => {
    const storesThemeConfig = useThemeConfig();
    const { themeConfig } = storeToRefs(storesThemeConfig);
    // 登录成功后的跳转
    const signInSuccess = (isNoPower: boolean | undefined, currentTime) => {
        if (isNoPower) {
            ElMessage.warning('您没有登录权限');
            clearAccessTokens();
        } else {
            // 初始化登录成功时间问候语
            const currentTimeInfo = currentTime;
            // 登录成功,跳到转首页
            // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
            // console.log(route.query, 199);
            const currentRoute = router.currentRoute.value;
 
            if (currentRoute.query?.redirect) {
                router.push({
                    path: <string>currentRoute.query?.redirect,
                    query: Object.keys(<string>currentRoute.query?.params).length > 0 ? getQueryParams(<string>currentRoute.query?.params) : '',
                });
                setTimeout(() => {
                    nextTick(() => {
                        NextLoading.done();
                    });
                }, 4000);
            } else {
                router.push('/');
            }
            // 登录成功提示
            const signInText = '欢迎回来!';
            ElMessage.success(`${currentTimeInfo},${signInText}`);
            // 添加 loading,防止第一次进入界面时出现短暂空白
            // NextLoading.start();
        }
        // state.loading.signIn = false;
    };
    const handleAfterLogin = async (res) => {
        const currentTime = formatAxis(new Date());
        Local.set(accessSessionKey, res.hswatersession);
        await useUserInfo().setUserInfos({
            ...(res ?? {}),
            photo: profileMan,
        }); //缓存用户信息
        // state.loading.signIn = true;
        if (!themeConfig.value.isRequestRoutes) {
            // 前端控制路由,2、请注意执行顺序
            const isNoPower = await initFrontEndControlRoutes();
            // console.log(isNoPower,172)
            signInSuccess(isNoPower, currentTime);
        } else {
            // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
            // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
            const isNoPower = await initBackEndControlRoutes();
            // 执行完 initBackEndControlRoutes,再执行 signInSuccess
            // console.log(isNoPower,178)
            signInSuccess(isNoPower, currentTime);
        }
    };
 
    return {
        handleAfterLogin,
    };
};