checkout.js 4.9 KB

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