|
|
@@ -93,10 +93,9 @@ const router = new VueRouter({
|
|
|
]
|
|
|
});
|
|
|
// 全局前置守卫:只对需要登录的页面进行登录校验
|
|
|
-router.beforeEach((to, from, next) => {
|
|
|
+router.beforeEach(async (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;
|
|
|
@@ -104,52 +103,15 @@ router.beforeEach((to, from, next) => {
|
|
|
try {
|
|
|
//先判断企微环境
|
|
|
checkWxWorkEnvAndUserCache();
|
|
|
- // 获取并处理 loginType,区分登录方式
|
|
|
const { loginType, wxid, WecomType } = to.query;
|
|
|
- const isNewLoginMode = loginType && String(loginType) === '2';
|
|
|
- // 保证本次会话的登录方式不变
|
|
|
- if(isNewLoginMode){
|
|
|
- sessionStorage.setItem("isNewLoginMode",String(isNewLoginMode));
|
|
|
- }
|
|
|
- if (sessionStorage.getItem("isNewLoginMode") === 'true') {
|
|
|
- window.localStorage.setItem('loginMode', 'wxidLogin');
|
|
|
- if (WecomType) {
|
|
|
- window.localStorage.setItem('agentFrom', getAgentFrom(WecomType));
|
|
|
- window.localStorage.setItem('agentFromAI', getAgentFrom(WecomType));
|
|
|
- }
|
|
|
- //验证时间戳和token
|
|
|
- initGuidInfo();
|
|
|
- const isLoggedIn = checkLoginStatus();
|
|
|
- if (isLoggedIn) {
|
|
|
- // debugger
|
|
|
- next();
|
|
|
- return;
|
|
|
- }
|
|
|
- if (wxid) {
|
|
|
- // 新登录方式:loginType存在且等于2
|
|
|
- console.log('-----当前为【新登录方式】,执行强制登录校验');
|
|
|
- handleNewLoginMode(to, next, wxid);
|
|
|
- } else {
|
|
|
- sessionStorage.setItem("errorMsgTit", '参数缺失,无权限');
|
|
|
- next({ path: "/error" });
|
|
|
- }
|
|
|
+ // 初始化登录模式标识
|
|
|
+ initLoginModeFlag(loginType);
|
|
|
+ // 判断当前登录模式,分发处理逻辑
|
|
|
+ const isNewLoginMode = sessionStorage.getItem('isNewLoginMode') === 'true';
|
|
|
+ if (isNewLoginMode) {
|
|
|
+ await handleWxidLoginMode(to, next, wxid, WecomType);
|
|
|
} else {
|
|
|
- const org_loginMode = window.localStorage.getItem('loginMode');
|
|
|
- if (org_loginMode != 'codeLogin') {
|
|
|
- window.localStorage.clear();
|
|
|
- }
|
|
|
- // 旧登录方式:无loginType / loginType≠2
|
|
|
- window.localStorage.setItem('loginMode', 'codeLogin');
|
|
|
- //验证时间戳和token
|
|
|
- initGuidInfo();
|
|
|
- const isLoggedIn = checkLoginStatus();
|
|
|
- if (isLoggedIn) {
|
|
|
- // debugger
|
|
|
- next();
|
|
|
- return;
|
|
|
- }
|
|
|
- console.log('🔑 当前为【旧登录方式】,执行原有企微登录逻辑');
|
|
|
- handleOldWecomLoginMode(to, next);
|
|
|
+ await handleCodeLoginMode(to, next);
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error("登录校验过程异常:", error);
|
|
|
@@ -163,10 +125,19 @@ router.beforeEach((to, from, next) => {
|
|
|
* @param to 目标路由
|
|
|
* @param next 路由放行方法
|
|
|
*/
|
|
|
-function handleNewLoginMode(to, next, wxId) {
|
|
|
+async function handleWxidLoginMode(to, next, wxId, WecomType) {
|
|
|
try {
|
|
|
- // 【核心】新登录方式的登录校验+获取Token逻辑
|
|
|
- // 替换为你的「新登录接口」,Promise异步处理
|
|
|
+ window.localStorage.setItem('loginMode', 'wxidLogin');
|
|
|
+ if (WecomType) {
|
|
|
+ const agentFrom = getAgentFrom(WecomType);
|
|
|
+ window.localStorage.setItem('agentFrom', agentFrom);
|
|
|
+ window.localStorage.setItem('agentFromAI', agentFrom);
|
|
|
+ }
|
|
|
+ // 3. 初始化鉴权信息,校验登录状态
|
|
|
+ initGuidInfo();
|
|
|
+ if (checkLoginStatus()) return next();
|
|
|
+ // 4. wxid参数校验
|
|
|
+ if (!wxId) return redirectToError(next);
|
|
|
const formData = new FormData();
|
|
|
const timestamp = new Date().getTime().toString();
|
|
|
const nonce = getGuid();
|
|
|
@@ -178,7 +149,7 @@ function handleNewLoginMode(to, next, wxId) {
|
|
|
doWxidLogin(formData)
|
|
|
.then((res) => {
|
|
|
if (res && res.StatusCode === 200 && res.Data && res.Data.token) {
|
|
|
- console.log("res=", res)
|
|
|
+ // console.log("res=", res)
|
|
|
window.localStorage.setItem('AIToken', res.Data.token);
|
|
|
window.localStorage.setItem('isRefreshProvider', res.Data.isRefreshProvider);
|
|
|
console.log(`新登录成功,Token已缓存,即将进入 ${to.path}`);
|
|
|
@@ -203,7 +174,20 @@ function handleNewLoginMode(to, next, wxId) {
|
|
|
* @param to 目标路由
|
|
|
* @param next 路由放行方法
|
|
|
*/
|
|
|
-function handleOldWecomLoginMode(to, next) {
|
|
|
+function handleCodeLoginMode(to, next) {
|
|
|
+ const org_loginMode = window.localStorage.getItem('loginMode');
|
|
|
+ if (org_loginMode != 'codeLogin') {
|
|
|
+ window.localStorage.clear();
|
|
|
+ }
|
|
|
+ window.localStorage.setItem('loginMode', 'codeLogin');
|
|
|
+ //验证时间戳和token
|
|
|
+ initGuidInfo();
|
|
|
+ const isLoggedIn = checkLoginStatus();
|
|
|
+ if (isLoggedIn) {
|
|
|
+ // debugger
|
|
|
+ next();
|
|
|
+ return;
|
|
|
+ }
|
|
|
const code = to.query.code as string;
|
|
|
let finalCode;
|
|
|
if (Array.isArray(code)) {
|
|
|
@@ -228,4 +212,25 @@ function handleOldWecomLoginMode(to, next) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 初始化登录模式标识 - 保证会话内登录方式统一
|
|
|
+ * @param {string} loginType 路由传参的登录类型
|
|
|
+ */
|
|
|
+function initLoginModeFlag(loginType) {
|
|
|
+ const isNewLoginMode = loginType && String(loginType) === '2';
|
|
|
+ // 保证本次会话的登录方式不变
|
|
|
+ if (isNewLoginMode) {
|
|
|
+ sessionStorage.setItem("isNewLoginMode", 'true');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 跳转错误页并存储错误信息
|
|
|
+ * @param {Function} next 路由放行函数
|
|
|
+ * @param {string} msg 错误提示信息
|
|
|
+ */
|
|
|
+function redirectToError(next, msg = '参数缺失,无权限') {
|
|
|
+ sessionStorage.setItem("errorMsgTit", msg);
|
|
|
+ next({ path: '/error', replace: true });
|
|
|
+}
|
|
|
export default router;
|