|
|
@@ -2,7 +2,8 @@ import Vue from "vue";
|
|
|
import VueRouter from "vue-router";
|
|
|
import layout from "@/layout/index.vue";
|
|
|
import { checkWxWorkEnvAndUserCache } from '@/utils/index'
|
|
|
-import { checkLoginStatus, initGuidInfo, doWecomLogin, getQyCode } from '@/utils/wecomLogin.ts';
|
|
|
+import { checkLoginStatus, initGuidInfo, doWecomLogin, getQyCode, getGuid, getMD5 } from '@/utils/wecomLogin.ts';
|
|
|
+import { doWxidLogin } from "@/api/indexAI";
|
|
|
Vue.use(VueRouter);
|
|
|
|
|
|
const router = new VueRouter({
|
|
|
@@ -91,40 +92,51 @@ const router = new VueRouter({
|
|
|
},
|
|
|
]
|
|
|
});
|
|
|
+// 会话缓存key:登录方式标记
|
|
|
+const STORAGE_LOGIN_TYPE_KEY = 'loginMode';
|
|
|
+// 登录方式枚举
|
|
|
+const LOGIN_MODE = {
|
|
|
+ NEW: 'wxidLogin', // 新登录方式 (loginType=2)
|
|
|
+ OLD: 'codeLogin' // 旧登录方式 (默认/其他loginType)
|
|
|
+};
|
|
|
+
|
|
|
// 全局前置守卫:只对需要登录的页面进行登录校验
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
+ // console.log("from=",from)
|
|
|
+ console.log("to=", to)
|
|
|
// console.log("-------页面链接=", window.location.href)
|
|
|
if (!to.meta || (to.meta && !to.meta.requiresAuth)) {
|
|
|
next();
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
- checkWxWorkEnvAndUserCache();//先判断企微环境
|
|
|
+ //先判断企微环境
|
|
|
+ checkWxWorkEnvAndUserCache();
|
|
|
+ //验证时间戳和token
|
|
|
initGuidInfo();
|
|
|
const isLoggedIn = checkLoginStatus();
|
|
|
if (isLoggedIn) {
|
|
|
next();
|
|
|
return;
|
|
|
}
|
|
|
- const code = to.query.code as string;
|
|
|
- let finalCode;
|
|
|
- if (Array.isArray(code)) {
|
|
|
- finalCode = code.length > 0 ? code[code.length - 1] : null;
|
|
|
- } else {
|
|
|
- finalCode = code || null;
|
|
|
- }
|
|
|
- if (finalCode) {
|
|
|
- doWecomLogin(finalCode)
|
|
|
- .then(() => {
|
|
|
- next();
|
|
|
- console.log(`登录成功,即将进入 ${to.path}`);
|
|
|
- })
|
|
|
- .catch(() => {
|
|
|
- next({ path: "/error" });
|
|
|
- });
|
|
|
+ // 获取并处理 loginType,区分登录方式
|
|
|
+ const { loginType, wxid } = to.query;
|
|
|
+ const isNewLoginMode = loginType && String(loginType) === '2';
|
|
|
+ if (isNewLoginMode) {
|
|
|
+ if (wxid) {
|
|
|
+ // 新登录方式:loginType存在且等于2
|
|
|
+ sessionStorage.setItem(STORAGE_LOGIN_TYPE_KEY, LOGIN_MODE.NEW);
|
|
|
+ console.log('-----当前为【新登录方式】,执行强制登录校验');
|
|
|
+ handleNewLoginMode(to, next, wxid);
|
|
|
+ } else {
|
|
|
+ sessionStorage.setItem("errorMsgTit", '参数缺失,无权限');
|
|
|
+ next({ path: "/error" });
|
|
|
+ }
|
|
|
} else {
|
|
|
- console.log(`访问 ${to.path} 需登录,正在跳转到授权页面`);
|
|
|
- getQyCode();
|
|
|
+ // 旧登录方式:无loginType / loginType≠2
|
|
|
+ sessionStorage.setItem(STORAGE_LOGIN_TYPE_KEY, LOGIN_MODE.OLD);
|
|
|
+ console.log('🔑 当前为【旧登录方式】,执行原有企微登录逻辑');
|
|
|
+ handleOldWecomLoginMode(to, next);
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error("登录校验过程异常:", error);
|
|
|
@@ -132,4 +144,73 @@ router.beforeEach((to, from, next) => {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+
|
|
|
+/**
|
|
|
+ * 新登录方式处理:强制检测登录 → 获取Token → 缓存 → 放行/失败跳转
|
|
|
+ * @param to 目标路由
|
|
|
+ * @param next 路由放行方法
|
|
|
+ */
|
|
|
+function handleNewLoginMode(to, next, wxId) {
|
|
|
+ try {
|
|
|
+ // 【核心】新登录方式的登录校验+获取Token逻辑
|
|
|
+ // 替换为你的「新登录接口」,Promise异步处理
|
|
|
+ const formData = new FormData();
|
|
|
+ const timestamp = new Date().getTime().toString();
|
|
|
+ const nonce = getGuid();
|
|
|
+ const sign = getMD5(nonce, timestamp, wxId);
|
|
|
+ formData.append('wxId', wxId);
|
|
|
+ formData.append('timestamp', timestamp);
|
|
|
+ formData.append('nonce', nonce);
|
|
|
+ formData.append('sign', sign);
|
|
|
+ doWxidLogin(formData)
|
|
|
+ .then((res) => {
|
|
|
+ if (res && res.StatusCode === 200 && res.Data && res.Data.token) {
|
|
|
+ console.log("res=",res)
|
|
|
+ window.localStorage.setItem('AIToken', res.Data.token);
|
|
|
+ window.localStorage.setItem('isRefreshProvider', res.Data.isRefreshProvider);
|
|
|
+ console.log(`新登录成功,Token已缓存,即将进入 ${to.path}`);
|
|
|
+ // next({ path: to.path, query: {}, replace: true });
|
|
|
+ next();
|
|
|
+ } else {
|
|
|
+ throw new Error('新登录失败,未获取到Token');
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ // 登录失败:跳转错误页
|
|
|
+ next({ path: "/error", replace: true });
|
|
|
+ });
|
|
|
+ } catch (err) {
|
|
|
+ console.error('新登录方式处理异常:', err);
|
|
|
+ next({ path: "/error", replace: true });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 旧登录方式处理:原有企微code授权登录逻辑
|
|
|
+ * @param to 目标路由
|
|
|
+ * @param next 路由放行方法
|
|
|
+ */
|
|
|
+function handleOldWecomLoginMode(to, next) {
|
|
|
+ const code = to.query.code as string;
|
|
|
+ let finalCode;
|
|
|
+ if (Array.isArray(code)) {
|
|
|
+ finalCode = code.length > 0 ? code[code.length - 1] : null;
|
|
|
+ } else {
|
|
|
+ finalCode = code || null;
|
|
|
+ }
|
|
|
+ if (finalCode) {
|
|
|
+ doWecomLogin(finalCode)
|
|
|
+ .then(() => {
|
|
|
+ next();
|
|
|
+ console.log(`登录成功,即将进入 ${to.path}`);
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ next({ path: "/error" });
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ console.log(`访问 ${to.path} 需登录,正在跳转到授权页面`);
|
|
|
+ getQyCode();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export default router;
|