Pārlūkot izejas kodu

首次进入首页时自动跳转登陆页

zuy 7 mēneši atpakaļ
vecāks
revīzija
4adda22aaa
2 mainītis faili ar 27 papildinājumiem un 11 dzēšanām
  1. 17 6
      src/pages/index/index.vue
  2. 10 5
      src/utils/auth.ts

+ 17 - 6
src/pages/index/index.vue

@@ -36,7 +36,7 @@
       <template v-if="isLogin">
         <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
       </template>
-      <uv-empty v-else mode="order" text="请先登录" :marginTop="100"></uv-empty>
+      <uv-empty v-else mode="order" text="请先登录" :marginTop="100" @click="gotoLogin"></uv-empty>
     </view>
 <!--
     <movable-area class="mov-area">
@@ -182,6 +182,7 @@ onShow(() => {
         isLogin.value = true;
       }).catch(e => {
         console.error(e)
+        gotoLogin()
       })
     }
   }
@@ -192,11 +193,14 @@ onShow(() => {
 
   console.log("show index>>>>", isLogin.value, state.stationList, state.location)
   let locationData = getApp<any>().globalData.location;
-  if (!locationData || !locationData.longitude) {
-    loadCurrentLocation();
-  } else {
-    state.location = locationData;
-    loadWashStationList();
+  if(isLogin.value) {
+    if (!locationData || !locationData.longitude) {
+      loadCurrentLocation();
+    } else {
+      state.location = locationData;
+      loadWashStationList();
+    }
+
   }
 
   loadBannerList();
@@ -251,6 +255,13 @@ const handleBannerClick = (index: number) => {
   }
 }
 
+const gotoLogin = () => {
+  console.log("gotoLogin");
+  uni.navigateTo({
+    url: '/pages-user/login/index'
+  })
+}
+
 const handleMenuClick = (menu: any) => {
   let {auth,name} = menu;
   if(auth){

+ 10 - 5
src/utils/auth.ts

@@ -81,7 +81,9 @@ export function login(e: any): Promise<string> {
                 body(`/user/wxLogin`, data).then(async (res: any) => {
                     let {satoken, userId} = res;
                     if (satoken) {
-                        getApp<any>().globalData.user.id = userId
+                        getApp<any>().globalData.user={
+                            id : userId
+                        }
                         // _resolveTokenQueue(satoken);
                         setToken(satoken);
                         await loadUserInfo();
@@ -139,18 +141,21 @@ export function tryLogin() {
                 body(`/user/wxLogin`, data).then(async (res: any) => {
                     const {satoken, userId} = res;
                     if (satoken) {
-                        getApp<any>().globalData.user.id = userId
+                        getApp<any>().globalData.user={
+                            id: userId
+                        }
                         setToken(satoken);
                         await loadUserInfo();
                         resolve(satoken)
                     } else {
                         console.error("tryLogin")
-                        // _resolveTokenQueue({
-                        //     errMsg:  `${JSON.stringify(res)}`,
-                        // });
+                        reject('fail')
+                        getApp<any>().globalData.user =null;
                     }
                     uni.hideLoading()
                 }).catch(e => {
+                    console.error("tryLogin",e)
+                    getApp<any>().globalData.user =null;
                     reject(e)
                     uni.hideLoading()
                 })