Sfoglia il codice sorgente

feat: handle weixin pay

wangyuan 2 anni fa
parent
commit
7023b3f3b1
1 ha cambiato i file con 56 aggiunte e 3 eliminazioni
  1. 56 3
      pages/buy/buy.js

+ 56 - 3
pages/buy/buy.js

@@ -1,5 +1,9 @@
 // pages/buy/buy.js
 import { goodsList } from '../../api/index'
+import { submitOrder } from '../../api/pay'
+import itt from '../../utils/util'
+const app = getApp()
+
 Page({
 
   /**
@@ -7,7 +11,12 @@ Page({
    */
   data: {
     activeIndex: -1,
-    goodsList: []
+    goodsList: [],
+    goodsId: '',
+    orderAmount: '',
+    num: '',
+    appusername: '',
+    appuserheadimg: ''
   },
 
   /**
@@ -15,6 +24,10 @@ Page({
    */
   onShow() {
     this.getGoodsList()
+    this.setData({
+      appusername: app.globalData.userInfo.userName.length > 4 ? app.globalData.userInfo.userName.substring(0,4) : app.globalData.userInfo.userName,
+      appuserheadimg: app.globalData.userInfo.headImg
+    })
   },
 
   //获取套餐列表
@@ -48,13 +61,53 @@ Page({
 
   handlePayType (e) {
     var index = Number(e.currentTarget.dataset.index)
+    var goodsInfo = e.currentTarget.dataset.goodsinfo
+    console.log(goodsInfo, 'goodsInfo');
     this.setData({
-      activeIndex: index
+      activeIndex: index,
+      goodsId: goodsInfo.goodsId,
+      orderAmount: goodsInfo.salePrice,
+      num: goodsInfo.donateNumber + goodsInfo.useNumber
     })
   },
   // 调用微信支付
   handleWXPay () {
-    
+    var data = {
+      goodsId: this.data.goodsId,
+      orderAmount: this.data.orderAmount,
+      num: this.data.num
+    }
+    wx.showLoading({
+      title: '加载中...',
+      mask: true
+    })
+    submitOrder(data).then(res => {
+      wx.hideLoading()
+      // 拉起微信支付
+      wx.requestPayment({
+        timeStamp: res.data.timeStamp,
+        nonceStr: res.data.nonceStr,
+        package: res.data.packageStr,
+        signType: res.data.signType,
+        paySign: res.data.paySign,
+        success (res) { 
+          wx.showToast({
+            title: '支付成功',
+            icon: 'success'
+          })
+        },
+        fail (res) { 
+          console.log('weixin pay fail', res);
+        }
+      })
+    }).catch(e => {
+      wx.hideLoading()
+      wx.showModal({
+        content: e,
+        confirmColor: '#333',
+        showCancel: false
+      })
+    })
   },
   handleAppointment () {
     wx.navigateTo({