wangxin 6 years ago
parent
commit
1b749cbf44
8 changed files with 163 additions and 105 deletions
  1. 1 1
      app.json
  2. 23 1
      dever/core.js
  3. 11 12
      dever/package/network.js
  4. 35 53
      template/carts/index.js
  5. 2 1
      template/carts/index.wxml
  6. 27 15
      template/home/index.js
  7. 1 1
      template/home/index.wxml
  8. 63 21
      template/login/index.js

+ 1 - 1
app.json

@@ -1,7 +1,7 @@
 {
   "pages": [
-    "template/home/index",
     "template/carts/index",
+    "template/home/index",
     "template/poster/index",
     "template/view/index",
     "template/pay/index",

+ 23 - 1
dever/core.js

@@ -38,6 +38,16 @@ var dever =
       wx.getStorageSync(key)
     }
 
+    //保存登录信息
+    ,saveLoginInfo: function(value){
+      this.save('applet.bind', value);
+    }
+
+    //获取登录信息
+    ,getLoginInfo: function(){
+      return this.getSave('applet.bind');
+    }
+
     //设置服务器传过来的公共值
     ,setSetting : function(value)
     {
@@ -117,7 +127,7 @@ var dever =
 
     //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
     ,goBack: function(delta){
-      if(typeof delta == 'undefined') delta = 1;
+      if(typeof delta != 'number') delta = 1;
       wx.navigateBack({
         delta: delta
       })
@@ -149,6 +159,18 @@ var dever =
     {
       this.package('carts').load(self, save, path);
     }
+
+    //检查用户是否登录状态,暂支持微信登录
+    ,isLogin: function(fun){
+      wx.checkSession({
+        success: function(res){
+          fun(true);
+        },
+        fail: function(res){
+          fun(false);
+        }
+      })    
+    }
 }
 dever.init();
 module.exports = {

+ 11 - 12
dever/package/network.js

@@ -20,21 +20,20 @@ function request(dever, url, param, callback, method, header)
         method: method.toUpperCase(), 
         header: header,
         success: function (res) {
-            if (res.data.status == 1) {
-                if (res.data.data.config) {
-                  dever.setSetting(res.data.data.config);
-                }
-                dever.stopPullDown();
-                callback.success(res.data.data, res);
-            } else {
-                dever.alert(res.msg);
-            }
+          if (res.data.status == 1) {
+              if (res.data.data.config) {
+                dever.setSetting(res.data.data.config);
+              }
+              callback.success(res.data.data, res);
+          } else {
+              callback.fail(res);
+          }
         },
-        fail: function () {
-            //callback.fail();
+        fail: function (res) {
+            callback.fail(res);
         },
         complete: function () {
-            //callback.complete();
+          dever.stopPullDown();
         }
     })
 }

+ 35 - 53
template/carts/index.js

@@ -1,17 +1,15 @@
 // template/carts/index.js
 Page({
   dever: getApp().dever,
-  /**
-   * 页面的初始数据
-   */
+  addrObj: null,
   data: {
-  
+    
   },
 
-  /**
-   * 生命周期函数--监听页面加载
-   */
   onLoad: function (options) {
+    // 初始化购物车
+    //self.dever.carts(self, 'carts.save', 'carts/index');
+
     //test - begin
     var list = new Array();
     for(var i=1; i<10; i++){
@@ -31,7 +29,7 @@ Page({
   delOrder: function(e){
     var that = this;
     wx.showModal({
-      content: '确删除该订单吗?',
+      content: '确删除该订单吗?',
       success: function(res){
         if (res.confirm){
           that.dever.alert('订单删除成功!');
@@ -40,41 +38,6 @@ Page({
     })
   },
 
-  /**
-   * 生命周期函数--监听页面初次渲染完成
-   */
-  onReady: function () {
-  
-  },
-
-  /**
-   * 生命周期函数--监听页面显示
-   */
-  onShow: function () {
-  
-  },
-
-  /**
-   * 生命周期函数--监听页面隐藏
-   */
-  onHide: function () {
-  
-  },
-
-  /**
-   * 生命周期函数--监听页面卸载
-   */
-  onUnload: function () {
-  
-  },
-
-  /**
-   * 页面相关事件处理函数--监听用户下拉动作
-   */
-  onPullDownRefresh: function () {
-  
-  },
-
   /**
    * 页面上拉触底事件的处理函数
    */
@@ -83,16 +46,35 @@ Page({
   },
 
   /**
-   * 用户点击右上角分享
-   */
-  onShareAppMessage: function () {
-  
-  },
-
-  /**
-   * 调用微信地址
+   * 使用微信地址
    */
-  useWxAddress: function(){
-
+  chooseAddress: function(event){
+    var that = this;
+    var success = function(){
+      wx.chooseAddress({
+        success: function (res) {
+          that.dever.log('wx.chooseAddress', res);
+          that.addrObj = res;
+          that.sets(that, res);
+        },
+        fail: function (res) {
+          that.dever.log('wx.chooseAddress', 'fail');
+        }
+      })
+    }
+    wx.getSetting({
+      success: function(res){
+        if (res.authSetting['scope.address']){
+          success();
+        }else{
+          wx.authorize({
+            scope: 'scope.address',
+            success: function () {
+              success();
+            }
+          })
+        }
+      }
+    })
   }
 })

+ 2 - 1
template/carts/index.wxml

@@ -1,7 +1,8 @@
 <view class='top'>
   <view class='address'>
+    <view>{{address}}</view>
     <text class='tips'>填写收货地址</text>
-    <button class='nxh__button' bindtap='useWxAddress'>使用微信地址</button>
+    <button class='nxh__button' bindtap='chooseAddress' >使用微信地址</button>
   </view>
   <view class='order' wx:for='{{list}}' wx:key='{{index}}' bindlongtap='delOrder'>
     <text class='title'>{{item.name}}</text>

+ 27 - 15
template/home/index.js

@@ -4,18 +4,16 @@ Page({
       test : 1
   },
   onLoad: function (options) {
-    // 初始化购物车
-    //self.dever.carts(self, 'carts.save', 'carts/index');
     var that = this;
-    var callback = {
-      success: function(data, res){
-        that.dever.setList(that, data.product);
-        that.dever.stopPullDown();
-      }
-    }
+    var sign = that.dever.getLoginInfo() == null ? that.dever.getLoginInfo().signature : '';
     that.dever.request('product.api.home', {
-      signature:''
-    },callback);
+      signature: sign
+    },{
+        success: function (data, res) {
+          that.dever.setList(that, data.product);
+          that.dever.stopPullDown();
+        }
+    });
   },
 
   /**
@@ -33,17 +31,31 @@ Page({
   /**
    * 添加购物车
    */
-  addCarts: function (event){
+  doBuy: function (event){
+    var that = this;
+    var id;
     if (typeof (event) == 'object') {
-      var id = event.currentTarget.dataset.id
+      id = event.currentTarget.dataset.id
     } else {
-      var id = event
+      id = event
     }
-    this.dever.location('carts/index?id=' + id);
+    this.dever.isLogin(function(res){
+      if(res){
+        that.dever.location('carts/index?id=' + id);
+      }else{
+        that.dever.location('login/index');
+      }
+    });
   },
 
   onPullDownRefresh: function () {
     this.dever.startPullDown(this);
-  }
+  },
 
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom: function () {
+    //TODO 
+  },
 })

+ 1 - 1
template/home/index.wxml

@@ -4,7 +4,7 @@
         <view class="page__title">{{item.name}}</view>
         <view class="page__desc">
           <text class="price">¥ {{item.price}}</text>
-          <view class="button" catchtap='addCarts' data-id="{{item.id}}" data-name="{{item.name}}"><button class="nxh__button" type="primary" size="mini">购买</button></view>
+          <view class="button" catchtap='doBuy' data-id="{{item.id}}" data-name="{{item.name}}"><button class="nxh__button" type="primary" size="mini">购买</button></view>
         </view>
     </view>
     <view class='cash'>

+ 63 - 21
template/login/index.js

@@ -5,45 +5,87 @@ Page({
     canIUse: wx.canIUse('button.open-type.getUserInfo')
   },
   onLoad: function (options) {
+    //TODO 
+  },
+
+  // 微信授权回调
+  bindGetUserInfo: function (e) {
+    var that = this;
+    if (e.detail.userInfo) {
+      that.wxLogin();
+    } else {
+      that.dever.log("用户拒绝授权");
+    }
+  },
+
+  // 查看是否授权,须在登录后调用
+  checkAuth: function(){
+    var that = this;
     // 查看是否授权
     wx.getSetting({
+      //是否带上登录态信息, true 返回的数据会包含 encryptedData, iv 等敏感信息
+      withCredentials: true,
       success(res) {
         if (res.authSetting['scope.userInfo']) {
           // 已经授权,可以直接调用 getUserInfo 获取头像昵称
           wx.getUserInfo({
             success: function (res) {
-              console.log(res.userInfo)
+              that.updateUserInfo(res);
             }
           })
         }
       }
-    })
+    });
   },
-  //微信授权回调
-  bindGetUserInfo: function (e) {
+
+  //微信登录
+  wxLogin: function(){
     var that = this;
-    if (e.detail.userInfo) {
-      console.log(e.detail.userInfo);
-      wx.login({
-        success: function (res) {
-          if (res.code) {
-            that.doLogin(res.code);
-          } else {
-            console.log(res.errMsg);
+    wx.login({
+      success: function (res) {
+        if (res.code) {
+          that.dever.request("passport.applet.bind"
+          ,{
+             json: 1,
+             code: res.code 
           }
+          ,{
+            success: function (data, res) {
+              that.dever.saveLoginInfo(data);
+              that.checkAuth();
+              that.dever.goBack();
+            },
+            fail: function(res){
+              that.dever.log('wx.login', JSON.stringify(res));
+            }
+          });
+        } else {
+          that.dever.log('wx.login', res.errMsg);
         }
-      })
-    } else {
-      console.log("拒绝了授权");
-    }
+      }
+    });
   },
-  doLogin: function(wxCode){
+
+  //更新用户信息
+  updateUserInfo: function (wxObj){
     var that = this;
-    this.dever.request("/wxLogin", {code: wxCode}, {
-      success: function (data, res) {
-        that.dever.goBack();
+    var userInfo = wxObj.userInfo;
+    var loginInfo = that.dever.getLoginInfo();
+    that.request('passport.applet.update'
+    ,{
+      nickname: userInfo.nickName,
+      avatarurl: userInfo.avatarUrl,
+      iv: wxObj.iv,
+      encryptedData: wxObj.encryptedData,
+      vid: loginInfo.vid,
+      uid: loginInfo.uid,
+      signature: loginInfo.signature
+    }
+    ,{
+      success: function(data, res){
+        that.derver.log('applet.update', '用户信息更新成功~');
       }
-    }, "POST");
+    }, 'POST');
   }
 
 })