ソースを参照

修改登陆逻辑

wyling007 3 年 前
コミット
047247a3a5
1 ファイル変更33 行追加28 行削除
  1. 33 28
      src/route/guard.ts

+ 33 - 28
src/route/guard.ts

@@ -7,40 +7,45 @@ import { userInfo } from "@/api";
 
 const guard = (router: Router) => {
 	router.beforeEach(async (to, from, next) => {
+		//如果是marked页面,不需要任何判断,直接进入
+		if (to.path === "/marked") {
+			next();
+			return;
+		}
+
 		if (true) {
 			console.log(location);
 		}
 
-		//监听到登陆事件
-		if (to.query.state === "LOGIN") {
-			const toast = Toast.loading({
-				duration: 0, // 持续展示 toast
-				forbidClick: true,
-				message: "登陆中",
-			});
-			try {
-				await useLogin(to.query);
-				toast.message = `登陆成功`;
-				setTimeout(() => {
-					Toast.clear();
-				}, 500);
-			} catch (error) {
-				toast.message = `登陆失败`;
-				setTimeout(() => {
-					Toast.clear();
-				}, 1000);
+		//检测token状态,token失效直接跳转至登陆链接
+		try {
+			const authTest = await userInfo();
+			if (authTest.data.code !== 200) {
+				throw authTest.data.msg;
 			}
-		}
-
-		//检测token状态,token为空直接跳转至登陆链接,token存在则请求某个接口验证token有效性
-		if (store.getters.getToken === "") {
-			if (to.path === "/marked") {
-				next();
-				return;
+		} catch (error) {
+			//监听到登陆事件
+			if (to.query.state === "LOGIN") {
+				const toast = Toast.loading({
+					duration: 0, // 持续展示 toast
+					forbidClick: true,
+					message: "登陆中",
+				});
+				try {
+					await useLogin(to.query);
+					toast.message = `登陆成功`;
+					setTimeout(() => {
+						Toast.clear();
+					}, 500);
+				} catch (error) {
+					toast.message = `登陆失败`;
+					setTimeout(() => {
+						Toast.clear();
+					}, 1000);
+				}
+			} else {
+				location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=${location.href}&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
 			}
-			location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=${location.href}&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
-		} else {
-			userInfo();
 		}
 
 		try {