Browse Source

兑换,购买商品按钮点击之前,添加授权验证处理,统一在授权登录也先后授权用户信息、获取手机号

wangxin 6 years ago
parent
commit
eff1936574
7 changed files with 78 additions and 40 deletions
  1. 20 0
      app.js
  2. 9 17
      template/home/index.js
  3. 1 1
      template/home/index.wxml
  4. 38 4
      template/login/index.js
  5. 2 1
      template/login/index.wxml
  6. 7 16
      template/view/index.js
  7. 1 1
      template/view/index.wxml

+ 20 - 0
app.js

@@ -25,6 +25,22 @@ App({
     return dever.getSign() == '' ? false : true;
   },
 
+  /**
+   * 判断是否授权
+   */
+  isAuth: function(scope, fun){
+    wx.getSetting({
+      withCredentials: true,
+      success(res) {
+        if (res.authSetting[scope]) {
+          fun(true);
+        } else {
+          fun(false)
+        }
+      }
+    })
+  },
+
   /**
    * 获取用户id
    */
@@ -32,6 +48,10 @@ App({
     return dever.getUid();
   },
 
+  getLoginInfo: function(){
+    return dever.getLoginInfo();
+  },
+
   /**
     * 设置模板变量[单个]
     */

+ 9 - 17
template/home/index.js

@@ -77,27 +77,19 @@ Page({
     app.location('view/index?id=' + id);
   },
 
-  /**
-    * 微信授权回调, 用于礼品卡兑换按钮
-    */
-  bindGetUserInfo: function (e) {
-    if (e.detail.userInfo) {
-      this.cash();
-    } else {
-      //TODO 用户拒绝了授权
-    }
-  },
-
   /**
    * 礼品卡兑换
    */
   cash: function(){
-    if (app.isLogin()) {
-      this.cashdialog.setErrMsg('');
-      this.cashdialog.showDialog();
-    } else {
-      app.location('login/index');
-    }
+    var that = this;
+    app.isAuth('scope.userInfo', function(res){
+      if(res){
+        that.cashdialog.setErrMsg('');
+        that.cashdialog.showDialog();
+      }else{
+        app.location('login/index');
+      }
+    });
   },
 
   /**

+ 1 - 1
template/home/index.wxml

@@ -11,7 +11,7 @@
     </view>
     <view class='cash'>
       <button class='nxh__button nxh__button3' open-type="contact">在线客服</button>
-      <button class='nxh__button nxh__button3' open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" lang="zh_CN">礼品卡兑换</button>
+      <button class='nxh__button nxh__button3' bindtap='cash'>礼品卡兑换</button>
     </view>
 </view>
 

+ 38 - 4
template/login/index.js

@@ -4,11 +4,12 @@ Page({
   
   data: {
     //判断小程序的API,回调,参数,组件等是否在当前版本可用。
-    canIUse: wx.canIUse('button.open-type.getUserInfo')
+    canIUse: wx.canIUse('button.open-type.getUserInfo'),
+    scope: 'userinfo'
   },
 
   /**
-   * 微信授权回调
+   * 微信授权回调(用户信息)
    */
   bindGetUserInfo: function (e) {
     if (e.detail.userInfo) {
@@ -18,6 +19,17 @@ Page({
     }
   },
 
+  /**
+   * 微信授权回调(手机号)
+   */
+  getPhoneNumber: function (e) {
+    if (e.detail.encryptedData){
+      this.bindMobile(e.detail);
+    } else {
+      //TODO 用户拒绝了授权
+    }
+  },
+
   /**
    * 查看是否授权,须在登录后调用
    */
@@ -51,7 +63,9 @@ Page({
           },{
             success: function (data, res) {
               that.checkAuth();
-              app.goBack();
+              that.setData({
+                scope: 'phonenumber'
+              });
             }
           })
         }
@@ -78,7 +92,27 @@ Page({
         success: function(data, res){
           //TODO 更新成功~
         }
-    }, 'POST');
+    });
+  },
+
+  /**
+   * 绑定手机号
+   */
+  bindMobile: function(wxObj){
+    var that = this;
+    var loginInfo = app.getLoginInfo();
+    app.request('passport.applet.mobile', {
+      iv: wxObj.iv,
+      encryptedData: wxObj.encryptedData,
+      vid: loginInfo.vid,
+      uid: loginInfo.uid,
+      signature: loginInfo.signature
+    }, {
+        success: function (data, res) {
+          //TODO 更新成功~
+          app.goBack();
+        }
+      });
   }
 
 })

+ 2 - 1
template/login/index.wxml

@@ -1,5 +1,6 @@
 <view wx:if="{{canIUse}}">
   <image src='/static/img/logo.png'></image>
-  <button class="nxh__button" open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">授权登录</button>
+  <button class="nxh__button" open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo" wx:if="{{scope=='userinfo'}}">授权登录</button>
+  <button class="nxh__button" open-type="getPhoneNumber" lang="zh_CN" bindgetphonenumber="getPhoneNumber" wx:elif="{{scope=='phonenumber'}}">获取手机号</button> 
 </view>
 <view wx:else>请升级微信版本</view>

+ 7 - 16
template/view/index.js

@@ -27,31 +27,22 @@ Page({
     }
   },
 
-  /**
-     * 微信授权回调
-     */
-  bindGetUserInfo: function (e) {
-    if (e.detail.userInfo) {
-      this.doBuy();
-    } else {
-      //TODO 用户拒绝了授权
-    }
-  },
-
   /**
    * 立即购买
    */
   doBuy: function(e){
     var that = this;
-    if(app.isLogin()){
-      app.request('product.api.addCarts', {product_id: this.pid, num: 1}, {
+    app.isAuth('scope.userInfo', function (res) {
+      if (res) {
+        app.request('product.api.addCarts', { product_id: that.pid, num: 1 }, {
           success: function (data, res) {
             app.location('carts/index?id=' + that.pid);
           }
         })
-    } else {
-      app.location('login/index');
-    }
+      } else {
+        app.location('login/index');
+      }
+    });
   },
 
   /**

+ 1 - 1
template/view/index.wxml

@@ -7,5 +7,5 @@
 </view>
 <view class='bottom'>
   <button class='nxh__button' bindtap='toShare'>分享给好友</button>
-  <button class='nxh__button' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">立即购买</button>
+  <button class='nxh__button' bindtap='doBuy'>立即购买</button>
 </view>