瀏覽代碼

fix(前端): 修复微信登录 invalid code (40029) —— 不再 onLoad 预取 login code,改为点击授权时实时获取

微信 code 一次性有效(5分钟且用过即废),onLoad 预取后复用旧 code
会导致 code2Session 返回 40029 invalid code。login.vue 改为与
invite/join.vue 一致的模式:getPhoneNumber 中实时 uni.login 获取新 code。
Xiaogang Liao 1 月之前
父節點
當前提交
814ee2b986
共有 1 個文件被更改,包括 36 次插入31 次删除
  1. 36 31
      cfc-frontend/pages/login/login.vue

+ 36 - 31
cfc-frontend/pages/login/login.vue

@@ -67,28 +67,23 @@ export default {
       agreed: false,
       nickname: '',
       avatar: '',
-      redirectUrl: '',
-      loginCode: ''
-    }
-  },
-  onLoad(options) {
-    // 处理邀请码
-    if (options.inviteCode) {
-      uni.setStorageSync('inviteCode', options.inviteCode)
-      uni.setStorageSync('inviteType', options.inviteType || 'family')
-    }
-    // 存储登录后跳转地址
-    if (options.redirect) {
-      this.redirectUrl = decodeURIComponent(options.redirect)
-    }
-    // 预获取微信登录code(有效期5分钟,用户操作通常在时限内)
-    var self = this
-    uni.login({
-      provider: 'weixin',
-      success: function(res) { self.loginCode = res.code },
-      fail: function() {}
-    })
+      redirectUrl: ''
+    },
   },
+  onLoad(options) {
+      // 处理邀请码
+      if (options.inviteCode) {
+        uni.setStorageSync('inviteCode', options.inviteCode)
+        uni.setStorageSync('inviteType', options.inviteType || 'family')
+      }
+      // 存储登录后跳转地址
+      if (options.redirect) {
+        this.redirectUrl = decodeURIComponent(options.redirect)
+      }
+      // 注意:不在 onLoad 预取 login code —— 微信 code 一次性有效(5分钟且用过即废),
+      // 预取后如果用户隔几分钟再点击授权,code 已失效会报 invalid code (40029)。
+      // 正确做法:getPhoneNumber 中用户点击授权时实时获取。
+    },
   methods: {
     handleImageError(e) {
       console.log('Logo 加载失败,使用默认显示')
@@ -121,16 +116,26 @@ export default {
       }
       var self = this
       self.loading = true
-      if (!self.loginCode) {
-        uni.showToast({ title: '网络异常,请重试', icon: 'none' })
-        self.loading = false
-        return
-      }
-      wechatPhoneLogin({ code: self.loginCode, phoneCode: e.detail.code }).then(function(res) {
-        self.completeLogin(res.data)
-      }).catch(function(err) {
-        uni.showToast({ title: err.message || '登录失败', icon: 'none' })
-        self.loading = false
+      // 点击授权时实时获取 login code(微信 code 一次性有效,不能复用旧 code)
+      uni.login({
+        provider: 'weixin',
+        success: function(loginRes) {
+          if (!loginRes.code) {
+            uni.showToast({ title: '网络异常,请重试', icon: 'none' })
+            self.loading = false
+            return
+          }
+          wechatPhoneLogin({ code: loginRes.code, phoneCode: e.detail.code }).then(function(res) {
+            self.completeLogin(res.data)
+          }).catch(function(err) {
+            uni.showToast({ title: err.message || '登录失败', icon: 'none' })
+            self.loading = false
+          })
+        },
+        fail: function() {
+          uni.showToast({ title: '网络异常,请重试', icon: 'none' })
+          self.loading = false
+        }
       })
     },
     completeLogin: function(data) {