Prechádzať zdrojové kódy

fix:修复微信登录api

jinx 3 rokov pred
rodič
commit
d71009f044

+ 91 - 37
component/auth/auth.js

@@ -1,7 +1,9 @@
 // component/auth/auth.js
 const api = require('../../config/api')
 const util = require('../../utils/util.js');
-import {getSubOpen} from './api'
+import {
+  getSubOpen
+} from './api'
 
 Component({
 
@@ -12,17 +14,23 @@ Component({
     show: false,
     canIUse: wx.canIUse('button.open-type.getUserInfo')
   },
-  
+
   attached() {
     this.callback = () => {
-      this.setData({show: !getApp().globalData.loginProps.isLogin})
+      this.setData({
+        show: !getApp().globalData.loginProps.isLogin
+      })
 
       let pages = getCurrentPages()
       let currPage = pages[pages.length - 1].route
       if (currPage === 'pages/discover/discover') {
-        this.setData({bottom: '50px'})
+        this.setData({
+          bottom: '50px'
+        })
       } else {
-        this.setData({bottom: 0})
+        this.setData({
+          bottom: 0
+        })
       }
     }
     this.callback()
@@ -34,13 +42,14 @@ Component({
     getApp().removeLoginListener(this.callback)
   },
   methods: {
-    quitHandle: function() {
+    quitHandle: function () {
       getApp().setLoginProps(true)
     },
-    getCode: function() {
+    getCode: function () {
       return new Promise(r => {
         wx.login({
-          success: function(res) {
+          success: function (res) {
+            console.log(res)
             if (res.code) {
               r(res.code)
             }
@@ -51,35 +60,80 @@ Component({
     authLogin() {
       this.triggerEvent('login')
     },
-    bindGetUserInfo: async function(e) {
-      let code = await this.getCode()
-      //登录远程服务器
-      util.request(api.AuthLoginByWeixin, {
-        code: code,
-        userInfo: e.detail
-      }, 'POST', 'application/json').then(res => {
-        if (res.errno === 0) {
-          //存储用户信息
-          res.data.userInfo.userId = res.data.userId
-          res.data.userInfo.sessionKey = res.data.sessionKey
-          console.log('-------', res.data.userInfo)
-          wx.setStorageSync('userInfo', res.data.userInfo);
-          wx.setStorageSync('token', res.data.token);
-          wx.setStorageSync('userId', res.data.userId);
-          wx.setStorageSync('isLogin', true);
-          getApp().setLoginProps(true)
-          this.authLogin()
-          getSubOpen()
-          
-        } else {
-          // util.showErrorToast(res.errmsg)
-          // wx.showModal({
-          //   title: '提示',
-          //   content: res.errmsg,
-          //   showCancel: false
-          // });
+    // bindGetUserInfo: async function (e) {
+    //   let code = await this.getCode()
+    //   console.log('******')
+    //   console.log(e.detail)
+    //   //登录远程服务器
+    //   util.request(api.AuthLoginByWeixin, {
+    //     code: code,
+    //     userInfo: e.detail
+    //   }, 'POST', 'application/json').then(res => {
+    //     if (res.errno === 0) {
+    //       console.log('&&&&&&&')
+    //       console.log(res.data)
+    //       //存储用户信息
+    //       res.data.userInfo.userId = res.data.userId
+    //       res.data.userInfo.sessionKey = res.data.sessionKey
+    //       console.log('-------', res.data.userInfo)
+    //       wx.setStorageSync('userInfo', res.data.userInfo);
+    //       wx.setStorageSync('token', res.data.token);
+    //       wx.setStorageSync('userId', res.data.userId);
+    //       wx.setStorageSync('isLogin', true);
+    //       getApp().setLoginProps(true)
+    //       this.authLogin()
+    //       getSubOpen()
+
+    //     } else {
+    //       // util.showErrorToast(res.errmsg)
+    //       // wx.showModal({
+    //       //   title: '提示',
+    //       //   content: res.errmsg,
+    //       //   showCancel: false
+    //       // });
+    //     }
+    //   });
+    // },
+    getUserProfile: function (e) {
+      console.log(e)
+      wx.getUserProfile({
+        desc: '用于完善用户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
+        success: async  (userInfo_res) => {
+          console.log(userInfo_res.userInfo)
+          let info = {userInfo:userInfo_res.userInfo}
+          let code = await this.getCode()
+
+          //登录远程服务器
+          util.request(api.AuthLoginByWeixin, {
+            code: code,
+            userInfo: info
+          }, 'POST', 'application/json').then(res => {
+            if (res.errno === 0) {
+              console.log('&&&&&&&')
+              console.log(res.data)
+              //存储用户信息
+              res.data.userInfo.userId = res.data.userId
+              res.data.userInfo.sessionKey = res.data.sessionKey
+              console.log('-------', res.data.userInfo)
+              wx.setStorageSync('userInfo', res.data.userInfo);
+              wx.setStorageSync('token', res.data.token);
+              wx.setStorageSync('userId', res.data.userId);
+              wx.setStorageSync('isLogin', true);
+              getApp().setLoginProps(true)
+              this.authLogin()
+              getSubOpen()
+
+            } else {
+              // util.showErrorToast(res.errmsg)
+              // wx.showModal({
+              //   title: '提示',
+              //   content: res.errmsg,
+              //   showCancel: false
+              // });
+            }
+          });
         }
-      });
-    },
+      })
+    }
   }
 })

+ 2 - 1
component/auth/auth.wxml

@@ -15,6 +15,7 @@
         </cover-view>
       </cover-view>
 
-      <button class="button" wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">允许使用</button>
+      <button class="button"  bindtap="getUserProfile">允许使用</button>
+      <!-- <button class="button" wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">允许使用</button> -->
     </cover-view>
 </view>

+ 2 - 2
config.js

@@ -18,5 +18,5 @@ const mal = {
 }
 
 
-// export default dev
-export default mal
+export default dev
+// export default mal

+ 1 - 1
pages/ucenter/index/index.wxml

@@ -5,7 +5,7 @@
   <view class="userinfo">
     <image src="../../../static/images/ucenterbg.png" class="user-bg"></image>
     <view class="u-avatar"  bindtap="tapByIdx" data-url="/pages/user/user">
-      <button class="btn-cls" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo"></button>
+      <!-- <button class="btn-cls" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo"></button> -->
       <image  class="userinfo-avatar" src="{{ userInfo.avatarUrl }}" background-size="cover"></image>
       <text class="userinfo-nickname">{{userInfo.nickName}}</text>
     </view>

+ 1 - 1
project.config.json

@@ -39,7 +39,7 @@
         "minifyWXML": true
     },
     "compileType": "miniprogram",
-    "libVersion": "2.6.6",
+    "libVersion": "2.20.2",
     "appid": "wxf1eb182777211ed7",
     "projectname": "FDshop",
     "simulatorType": "wechat",