浏览代码

fix(学员端): 守卫登录态判定增加storage兜底,修复已登录仍被弹回

liaoxg 1 周之前
父节点
当前提交
c9fa4cc
共有 1 个文件被更改,包括 14 次插入1 次删除
  1. 14 1
      train-frontend/utils/guard.js

+ 14 - 1
train-frontend/utils/guard.js

@@ -5,7 +5,20 @@
  * 未登录触发 requireLogin() 时:toast 提示 + 跳回首页 tab + 触发登录浮层(showLoginSheet)。
  */
 export function requireLogin() {
-  if (getApp() && getApp().$store && getApp().$store.getters.isLoggedIn) return true
+  var loggedIn = false
+  try {
+    if (getApp() && getApp().$store && getApp().$store.getters && getApp().$store.getters.isLoggedIn) {
+      loggedIn = true
+    }
+  } catch (e) {
+    console.error('[guard] store 判定异常,改用 storage', e)
+  }
+  // 兜底:mp-weixin 下 getApp().$store 可能不可达,此时直读 storage token。
+  // storage token 是登录态唯一可信载体(setToken 总是同步写入,401 清理时同步移除)
+  if (!loggedIn && uni.getStorageSync('token')) {
+    loggedIn = true
+  }
+  if (loggedIn) return true
   uni.showToast({ title: '请先登录', icon: 'none' })
   setTimeout(function() {
     uni.switchTab({ url: '/pages/index/index' })