refactor(登录): 优化微信登录流程和初始化逻辑
- 注释掉临时登录代码,避免自动登录 - 将登录接口参数改为可选,提高灵活性 - 新增微信存储初始化逻辑,避免重复初始化 - 在路由守卫中添加微信登录处理,确保登录流程正确执行
This commit is contained in:
parent
51fde85acc
commit
885e1aa8d1
|
@ -16,13 +16,13 @@ export type ConfigDTO = {
|
|||
|
||||
export type LoginByPasswordDTO = {
|
||||
/** 用户名 */
|
||||
username: string;
|
||||
username?: string;
|
||||
/** 密码 */
|
||||
password: string;
|
||||
password?: string;
|
||||
/** 验证码 */
|
||||
captchaCode: string;
|
||||
captchaCode?: string;
|
||||
/** 验证码对应的缓存key */
|
||||
captchaCodeKey: string;
|
||||
captchaCodeKey?: string;
|
||||
/** 企业微信 */
|
||||
corpid: string;
|
||||
code: string;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// import "@/utils/sso";
|
||||
import { getConfig } from "@/config";
|
||||
import NProgress from "@/utils/progress";
|
||||
import { sessionKey } from "@/utils/auth";
|
||||
import { sessionKey, setTokenFromBackend } from "@/utils/auth";
|
||||
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
|
||||
import { usePermissionStoreHook } from "@/store/modules/permission";
|
||||
import {
|
||||
|
@ -26,6 +26,9 @@ import { isUrl, openLink, storageSession, isAllEmpty } from "@pureadmin/utils";
|
|||
|
||||
import remainingRouter from "./modules/remaining";
|
||||
import { TokenDTO } from "@/api/common/login";
|
||||
import { useWxStore, useWxStoreOutside } from "@/store/modules/wx";
|
||||
import * as LoginAPI from "@/api/common/login";
|
||||
import { message } from "@/utils/message";
|
||||
|
||||
/** 自动导入全部静态路由,无需再手动引入!匹配 src/router/modules 目录(任何嵌套级别)中具有 .ts 扩展名的所有文件,除了 remaining.ts 文件
|
||||
* 如何匹配所有文件请看:https://github.com/mrmlnc/fast-glob#basic-syntax
|
||||
|
@ -101,7 +104,7 @@ const whiteList = ["/login"];
|
|||
|
||||
const { VITE_HIDE_HOME } = import.meta.env;
|
||||
|
||||
router.beforeEach((to: ToRouteType, _from, next) => {
|
||||
router.beforeEach(async (to: ToRouteType, _from, next) => {
|
||||
if (to.meta?.keepAlive) {
|
||||
handleAliveRoute(to, "add");
|
||||
// 页面整体刷新和点击标签页刷新
|
||||
|
@ -109,6 +112,30 @@ router.beforeEach((to: ToRouteType, _from, next) => {
|
|||
handleAliveRoute(to);
|
||||
}
|
||||
}
|
||||
|
||||
const wxStore = useWxStoreOutside();
|
||||
if (!wxStore.isInit) {
|
||||
wxStore.initWx();
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
console.log('urlParams', urlParams);
|
||||
const corpid = urlParams.get('corpid') || undefined;
|
||||
const code = urlParams.get('code') || undefined;
|
||||
const state = urlParams.get('state') || undefined;
|
||||
if (code && corpid) {
|
||||
wxStore.handleWxCallback({ corpid, code, state });
|
||||
const { data } = await LoginAPI.loginByPassword({
|
||||
corpid,
|
||||
code,
|
||||
state
|
||||
});
|
||||
// 登录成功后 将token存储到sessionStorage中
|
||||
setTokenFromBackend(data);
|
||||
// 获取后端路由
|
||||
await initRouter();
|
||||
next({ path: getTopMenu(true).path });
|
||||
}
|
||||
}
|
||||
|
||||
const userInfo = storageSession().getItem<TokenDTO>(sessionKey)?.currentUser;
|
||||
NProgress.start();
|
||||
const externalLink = isUrl(to?.name as string);
|
||||
|
|
|
@ -13,12 +13,19 @@ export const useWxStore = defineStore("wx", () => {
|
|||
const state = ref<string>("")
|
||||
// 用户 userid
|
||||
const userid = ref<string>("")
|
||||
// 初始化
|
||||
const isInit = ref<boolean>(false);
|
||||
|
||||
// 设置 userid
|
||||
const setUserid = (id: string) => {
|
||||
userid.value = id
|
||||
}
|
||||
|
||||
const initWx = () => {
|
||||
if (isInit.value) return;
|
||||
isInit.value = true;
|
||||
}
|
||||
|
||||
const handleWxCallback = async (params: { corpid: string; code: string; state: string }) => {
|
||||
console.log('handleWxCallback:', params)
|
||||
if (params.code && params.corpid) {
|
||||
|
@ -42,7 +49,7 @@ export const useWxStore = defineStore("wx", () => {
|
|||
}
|
||||
}
|
||||
|
||||
return { corpid, code, state, userid, setUserid, handleWxCallback }
|
||||
return { corpid, code, state, userid, isInit, setUserid, handleWxCallback, initWx }
|
||||
})
|
||||
|
||||
/**
|
||||
|
|
|
@ -194,9 +194,9 @@ onBeforeMount(async () => {
|
|||
onMounted(() => {
|
||||
window.document.addEventListener("keypress", onkeypress);
|
||||
// 临时登录
|
||||
if (wxStore.code && wxStore.corpid) {
|
||||
/* if (wxStore.code && wxStore.corpid) {
|
||||
onLogin(ruleFormRef.value);
|
||||
}
|
||||
} */
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
|
|
Loading…
Reference in New Issue