checkout.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. availableCouponLength: 0, // 可用的优惠券数量
  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. userCouponId: 0,
  19. message: '',
  20. grouponLinkId: 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. userCouponId: that.data.userCouponId,
  34. grouponRulesId: that.data.grouponRulesId
  35. }).then(function(res) {
  36. if (res.errno === 0) {
  37. that.setData({
  38. checkedGoodsList: res.data.checkedGoodsList,
  39. checkedAddress: res.data.checkedAddress,
  40. availableCouponLength: res.data.availableCouponLength,
  41. actualPrice: res.data.actualPrice,
  42. couponPrice: res.data.couponPrice,
  43. grouponPrice: res.data.grouponPrice,
  44. freightPrice: res.data.freightPrice,
  45. goodsTotalPrice: res.data.goodsTotalPrice,
  46. orderTotalPrice: res.data.orderTotalPrice,
  47. addressId: res.data.addressId,
  48. couponId: res.data.couponId,
  49. userCouponId: res.data.userCouponId,
  50. grouponRulesId: res.data.grouponRulesId,
  51. });
  52. }
  53. wx.hideLoading();
  54. });
  55. },
  56. selectAddress() {
  57. wx.navigateTo({
  58. url: '/pages/ucenter/address/address',
  59. })
  60. },
  61. selectCoupon() {
  62. wx.navigateTo({
  63. url: '/pages/ucenter/couponSelect/couponSelect',
  64. })
  65. },
  66. bindMessageInput: function(e) {
  67. this.setData({
  68. message: e.detail.value
  69. });
  70. },
  71. onReady: function() {
  72. // 页面渲染完成
  73. },
  74. onShow: function() {
  75. // 页面显示
  76. wx.showLoading({
  77. title: '加载中...',
  78. });
  79. try {
  80. var cartId = wx.getStorageSync('cartId');
  81. if (cartId === "") {
  82. cartId = 0;
  83. }
  84. var addressId = wx.getStorageSync('addressId');
  85. if (addressId === "") {
  86. addressId = 0;
  87. }
  88. var couponId = wx.getStorageSync('couponId');
  89. if (couponId === "") {
  90. couponId = 0;
  91. }
  92. var userCouponId = wx.getStorageSync('userCouponId');
  93. if (userCouponId === "") {
  94. userCouponId = 0;
  95. }
  96. var grouponRulesId = wx.getStorageSync('grouponRulesId');
  97. if (grouponRulesId === "") {
  98. grouponRulesId = 0;
  99. }
  100. var grouponLinkId = wx.getStorageSync('grouponLinkId');
  101. if (grouponLinkId === "") {
  102. grouponLinkId = 0;
  103. }
  104. this.setData({
  105. cartId: cartId,
  106. addressId: addressId,
  107. couponId: couponId,
  108. userCouponId: userCouponId,
  109. grouponRulesId: grouponRulesId,
  110. grouponLinkId: grouponLinkId
  111. });
  112. } catch (e) {
  113. // Do something when catch error
  114. console.log(e);
  115. }
  116. this.getCheckoutInfo();
  117. },
  118. onHide: function() {
  119. // 页面隐藏
  120. },
  121. onUnload: function() {
  122. // 页面关闭
  123. },
  124. submitOrder: function() {
  125. if (this.data.addressId <= 0) {
  126. util.showErrorToast('请选择收货地址');
  127. return false;
  128. }
  129. util.request(api.OrderSubmit, {
  130. cartId: this.data.cartId,
  131. addressId: this.data.addressId,
  132. couponId: this.data.couponId,
  133. userCouponId: this.data.userCouponId,
  134. message: this.data.message,
  135. grouponRulesId: this.data.grouponRulesId,
  136. grouponLinkId: this.data.grouponLinkId
  137. }, 'POST').then(res => {
  138. if (res.errno === 0) {
  139. // 下单成功,重置couponId
  140. try {
  141. wx.setStorageSync('couponId', 0);
  142. } catch (error) {
  143. }
  144. const orderId = res.data.orderId;
  145. const grouponLinkId = res.data.grouponLinkId;
  146. util.request(api.OrderPrepay, {
  147. orderId: orderId
  148. }, 'POST').then(function(res) {
  149. if (res.errno === 0) {
  150. const payParam = res.data;
  151. console.log("支付过程开始");
  152. wx.requestPayment({
  153. 'timeStamp': payParam.timeStamp,
  154. 'nonceStr': payParam.nonceStr,
  155. 'package': payParam.packageValue,
  156. 'signType': payParam.signType,
  157. 'paySign': payParam.paySign,
  158. 'success': function(res) {
  159. console.log("支付过程成功");
  160. if (grouponLinkId) {
  161. setTimeout(() => {
  162. wx.redirectTo({
  163. url: '/pages/groupon/grouponDetail/grouponDetail?id=' + grouponLinkId
  164. })
  165. }, 1000);
  166. } else {
  167. wx.redirectTo({
  168. url: '/pages/payResult/payResult?status=1&orderId=' + orderId
  169. });
  170. }
  171. },
  172. 'fail': function(res) {
  173. console.log("支付过程失败");
  174. wx.redirectTo({
  175. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  176. });
  177. },
  178. 'complete': function(res) {
  179. console.log("支付过程结束")
  180. }
  181. });
  182. } else {
  183. wx.redirectTo({
  184. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  185. });
  186. }
  187. });
  188. } else {
  189. util.showErrorToast(res.errmsg);
  190. }
  191. });
  192. }
  193. });