Browse Source

修改登陆逻辑

wyling 3 years ago
parent
commit
02e86a4080
1 changed files with 21 additions and 3 deletions
  1. 21 3
      src/route/guard.ts

+ 21 - 3
src/route/guard.ts

@@ -3,24 +3,42 @@ import store from "@/store";
 import dayjs from "dayjs";
 import { Toast } from "vant";
 import { useLogin } from "@/hooks";
+import { userInfo } from "@/api";
 
 const guard = (router: Router) => {
   router.beforeEach(async (to, from, next) => {
+    if (true) {
+      console.log(location);
+    }
+
+    //监听到登陆事件
     if (to.query.state === "LOGIN") {
       const toast = Toast.loading({
         duration: 0, // 持续展示 toast
         forbidClick: true,
         message: "登陆中",
       });
-      await useLogin(to.query);
-      toast.message = `登陆成功`;
-      Toast.clear();
+      try {
+        await useLogin(to.query);
+        toast.message = `登陆成功`;
+        setTimeout(() => {
+          Toast.clear();
+        }, 500);
+      } catch (error) {
+        toast.message = `登陆失败`;
+        setTimeout(() => {
+          Toast.clear();
+        }, 1000);
+      }
     }
 
+    //检测token状态,token为空直接跳转至登陆链接,token存在则请求某个接口验证token有效性
     if (store.getters.getToken === "") {
       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();
     }
 
     const userTime = dayjs(store.getters.getUserData.expireTime).valueOf();