zhashaonan 6 سال پیش
والد
کامیت
06cd839903
4فایلهای تغییر یافته به همراه88 افزوده شده و 61 حذف شده
  1. 7 1
      config.js
  2. 26 24
      pages/components/auth/auth.js
  3. 23 21
      pages/login/login.js
  4. 32 15
      utils/WxAuthor.js

+ 7 - 1
config.js

@@ -7,13 +7,19 @@ function getReq(data, callback, method) {
   }
   data = Object.assign({}, options, data);
   let session_id = wx.getStorageSync('session_id');
+  if (!session_id && data['op'] != 'ministart') {
+    setTimeout(() => {
+      getReq(data, callback, method)
+    }, 0);
+    return;
+  }
 
   let header = {
     'content-type': 'application/json', // 默认值
     'Cookie': 'MPHPSESSID=' + session_id
   }
 
-  if(method == 'POST') {
+  if (method == 'POST') {
     header = {
       'content-type': 'application/x-www-form-urlencoded', // 数据转换成 query string 
       'Cookie': 'MPHPSESSID=' + session_id

+ 26 - 24
pages/components/auth/auth.js

@@ -1,5 +1,6 @@
 // pages/components/auth/auth.js
 const api = require('../../../config');
+const getReq = require('../../../config').getReq;
 let app = getApp();
 Component({
   /**
@@ -25,30 +26,31 @@ Component({
       if (e.detail.errMsg == "getUserInfo:ok") {
         app.globalData.userInfo = e.detail.userInfo;
         app.globalData.userInfo.nickname = e.detail.userInfo.nickName;
-        let userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
-        wx.request({
-          url: api.host,
-          method: 'GET',
-          data: {
-            user_info: userInfo,
-            act: "login",
-            op: "wxauthen",
-            client_type: "mini"
-          },
-          success: function (res) {
-            wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
-            if (res.statusCode == 200) {
-              self.triggerEvent('getAuth', {
-                userInfo
-              })
-            }
-            else {
-              wx.showToast({
-                icon: 'none',
-                title: '登陆失败',
-                duration: 2000
-              })
-            }
+        let userInfo = app.globalData.userInfo;
+        if (app.globalData.userId.unionid) {
+          userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
+        }
+        else {
+          let { encryptedData, iv, signature } = e.detail
+          userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
+        }
+        let params = {
+          user_info: userInfo,
+          act: "login",
+          op: "wxauthen"
+        }
+        getReq(params, function (res) {
+          if (res.code == 200) {
+            self.triggerEvent('getAuth', {
+              userInfo
+            })
+          }
+          else {
+            wx.showToast({
+              icon: 'none',
+              title: '登陆失败',
+              duration: 2000
+            })
           }
         })
       }

+ 23 - 21
pages/login/login.js

@@ -1,5 +1,6 @@
 // pages/login/login.js
 const api = require('../../config.js');
+const getReq = require('./../../config.js').getReq;
 let app = getApp();
 Page({
 
@@ -66,29 +67,30 @@ Page({
     if (e.detail.errMsg == "getUserInfo:ok") {
       app.globalData.userInfo = e.detail.userInfo;
       app.globalData.userInfo.nickname = e.detail.userInfo.nickName;
-      let userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
-      wx.request({
-        url: api.host,
-        method: 'GET',
-        data: {
+      let userInfo = app.globalData.userInfo;
+      if (app.globalData.userId.unionid) {
+        userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
+      }
+      else {
+        let { encryptedData, iv, signature } = e.detail
+        userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
+      }
+      let params = {
           user_info: userInfo,
           act: "login",
-          op: "wxauthen",
-          client_type: "mini"
-        },
-        success: function (res) {
-          wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
-          if (res.statusCode == 200) {
-            app.globalData.backLogin = true;
-            wx.navigateBack();
-          }
-          else {
-            wx.showToast({
-              icon: 'none',
-              title: '登陆失败',
-              duration: 2000
-            })
-          }
+          op: "wxauthen"
+      }
+      getReq(params, function (res) {
+        if (res.code == 200) {
+          app.globalData.backLogin = true;
+          wx.navigateBack();
+        }
+        else {
+          wx.showToast({
+            icon: 'none',
+            title: '登陆失败',
+            duration: 2000
+          })
         }
       })
     }

+ 32 - 15
utils/WxAuthor.js

@@ -1,6 +1,5 @@
 const getReq = require('../config.js').getReq
 const api = require('../config.js').host
-
 export default class WxAuthor {
   constructor(app) {
     this.target = app
@@ -22,31 +21,50 @@ export default class WxAuthor {
       code
     }
     getReq(params, res => {
-      let { openid, unionid } = res.datas
-      this.setId(openid, unionid)
-      // 获取用户授权设置信息
-      this.getSetting()
+      if (res.code == 200) {
+        let { openid, unionid, HPHPSESSID } = res.datas
+        wx.setStorageSync('session_id', HPHPSESSID);
+        if (openid && unionid) {
+          this.setId(openid, unionid)
+          this.getSetting(false)
+        }
+        else {
+          this.getSetting(true)
+        }
+      }
+      else {
+        return
+      }
     })
   }
 
-  getSetting() {
+  getSetting(flag) {
     wx.getSetting({
       success: res => {
         if (res.authSetting['scope.userInfo']) {
-          this.getUserInfo()
+          this.getUserInfo(flag)
         }
         else {
-          return 
+          return
         }
       }
     })
   }
 
-  getUserInfo() {
+  getUserInfo(flag) {
+    let withCredentials = flag
     wx.getUserInfo({
+      withCredentials,
       success: res => {
         this.setUserInfo(res.userInfo, res.userInfo.nickName)
-        let userInfo = Object.assign({}, this.target.globalData.userId, this.target.globalData.userInfo)
+        let userInfo = this.target.globalData.userInfo
+        if (flag) {
+          let { encryptedData, signature, iv } = res
+          userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
+        }
+        else {
+          userInfo = Object.assign({}, this.target.globalData.userId, userInfo)
+        }
         this.wxauthen(userInfo)
       }
     })
@@ -58,7 +76,6 @@ export default class WxAuthor {
       op: "wxauthen"
     }
     getReq(params, res => {
-      wx.setStorageSync('session_id', res.datas.HPHPSESSID)
       if (res.code == 200) {
         let url = this.getUrl()
         wx.reLaunch({
@@ -67,10 +84,10 @@ export default class WxAuthor {
       }
       else {
         wx.showToast({
-              icon: 'none',
-              title: '登陆失败',
-              duration: 2000
-            })
+          icon: 'none',
+          title: '登陆失败',
+          duration: 2000
+        })
       }
     })
   }