Răsfoiți Sursa

fix: 扫码拓客端进入小程序时未登录用户先静默登录,已登录用户直接使用本地数据执行拓客逻辑

- App.vue: 已登录用户直接使用本地 wxLoginIsNew 执行拓客逻辑,避免旧 token 导致 wxLogin 失败
- App.vue: 未登录用户走 wxLogin 静默登录后执行拓客逻辑
- App.vue: 新用户触发 loginSuccess 事件弹窗,老用户记录推广失败
- pages/index/index.vue: onShow 中增加 handleExpandParam 兜底处理,用户从登录页回来后自动处理拓客逻辑
- pages/index/index.vue: getRuleById 增加错误处理和调试日志
- pages/login/login.vue: 登录后同步存储 wxLoginIsNew

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
xuyunhui 2 zile în urmă
părinte
comite
68626d81a5
3 a modificat fișierele cu 149 adăugiri și 61 ștergeri
  1. 91 60
      App.vue
  2. 57 1
      pages/index/index.vue
  3. 1 0
      pages/login/login.vue

+ 91 - 60
App.vue

@@ -10,83 +10,114 @@
         onShow: function () {
             console.log('App Show 小程序显示了+++++++++++++++++++++++')
             let that = this;
+
+            // 检查是否已登录(有 accessToken)
+            const accessToken = uni.getStorageSync('accessToken')
+
+            if (accessToken) {
+                // 已登录,直接使用本地数据执行拓客逻辑,不再调用 wxLogin
+                console.log('++++++++++已登录用户,直接使用本地数据执行拓客逻辑++++++++++')
+                that.handleShareBind()
+                that.handleExpandLogic()
+                return
+            }
+
+            // 未登录,走静默登录
             wx.login({
                 success(re) {
                     that.$api.wxLogin({code: re.code}).then(res => {
                         console.log('++++++++++登录返回了++++++++++++++++', res)
                         uni.setStorageSync('userInfo', res.data.data.userInfo)
                         uni.setStorageSync('accessToken', res.data.data.access_token)
+                        uni.setStorageSync('phoneIsBind', res.data.data.phoneIsBind)
+                        uni.setStorageSync('wxLoginIsNew', res.data.data.isNew)
                         let isNew = res.data.data.isNew
                         uni.removeStorageSync('parent_member_id');
 
+                        that.handleShareBind(res.data.data.userInfo.otherId)
+                        that.handleExpandLogic(isNew)
 
-                        //用户点击用户分享进入小程序,进行绑定操作
-                        let shareUserId = uni.getStorageSync('shareUserId')
-                        if (shareUserId) {
-                            that.$api.scanCode({
-                                openId: res.data.data.userInfo.otherId,
-                                memberId: shareUserId
-                            }).then(res => {
-                                console.log('用户分享用户绑定成功', res)
-                                uni.removeStorageSync('shareUserId');
-                            })
-                        }
+                    }).catch(err => {
+                        console.log('++++++++++App.vue wxLogin 失败++++++++++++++++')
+                        console.log('err.data:', err.data)
+                        console.log('err.statusCode:', err.statusCode)
+                        console.log('err.header:', err.header)
+                    })
+                },
+            })
 
 
-                        //用户扫码拓客端进入小程序,携带的参数,进行绑定操作
-                        let expandParam = uni.getStorageSync('expandParam')
-                        if (expandParam) {
-
-                          if (expandParam.isUse != 1){
-                            console.log('获取到的拓客端参数',expandParam)
-                            if (new Date().getTime() <= expandParam.timestamp){
-                              if (isNew){
-                                //登录成功,且是新用户,触发首页的自定义事件
-                                uni.$emit("loginSuccess")
-                              }else {
-
-                                that.$api.addRecordForAttach({
-                                  ...expandParam,
-                                  isNew:0,
-                                  isBindSuccess:0,
-                                }).then(res=>{
-                                  expandParam.isUse = 1
-                                  uni.setStorageSync('expandParam', expandParam)
-                                  console.log('+++++++++老用户,扫码推广失败记录!++++++++++++++++')
-                                })
-
-                                // uni.removeStorageSync('expandParam');
-                              }
-
-                            }else {
-                              uni.showModal({
-                                title: '温馨提示',
-                                content: ' 当前二维码已过期,请重新扫码',
-                                showCancel:false,
-                                success: function (res) {
-                                  if (res.confirm) {
-                                    console.log('用户点击确定');
-                                  } else if (res.cancel) {
-                                    console.log('用户点击取消');
-                                  }
-                                }
-                              });
-                              expandParam.isUse = 1
-                              uni.setStorageSync('expandParam', expandParam)
-                              // uni.removeStorageSync('expandParam');
-                            }
-                          }
-                        }
+        },
+        methods: {
+            // 处理用户分享绑定
+            handleShareBind(openId) {
+                let shareUserId = uni.getStorageSync('shareUserId')
+                if (!shareUserId) return
 
-                        //用户扫码技师端进入小程序,携带的参数。 暂无操作
-                        let promotionParam = uni.getStorageSync('promotionParam')
+                let userInfo = uni.getStorageSync('userInfo')
+                let targetOpenId = openId || (userInfo && userInfo.otherId)
+                if (!targetOpenId) return
 
+                this.$api.scanCode({
+                    openId: targetOpenId,
+                    memberId: shareUserId
+                }).then(res => {
+                    console.log('用户分享用户绑定成功', res)
+                    uni.removeStorageSync('shareUserId');
+                })
+            },
+            // 处理拓客端扫码逻辑
+            // isNew: 是否新用户(true=新用户弹窗领券,false=老用户记录推广失败)
+            handleExpandLogic(isNew) {
+                let expandParam = uni.getStorageSync('expandParam')
+                if (!expandParam) return
 
-                    })
-                },
-            })
+                // 使用传入的参数或本地缓存
+                let _isNew = isNew !== undefined ? isNew : uni.getStorageSync('wxLoginIsNew')
+
+                if (expandParam.isUse != 1){
+                    console.log('获取到的拓客端参数',expandParam)
+                    if (new Date().getTime() <= expandParam.timestamp){
+                        if (_isNew){
+                            //登录成功,且是新用户,触发首页的自定义事件(弹出新人专享窗口)
+                            uni.$emit("loginSuccess")
+                            // 标记为已使用,避免重复处理
+                            expandParam.isUse = 1
+                            uni.setStorageSync('expandParam', expandParam)
+                        }else {
+
+                            this.$api.addRecordForAttach({
+                                ...expandParam,
+                                isNew:0,
+                                isBindSuccess:0,
+                            }).then(res=>{
+                                expandParam.isUse = 1
+                                uni.setStorageSync('expandParam', expandParam)
+                                console.log('+++++++++老用户,扫码推广失败记录!++++++++++++++++')
+                            })
 
+                            // uni.removeStorageSync('expandParam');
+                        }
 
+                    }else {
+                        uni.showModal({
+                            title: '温馨提示',
+                            content: ' 当前二维码已过期,请重新扫码',
+                            showCancel:false,
+                            success: function (res) {
+                                if (res.confirm) {
+                                    console.log('用户点击确定');
+                                } else if (res.cancel) {
+                                    console.log('用户点击取消');
+                                }
+                            }
+                        });
+                        expandParam.isUse = 1
+                        uni.setStorageSync('expandParam', expandParam)
+                        // uni.removeStorageSync('expandParam');
+                    }
+                }
+            }
         },
         onHide: function () {
             console.log('App Hide')

+ 57 - 1
pages/index/index.vue

@@ -472,6 +472,9 @@ export default {
     } else {
       this.storeInfo = data;
     }
+
+    // 用户主动登录后,处理拓客端扫码逻辑
+    this.handleExpandParam()
   },
   onLoad(e) {
 
@@ -574,6 +577,51 @@ export default {
 
 
   methods: {
+    // 处理拓客端扫码逻辑(用户从登录页回来后调用)
+    handleExpandParam() {
+      let expandParam = uni.getStorageSync('expandParam')
+      console.log('========handleExpandParam expandParam=======', expandParam)
+      if (!expandParam || expandParam.isUse == 1) {
+        console.log('========handleExpandParam 无参数或已使用,直接返回=======')
+        return
+      }
+
+      // 检查是否已登录(有 accessToken)
+      const accessToken = uni.getStorageSync('accessToken')
+      if (!accessToken) {
+        console.log('========handleExpandParam 未登录,直接返回=======')
+        return
+      }
+
+      // 二维码过期检查
+      if (new Date().getTime() > expandParam.timestamp) {
+        uni.showModal({
+          title: '温馨提示',
+          content: '当前二维码已过期,请重新扫码',
+          showCancel: false
+        });
+        expandParam.isUse = 1
+        uni.setStorageSync('expandParam', expandParam)
+        return
+      }
+
+      const isNew = uni.getStorageSync('wxLoginIsNew')
+      console.log('========handleExpandParam isNew=======', isNew)
+      if (isNew) {
+        this.getRuleById()
+      } else {
+        this.$api.addRecordForAttach({
+          ...expandParam,
+          isNew: 0,
+          isBindSuccess: 0,
+        }).then(res => {
+          expandParam.isUse = 1
+          uni.setStorageSync('expandParam', expandParam)
+          console.log('+++++++++老用户,扫码推广失败记录!++++++++++++++++')
+        })
+      }
+    },
+
     myActivity() {
       // this.$api.myActivity({
       //   pageNum: 1,
@@ -710,12 +758,15 @@ export default {
 
     getRuleById() {
       let expandParam = uni.getStorageSync('expandParam')
+      console.log('========getRuleById expandParam=======', expandParam)
+      console.log('========getRuleById ruleId=======', expandParam.ruleId)
       this.$api.getRuleById({
         id: expandParam.ruleId
       }).then(res => {
+        console.log('========getRuleById res=======', res)
         this.rule = res.data.data
 
-        if (this.rule.strategyList.length > 0) {
+        if (this.rule.strategyList && this.rule.strategyList.length > 0) {
           for (const strategy of this.rule.strategyList) {
             if (this.rule.isAllReceived == 1) {
               strategy.select = 1
@@ -724,7 +775,12 @@ export default {
             }
           }
           this.$refs.strategyListPoup.open()
+          console.log('========拓客弹窗已打开=======')
+        } else {
+          console.log('========strategyList为空,不弹窗=======')
         }
+      }).catch(err => {
+        console.log('========getRuleById 请求失败=======', err)
       })
     },
 

+ 1 - 0
pages/login/login.vue

@@ -154,6 +154,7 @@
 							uni.setStorageSync('userInfo',res.data.data.userInfo)
 							uni.setStorageSync('accessToken',res.data.data.access_token)
 							uni.setStorageSync('phoneIsBind',res.data.data.phoneIsBind)
+							uni.setStorageSync('wxLoginIsNew',res.data.data.isNew)
 
 							uni.removeStorageSync('parent_member_id');
 							// if (!res.data.data.phoneIsBind){