Forráskód Böngészése

feat 添加微信支付

tumobi 7 éve
szülő
commit
acbf6276eb

+ 1 - 1
app.json

@@ -6,9 +6,9 @@
     "pages/hotGoods/hotGoods",
     "pages/ucenter/address/address",
     "pages/ucenter/addressAdd/addressAdd",
-    "pages/ucenter/orderDetail/orderDetail",
     "pages/ucenter/footprint/footprint",
     "pages/ucenter/order/order",
+    "pages/ucenter/orderDetail/orderDetail",
     "pages/ucenter/feedback/feedback",
     "pages/ucenter/coupon/coupon",
     "pages/ucenter/collect/collect",

+ 1 - 1
config/api.js

@@ -27,7 +27,7 @@ module.exports = {
     CartCheckout: NewApiRootUrl + 'cart/checkout', // 下单前信息确认
 
     OrderSubmit: NewApiRootUrl + 'order/submit', // 提交订单
-    PayPrepayId: NewApiRootUrl + 'pay/payPrepay', //获取微信统一下单prepay_id
+    PayPrepayId: NewApiRootUrl + 'pay/prepay', //获取微信统一下单prepay_id
 
     CollectList: NewApiRootUrl + 'collect/list',  //收藏列表
     CollectAddOrDelete: NewApiRootUrl + 'collect/addordelete',  //添加或取消收藏

+ 25 - 11
pages/payResult/payResult.js

@@ -1,28 +1,42 @@
-var app = getApp();
+var util = require('../../utils/util.js');
+var api = require('../../config/api.js');
+const pay = require('../../services/pay.js');
 
+var app = getApp();
 Page({
-  data:{
+  data: {
     status: false,
+    orderId: 0
   },
-  onLoad:function(options){
+  onLoad: function (options) {
     // 页面初始化 options为页面跳转所带来的参数
     this.setData({
+      orderId: options.orderId || 24,
       status: options.status
     })
   },
-  onReady:function(){
-    
+  onReady: function () {
+
   },
-  onShow:function(){
+  onShow: function () {
     // 页面显示
-    
+
   },
-  onHide:function(){
+  onHide: function () {
     // 页面隐藏
-    
+
   },
-  onUnload:function(){
+  onUnload: function () {
     // 页面关闭
-    
+
+  },
+  payOrder() {
+    pay.payOrder(parseInt(this.data.orderId)).then(res => {
+      this.setData({
+        status: true
+      });
+    }).catch(res => {
+      util.showErrorToast('支付失败');
+    });
   }
 })

+ 1 - 1
pages/payResult/payResult.wxml

@@ -15,7 +15,7 @@
       </view>
       <view class="btns">
         <navigator class="btn" url="/pages/ucenter/order/order" open-type="redirect">查看订单</navigator>
-        <navigator class="btn" url="/pages/pay/pay" open-type="redirect">重新付款</navigator>
+        <view class="btn" bindtap='payOrder'>重新付款</view>
       </view>
     </view>
   </view>

+ 28 - 26
pages/shopping/checkout/checkout.js

@@ -1,10 +1,11 @@
 var util = require('../../../utils/util.js');
 var api = require('../../../config/api.js');
+const pay = require('../../../services/pay.js');
 
 var app = getApp();
 
 Page({
-  data:{
+  data: {
     checkedGoodsList: [],
     checkedAddress: {},
     checkedCoupon: [],
@@ -17,7 +18,7 @@ Page({
     addressId: 0,
     couponId: 0
   },
-  onLoad:function(options){
+  onLoad: function (options) {
 
     // 页面初始化 options为页面跳转所带来的参数
 
@@ -39,11 +40,11 @@ Page({
       // Do something when catch error
     }
 
-    
+
   },
   getCheckoutInfo: function () {
     let that = this;
-    util.request(api.CartCheckout, { addressId: that.data.addressId, couponId: that.data.couponId}).then(function (res) {
+    util.request(api.CartCheckout, { addressId: that.data.addressId, couponId: that.data.couponId }).then(function (res) {
       if (res.errno === 0) {
         console.log(res.data);
         that.setData({
@@ -61,7 +62,7 @@ Page({
       wx.hideLoading();
     });
   },
-  selectAddress(){
+  selectAddress() {
     wx.navigateTo({
       url: '/pages/shopping/address/address',
     })
@@ -71,45 +72,46 @@ Page({
       url: '/pages/shopping/addressAdd/addressAdd',
     })
   },
-  onReady:function(){
+  onReady: function () {
     // 页面渲染完成
-    
+
   },
-  onShow:function(){
+  onShow: function () {
     // 页面显示
     wx.showLoading({
       title: '加载中...',
     })
     this.getCheckoutInfo();
-    
+
   },
-  onHide:function(){
+  onHide: function () {
     // 页面隐藏
-    
+
   },
-  onUnload:function(){
+  onUnload: function () {
     // 页面关闭
-    
-  },
-  submitOrder: function(){
 
-    if (this.data.addressId <=0) {
+  },
+  submitOrder: function () {
+    if (this.data.addressId <= 0) {
       util.showErrorToast('请选择收货地址');
       return false;
     }
-
-    let that = this;
-    util.request(api.OrderSubmit, { addressId: that.data.addressId, couponId: that.data.couponId }, 'POST').then(function (res) {
+    util.request(api.OrderSubmit, { addressId: this.data.addressId, couponId: this.data.couponId }, 'POST').then(res => {
       if (res.errno === 0) {
-        wx.redirectTo({
-          url: '/pages/pay/pay?orderId=' + res.data.orderInfo.id + '&actualPrice=' + res.data.orderInfo.actual_price
-        })
-      
+        const orderId = res.data.orderInfo.id;
+        pay.payOrder(parseInt(orderId)).then(res => {
+          wx.redirectTo({
+            url: '/pages/payResult/payResult?status=1&orderId=' + orderId
+          });
+        }).catch(res => {
+          wx.redirectTo({
+            url: '/pages/payResult/payResult?status=0&orderId=' + orderId
+          });
+        });
       } else {
-        util.showErrorToast(res.data.errmsg);
+        util.showErrorToast('下单失败');
       }
     });
-
-   
   }
 })

+ 1 - 1
pages/ucenter/index/index.wxml

@@ -3,7 +3,7 @@
     <image class="avatar" src="{{userInfo.avatar}}"></image>
     <view class="info">
       <text class="name">{{userInfo.nickname}}</text>
-      <text class="level">{{userInfo.username}}</text>
+      <!-- <text class="level">{{userInfo.username}}</text> -->
     </view>
   </view>
 

+ 30 - 11
pages/ucenter/orderDetail/orderDetail.js

@@ -2,13 +2,13 @@ var util = require('../../../utils/util.js');
 var api = require('../../../config/api.js');
 
 Page({
-  data:{
+  data: {
     orderId: 0,
     orderInfo: {},
     orderGoods: [],
     handleOption: {}
   },
-  onLoad:function(options){
+  onLoad: function (options) {
     // 页面初始化 options为页面跳转所带来的参数
     this.setData({
       orderId: options.id
@@ -31,10 +31,10 @@ Page({
       }
     });
   },
-  payTimer (){
+  payTimer() {
     let that = this;
     let orderInfo = that.data.orderInfo;
-    
+
     setInterval(() => {
       console.log(orderInfo);
       orderInfo.add_time -= 1;
@@ -44,20 +44,39 @@ Page({
     }, 1000);
   },
   payOrder() {
-    wx.redirectTo({
-      url: '/pages/pay/pay',
-    })
+    let that = this;
+    util.request(api.PayPrepayId, {
+      orderId: that.data.orderId || 15
+    }).then(function (res) {
+      if (res.errno === 0) {
+        const payParam = res.data;
+        wx.requestPayment({
+          'timeStamp': payParam.timeStamp,
+          'nonceStr': payParam.nonceStr,
+          'package': payParam.package,
+          'signType': payParam.signType,
+          'paySign': payParam.paySign,
+          'success': function (res) {
+            console.log(res)
+          },
+          'fail': function (res) {
+            console.log(res)
+          }
+        });
+      }
+    });
+
   },
-  onReady:function(){
+  onReady: function () {
     // 页面渲染完成
   },
-  onShow:function(){
+  onShow: function () {
     // 页面显示
   },
-  onHide:function(){
+  onHide: function () {
     // 页面隐藏
   },
-  onUnload:function(){
+  onUnload: function () {
     // 页面关闭
   }
 })

+ 56 - 0
services/pay.js

@@ -0,0 +1,56 @@
+/**
+ * 支付相关服务
+ */
+
+const util = require('../utils/util.js');
+const api = require('../config/api.js');
+
+/**
+ * 判断用户是否登录
+ */
+function payOrder(orderId) {
+  return new Promise(function (resolve, reject) {
+    util.request(api.PayPrepayId, {
+      orderId: orderId
+    }).then((res) => {
+      console.log(res)
+      if (res.errno === 0) {
+        const payParam = res.data;
+        wx.requestPayment({
+          'timeStamp': payParam.timeStamp,
+          'nonceStr': payParam.nonceStr,
+          'package': payParam.package,
+          'signType': payParam.signType,
+          'paySign': payParam.paySign,
+          'success': function (res) {
+            resolve(res);
+          },
+          'fail': function (res) {
+            reject(res);
+          },
+          'complete': function (res) {
+            reject(res);
+          }
+        });
+      } else {
+        reject(res);
+      }
+    });
+  });
+}
+
+
+module.exports = {
+  payOrder,
+};
+
+
+
+
+
+
+
+
+
+
+

+ 2 - 0
utils/util.js

@@ -102,6 +102,7 @@ function login() {
       success: function (res) {
         if (res.code) {
           //登录远程服务器
+          console.log(res)
           resolve(res);
         } else {
           reject(res);
@@ -119,6 +120,7 @@ function getUserInfo() {
     wx.getUserInfo({
       withCredentials: true,
       success: function (res) {
+        console.log(res)
         resolve(res);
       },
       fail: function (err) {