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,
|
};
|
};
|