checkout.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. checkedGoodsList: [],
  7. checkedAddress: {},
  8. checkedCoupon: [],
  9. goodsTotalPrice: 0.00, //商品总价
  10. freightPrice: 0.00, //快递费
  11. couponPrice: 0.00, //优惠券的价格
  12. grouponPrice: 0.00, //团购优惠价格
  13. orderTotalPrice: 0.00, //订单总价
  14. actualPrice: 0.00, //实际需要支付的总价
  15. cartId: 0,
  16. addressId: 0,
  17. couponId: 0,
  18. grouponLinkId: 0, //参与的团购,如果是发起则为0
  19. grouponRulesId: 0 //团购规则ID
  20. },
  21. onLoad: function(options) {
  22. // 页面初始化 options为页面跳转所带来的参数
  23. },
  24. //获取checkou信息
  25. getCheckoutInfo: function() {
  26. let that = this;
  27. util.request(api.CartCheckout, {
  28. cartId: that.data.cartId,
  29. addressId: that.data.addressId,
  30. couponId: that.data.couponId,
  31. grouponRulesId: that.data.grouponRulesId
  32. }).then(function(res) {
  33. if (res.errno === 0) {
  34. that.setData({
  35. checkedGoodsList: res.data.checkedGoodsList,
  36. checkedAddress: res.data.checkedAddress,
  37. actualPrice: res.data.actualPrice,
  38. checkedCoupon: res.data.checkedCoupon,
  39. couponPrice: res.data.couponPrice,
  40. grouponPrice: res.data.grouponPrice,
  41. freightPrice: res.data.freightPrice,
  42. goodsTotalPrice: res.data.goodsTotalPrice,
  43. orderTotalPrice: res.data.orderTotalPrice,
  44. addressId: res.data.addressId,
  45. couponId: res.data.couponId,
  46. grouponRulesId: res.data.grouponRulesId,
  47. });
  48. }
  49. wx.hideLoading();
  50. });
  51. },
  52. selectAddress() {
  53. wx.navigateTo({
  54. url: '/pages/ucenter/address/address',
  55. })
  56. },
  57. onReady: function() {
  58. // 页面渲染完成
  59. },
  60. onShow: function() {
  61. // 页面显示
  62. wx.showLoading({
  63. title: '加载中...',
  64. });
  65. try {
  66. var cartId = wx.getStorageSync('cartId');
  67. if (cartId === "") {
  68. cartId = 0;
  69. }
  70. var addressId = wx.getStorageSync('addressId');
  71. if (addressId === "") {
  72. addressId = 0;
  73. }
  74. var couponId = wx.getStorageSync('couponId');
  75. if (couponId === "") {
  76. couponId = 0;
  77. }
  78. var grouponRulesId = wx.getStorageSync('grouponRulesId');
  79. if (grouponRulesId === "") {
  80. grouponRulesId = 0;
  81. }
  82. var grouponLinkId = wx.getStorageSync('grouponLinkId');
  83. if (grouponLinkId === "") {
  84. grouponLinkId = 0;
  85. }
  86. this.setData({
  87. cartId: cartId,
  88. addressId: addressId,
  89. couponId: couponId,
  90. grouponRulesId: grouponRulesId,
  91. grouponLinkId: grouponLinkId
  92. });
  93. } catch (e) {
  94. // Do something when catch error
  95. console.log(e);
  96. }
  97. this.getCheckoutInfo();
  98. },
  99. onHide: function() {
  100. // 页面隐藏
  101. },
  102. onUnload: function() {
  103. // 页面关闭
  104. },
  105. submitOrder: function() {
  106. if (this.data.addressId <= 0) {
  107. util.showErrorToast('请选择收货地址');
  108. return false;
  109. }
  110. util.request(api.OrderSubmit, {
  111. cartId: this.data.cartId,
  112. addressId: this.data.addressId,
  113. couponId: this.data.couponId,
  114. grouponRulesId: this.data.grouponRulesId,
  115. grouponLinkId: this.data.grouponLinkId
  116. }, 'POST').then(res => {
  117. if (res.errno === 0) {
  118. const orderId = res.data.orderId;
  119. util.request(api.OrderPrepay, {
  120. orderId: orderId
  121. }, 'POST').then(function(res) {
  122. if (res.errno === 0) {
  123. const payParam = res.data;
  124. console.log("支付过程开始");
  125. wx.requestPayment({
  126. 'timeStamp': payParam.timeStamp,
  127. 'nonceStr': payParam.nonceStr,
  128. 'package': payParam.packageValue,
  129. 'signType': payParam.signType,
  130. 'paySign': payParam.paySign,
  131. 'success': function(res) {
  132. console.log("支付过程成功");
  133. wx.redirectTo({
  134. url: '/pages/payResult/payResult?status=1&orderId=' + orderId
  135. });
  136. },
  137. 'fail': function(res) {
  138. console.log("支付过程失败");
  139. wx.redirectTo({
  140. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  141. });
  142. },
  143. 'complete': function(res) {
  144. console.log("支付过程结束")
  145. }
  146. });
  147. } else {
  148. wx.redirectTo({
  149. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  150. });
  151. }
  152. });
  153. } else {
  154. util.showErrorToast(res.errmsg);
  155. }
  156. });
  157. }
  158. });