diff --git a/src/api/common/login.ts b/src/api/common/login.ts index 71af851..fa517b6 100644 --- a/src/api/common/login.ts +++ b/src/api/common/login.ts @@ -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; diff --git a/src/router/index.ts b/src/router/index.ts index 9df7082..04c93ed 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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(sessionKey)?.currentUser; NProgress.start(); const externalLink = isUrl(to?.name as string); diff --git a/src/store/modules/wx.ts b/src/store/modules/wx.ts index 7da610f..97762c7 100644 --- a/src/store/modules/wx.ts +++ b/src/store/modules/wx.ts @@ -13,12 +13,19 @@ export const useWxStore = defineStore("wx", () => { const state = ref("") // 用户 userid const userid = ref("") + // 初始化 + const isInit = ref(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 } }) /** diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 51ddb41..f25ddab 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -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(() => {