Просмотр исходного кода

修复微信绑定页面问题

shenjj 7 лет назад
Родитель
Сommit
9ad7e448cc

+ 2 - 1
components/mobile/Home.vue

@@ -198,8 +198,9 @@
             let info = localStorage.getItem('USOFTMALLWECHATINFO')
             if (info) {
               this.$store.commit('option/REQUEST_WECHATINFO_STATUS_SUCCESS', JSON.parse(info))
+            } else {
+              this.$store.dispatch('GerWechatInfo', {code: this.$route.query.code})
             }
-            this.$store.dispatch('GerWechatInfo', {code: this.$route.query.code})
           }
         }
       })

+ 3 - 3
middleware/authenticated.js

@@ -2,12 +2,12 @@ export default function ({ isServer, store, req, redirect, route }) {
   // If nuxt generate, pass this middleware
   if (isServer && !req) return
   if (!store.state.option.user.logged) {
-    let ua = req.headers['user-agent'].toLowerCase()
+    let ua = store.state.option.userAgent.toLowerCase()
     if (ua.match(/micromessenger/i) && ua.match(/micromessenger/i)[0] === 'micromessenger' && req.url.indexOf('?code') === -1) {
-      // 判断是浏览器,切不存在code
+      // 判断是微信浏览器,且不存在code
       return redirect(`/mobile/wechat/toggle?url=${route.path}`)
     } else if (ua.match(/micromessenger/i) && ua.match(/micromessenger/i)[0] === 'micromessenger' && req.url.indexOf('?code') > -1) {
-      // 判断是浏览器,切不存在code
+      // 判断是微信浏览器,且存在code
       return redirect(`/mobile/wechat?url=${route.path}`)
     } else {
       return redirect(`/auth/login?returnUrl=${'http://' + req.headers.host + route.fullPath}`)

+ 10 - 3
middleware/wechatInfo.js

@@ -1,10 +1,17 @@
 export default function ({ isServer, store, req, redirect, route }) {
   if (isServer && !req) return
-  if (!store.state.option.user.logged) {
+  // nuxt拦截器 存在一个问题,如果进入到了判断的地方,一定需要做跳转路由相关操作,不然会出现404
+  if (!store.state.option.wechatInfo.data.openid) {
     let ua = store.state.option.userAgent.toLowerCase()
+    // let _info = localStorage.getItem('USOFTMALLWECHATINFO')
     if (ua.match(/micromessenger/i) && ua.match(/micromessenger/i)[0] === 'micromessenger' && req.url.indexOf('?code') === -1) {
-      // 判断是浏览器,切不存在code
-      return redirect(`/mobile/wechat/toggle?url=${route.path}`)
+      // 判断是浏览器,且不存在code
+      console.log('进入拦截器')
+      if (isServer) {
+        return redirect(`/mobile/wechat/toggle?url=${route.path}`)
+      }
+    } else {
+      return true
     }
   }
 }

+ 15 - 2
pages/mobile/wechat/index.vue

@@ -116,10 +116,23 @@
         if (this.telerror === true) {
           return false
         }
+        if (!this.passwordNum || this.passwordNum === '') {
+          this.collectResult = '密码不能为空'
+          this.timeoutCount++
+          return false
+        }
         let openid = this.$store.state.option.wechatInfo.data.openid
         this.$http.post('/wx/bindUser', {userTel: this.telphoneNum, userPwd: this.passwordNum, openId: openid}).then(res => {
-          this.$store.commit('option/REQUEST_WECHATINFO_STATUS_SUCCESS', res.data)
-          localStorage.setItem('USOFTMALLWECHATINFO', JSON.stringify(res.data))
+          if (res.data.success) {
+            this.$store.commit('option/REQUEST_WECHATINFO_STATUS_SUCCESS', res.data)
+            localStorage.setItem('USOFTMALLWECHATINFO', JSON.stringify(res.data))
+          } else {
+            this.collectResult = res.data.message
+            this.timeoutCount++
+          }
+        }, err => {
+          this.collectResult = err.response.data || '绑定失败'
+          this.timeoutCount++
         })
       },
       resgiter() {

+ 1 - 0
pages/mobile/wechat/toggle.vue

@@ -9,6 +9,7 @@
       this.$nextTick(() => {
         // this.$route.query.url
         window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb3274b676737a319&redirect_uri=https://www.usoftmall.com${this.$route.query.url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
+        // window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd29bbca61728b189&redirect_uri=http://6fpbmx.natappfree.cc${this.$route.query.url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
       })
     }
   }