瀏覽代碼

feat: use virtual payment for recharge

root 1 天之前
父節點
當前提交
7461ddad5d
共有 2 個文件被更改,包括 144 次插入74 次删除
  1. 104 52
      packageUser/pages/user/recharge/recharge.js
  2. 40 22
      utils/request.js

+ 104 - 52
packageUser/pages/user/recharge/recharge.js

@@ -43,85 +43,137 @@ Page({
   },
   createRecharge () {
     var that = this
-    var url = 'v1/recas_order/generate'
-    var money = that.data.price * 100
+    var url = 'v1/virtual_pay/recharge'
+    var price = parseFloat(that.data.price)
+    var money = Math.round(price * 100)
     var params = {
-      count: money,
-      payway: 'recharge_wxpay'
+      count: money
     }
     var success = function (res) {
       if (res.data) {
-        that.data.lock = false
-        that.rechargeClick(res.data.order_id)
+        that.rechargeClick(res.data)
       }
     }
-    if (!money) {
+    var fail = function () {
+      that.setData({
+        lock: false
+      })
+    }
+    if (!price || isNaN(price)) {
       wx.showToast({
         title: '充值金额不能为空',
         icon: 'none',
         duration: 2000
       })
-    } else if (money < 1) {
+    } else if (money < 100) {
+      wx.showToast({
+        title: '充值金额要大于1元',
+        icon: 'none',
+        duration: 2000
+      })
+    } else if (money % 100 !== 0) {
       wx.showToast({
-        title: '充值金额要大于0.01',
+        title: '虚拟支付需输入整数元',
         icon: 'none',
         duration: 2000
       })
     } else {
       if (!that.data.lock) {
-        that.data.lock = true
-        _request.$post(url, params, success)
+        that.setData({
+          lock: true
+        })
+        _request.$post(url, params, success, fail)
       }
     }
   },
-  rechargeClick (oid) {
+  rechargeClick (payData) {
     var that = this
-    var url = 'v1/pay'
-    var params = {
-      order_id: oid,
-      pay_way: 'recharge_wxpay'
+    if (!wx.requestVirtualPayment) {
+      that.setData({
+        lock: false
+      })
+      wx.showToast({
+        title: '当前微信版本不支持虚拟支付',
+        icon: 'none',
+        duration: 2000
+      })
+      return
     }
-    var success = function (val) {
-      wx.requestPayment({
-        'timeStamp': val.data.pay_data.timeStamp,
-        'nonceStr': val.data.pay_data.nonceStr,
-        'package': val.data.pay_data.package,
-        'signType': val.data.pay_data.signType,
-        'paySign': val.data.pay_data.paySign,
-        'success':function(res){
-          wx.showToast({
-            title: '充值成功',
-            icon: 'success',
-            duration: 1000
-          })
-		  
-		  console.log('that.data.source',that.data.source)
-		  setTimeout(function(){
-			  if(that.data.source){
-			  			  // wx.navigateTo({
-			  			  //   url: that.data.source
-			  			  // })
-						  wx.navigateBack();
-						  wx.navigateBack();
-			  }else{
-			  			  // wx.navigateTo({
-			  			  //   url: '/packageUser/pages/user/fund/fund'
-			  			  // })
-			  			  wx.navigateBack();
-			  }
-		  },1000)
-		  
-          
-        },
-        'fail':function(res){
-        }
+    wx.requestVirtualPayment({
+      env: payData.env,
+      mode: payData.mode,
+      offerId: payData.offerId,
+      signData: payData.signData,
+      paySig: payData.paySig,
+      signature: payData.signature,
+      success: function () {
+        wx.showToast({
+          title: '充值处理中',
+          icon: 'success',
+          duration: 1000
+        })
+        that.waitRechargePaid(payData.order_id, 0)
+      },
+      fail: function () {
+        that.setData({
+          lock: false
+        })
+      }
+    })
+  },
+  waitRechargePaid (oid, times) {
+    var that = this
+    var url = 'v1/virtual_pay/recharge/status'
+    var success = function (res) {
+      if (res.data && res.data.state === 1) {
+        that.getCashTotal()
+        wx.showToast({
+          title: '充值成功',
+          icon: 'success',
+          duration: 1000
+        })
+        setTimeout(function () {
+          that.backAfterRecharge()
+        }, 1000)
+      } else if (times < 5) {
+        setTimeout(function () {
+          that.waitRechargePaid(oid, times + 1)
+        }, 1000)
+      } else {
+        that.setData({
+          lock: false
+        })
+        wx.showToast({
+          title: '支付已提交,到账稍后刷新',
+          icon: 'none',
+          duration: 2000
+        })
+        that.getCashTotal()
+      }
+    }
+    var fail = function () {
+      that.setData({
+        lock: false
       })
     }
-    _request.$post(url, params, success)
+    _request.$get(url, { order_id: oid }, success, fail)
+  },
+  backAfterRecharge () {
+    var that = this
+    that.setData({
+      lock: false
+    })
+    console.log('that.data.source', that.data.source)
+    if (that.data.source) {
+      wx.navigateBack()
+      wx.navigateBack()
+    } else {
+      wx.navigateBack()
+    }
   },
   onShareAppMessage: function (val) {
     return _request.share({
       sc: 'xcx_user_recharge'
     })
   }
-})
+})

+ 40 - 22
utils/request.js

@@ -1,15 +1,28 @@
+const prodApiHost = 'https://xcx-api.fohowyc.com/';
+const prodApiHostX = 'https://app-api.fohowyc.com/';
+const testApiHost = 'https://tfohowapi.hiwavo.com/';
+const testApiHostX = 'https://txj.hiwavo.com/';
 
-
-// 新正式
-  const apiHost = 'https://xcx-api.fohowyc.com/';
-  const apiHostX = 'https://app-api.fohowyc.com/';//正式
-
-// 测试
-  // const apiHost = 'https://tfohowapi.hiwavo.com/';
-  // const apiHostX = 'https://txj.hiwavo.com/';
-
-
-
+function getApiHostConfig () {
+  var envVersion = 'release'
+  try {
+    var accountInfo = wx.getAccountInfoSync && wx.getAccountInfoSync()
+    if (accountInfo && accountInfo.miniProgram && accountInfo.miniProgram.envVersion) {
+      envVersion = accountInfo.miniProgram.envVersion
+    }
+  } catch (e) {
+  }
+  if (envVersion === 'release') {
+    return {
+      apiHost: prodApiHost,
+      apiHostX: prodApiHostX
+    }
+  }
+  return {
+    apiHost: testApiHost,
+    apiHostX: testApiHostX
+  }
+}
 
 
 function $get (url, params, success, fail) {
@@ -18,11 +31,12 @@ function $get (url, params, success, fail) {
   var token = wx.getStorageSync('lbt_token_key')
   // console.log('nowtoken',token);
   let apih = '';
+  var apiConfig = getApiHostConfig()
   if(url.split(':')[0] == 'newapi'){
-	  apih = apiHostX;
+	  apih = apiConfig.apiHostX;
 	  url = url.split(':')[1];
   }else{
-	  apih = apiHost;
+	  apih = apiConfig.apiHost;
   }
   console.log('apih',apih);
   wx.request({
@@ -52,11 +66,12 @@ function $post (url, params, success, fail) {
   var session = wx.getStorageSync('lbt_session_key');
   var token = wx.getStorageSync('lbt_token_key');
   let apih = '';
+  var apiConfig = getApiHostConfig()
   if(url.split(':')[0] == 'newapi'){
-  	  apih = apiHostX;
+	  apih = apiConfig.apiHostX;
   	  url = url.split(':')[1];
   }else{
-  	  apih = apiHost;
+	  apih = apiConfig.apiHost;
   }
   wx.request({
     url: apih + url,
@@ -84,11 +99,12 @@ function $put (url, params, success, fail) {
   var session = wx.getStorageSync('lbt_session_key');
   var token = wx.getStorageSync('lbt_token_key');
   let apih = '';
+  var apiConfig = getApiHostConfig()
   if(url.split(':')[0] == 'newapi'){
-  	  apih = apiHostX;
+	  apih = apiConfig.apiHostX;
   	  url = url.split(':')[1];
   }else{
-  	  apih = apiHost;
+	  apih = apiConfig.apiHost;
   }
   wx.request({
     url: apih + url,
@@ -116,11 +132,12 @@ function $del (url, params, success, fail) {
   var session = wx.getStorageSync('lbt_session_key');
   var token = wx.getStorageSync('lbt_token_key');
   let apih = '';
+  var apiConfig = getApiHostConfig()
   if(url.split(':')[0] == 'newapi'){
-  	  apih = apiHostX;
+	  apih = apiConfig.apiHostX;
   	  url = url.split(':')[1];
   }else{
-  	  apih = apiHost;
+	  apih = apiConfig.apiHost;
   }
   wx.request({
     url: apih + url,
@@ -406,8 +423,9 @@ function $uploadFile(url, name, file, params, success, fail){
 	wx.showNavigationBarLoading()
 	var session = wx.getStorageSync('lbt_session_key');
 	var token = wx.getStorageSync('lbt_token_key');
+	var apiConfig = getApiHostConfig()
 	wx.uploadFile({
-		url: apiHost + url, //仅为示例,非真实的接口地址
+		url: apiConfig.apiHost + url, //仅为示例,非真实的接口地址
 		filePath: file,
 		name: name,
 		formData: params,
@@ -448,8 +466,8 @@ module.exports = {
   $put: $put,
   $get: $get,
   $del: $del,
-  apiHost: apiHost,
-  apiHostX: apiHostX,
+  apiHost: getApiHostConfig().apiHost,
+  apiHostX: getApiHostConfig().apiHostX,
   login: login,
   getAuthorize: getAuthorize,
   updateWxUserInfo: updateWxUserInfo,