checkout.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. const pay = require('../../../services/pay.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. checkedGoodsList: [],
  8. checkedAddress: {},
  9. checkedCoupon: [],
  10. couponList: [],
  11. goodsTotalPrice: 0.00, //商品总价
  12. freightPrice: 0.00, //快递费
  13. couponPrice: 0.00, //优惠券的价格
  14. orderTotalPrice: 0.00, //订单总价
  15. actualPrice: 0.00, //实际需要支付的总价
  16. addressId: 0,
  17. couponId: 0
  18. },
  19. onLoad: function (options) {
  20. // 页面初始化 options为页面跳转所带来的参数
  21. try {
  22. var addressId = wx.getStorageSync('addressId');
  23. if (addressId) {
  24. this.setData({
  25. 'addressId': addressId
  26. });
  27. }
  28. var couponId = wx.getStorageSync('couponId');
  29. if (couponId) {
  30. this.setData({
  31. 'couponId': couponId
  32. });
  33. }
  34. } catch (e) {
  35. // Do something when catch error
  36. }
  37. },
  38. getCheckoutInfo: function () {
  39. let that = this;
  40. util.request(api.CartCheckout, { addressId: that.data.addressId, couponId: that.data.couponId }).then(function (res) {
  41. if (res.errno === 0) {
  42. console.log(res.data);
  43. that.setData({
  44. checkedGoodsList: res.data.checkedGoodsList,
  45. checkedAddress: res.data.checkedAddress,
  46. actualPrice: res.data.actualPrice,
  47. checkedCoupon: res.data.checkedCoupon,
  48. couponList: res.data.couponList,
  49. couponPrice: res.data.couponPrice,
  50. freightPrice: res.data.freightPrice,
  51. goodsTotalPrice: res.data.goodsTotalPrice,
  52. orderTotalPrice: res.data.orderTotalPrice
  53. });
  54. }
  55. wx.hideLoading();
  56. });
  57. },
  58. selectAddress() {
  59. wx.navigateTo({
  60. url: '/pages/shopping/address/address',
  61. })
  62. },
  63. addAddress() {
  64. wx.navigateTo({
  65. url: '/pages/shopping/addressAdd/addressAdd',
  66. })
  67. },
  68. onReady: function () {
  69. // 页面渲染完成
  70. },
  71. onShow: function () {
  72. // 页面显示
  73. wx.showLoading({
  74. title: '加载中...',
  75. })
  76. this.getCheckoutInfo();
  77. },
  78. onHide: function () {
  79. // 页面隐藏
  80. },
  81. onUnload: function () {
  82. // 页面关闭
  83. },
  84. submitOrder: function () {
  85. if (this.data.addressId <= 0) {
  86. util.showErrorToast('请选择收货地址');
  87. return false;
  88. }
  89. util.request(api.OrderSubmit, { addressId: this.data.addressId, couponId: this.data.couponId }, 'POST').then(res => {
  90. if (res.errno === 0) {
  91. const orderId = res.data.orderInfo.id;
  92. pay.payOrder(parseInt(orderId)).then(res => {
  93. wx.redirectTo({
  94. url: '/pages/payResult/payResult?status=1&orderId=' + orderId
  95. });
  96. }).catch(res => {
  97. wx.redirectTo({
  98. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  99. });
  100. });
  101. } else {
  102. util.showErrorToast('下单失败');
  103. }
  104. });
  105. }
  106. })