checkout.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. const pay = require('../../../services/pay.js');
  4. import { Router } from '../../../utils/router.js'
  5. var app = getApp();
  6. Router({
  7. data: {
  8. checkedGoodsList: [],
  9. checkedAddress: {},
  10. checkedCoupon: [],
  11. couponList: [],
  12. goodsTotalPrice: 0.00, //商品总价
  13. freightPrice: 0.00, //快递费
  14. couponPrice: 0.00, //优惠券的价格
  15. orderTotalPrice: 0.00, //订单总价
  16. actualPrice: 0.00, //实际需要支付的总价
  17. addressId: 0,
  18. couponId: 0,
  19. isBuy: false,
  20. couponDesc: '',
  21. couponCode: '',
  22. buyType: ''
  23. },
  24. onLoad: function (options) {
  25. // 页面初始化 options为页面跳转所带来的参数
  26. if (options.isBuy!=null) {
  27. this.data.isBuy = options.isBuy
  28. }
  29. this.data.buyType = this.data.isBuy?'buy':'cart'
  30. //每次重新加载界面,清空数据
  31. app.globalData.userCoupon = 'NO_USE_COUPON'
  32. app.globalData.courseCouponCode = {}
  33. wx.setStorageSync('addressId', '');
  34. },
  35. getCheckoutInfo: function () {
  36. let that = this;
  37. var url = api.CartCheckout
  38. let buyType = this.data.isBuy ? 'buy' : 'cart'
  39. util.request(url, { addressId: that.data.addressId, couponId: that.data.couponId, type: buyType }).then(function (res) {
  40. if (res.errno === 0) {
  41. that.setData({
  42. checkedGoodsList: res.data.checkedGoodsList,
  43. checkedAddress: res.data.checkedAddress,
  44. actualPrice: res.data.actualPrice,
  45. checkedCoupon: res.data.checkedCoupon ? res.data.checkedCoupon : "",
  46. couponList: res.data.couponList ? res.data.couponList : "",
  47. couponPrice: res.data.couponPrice,
  48. freightPrice: res.data.freightPrice,
  49. goodsTotalPrice: res.data.goodsTotalPrice,
  50. orderTotalPrice: res.data.orderTotalPrice
  51. });
  52. //设置默认收获地址
  53. if (that.data.checkedAddress.id){
  54. let addressId = that.data.checkedAddress.id;
  55. if (addressId) {
  56. that.setData({ addressId: addressId });
  57. }
  58. }else{
  59. wx.showModal({
  60. title: '',
  61. content: '请添加默认收货地址!',
  62. success: function (res) {
  63. if (res.confirm) {
  64. that.selectAddress();
  65. }
  66. }
  67. })
  68. }
  69. }
  70. wx.hideLoading();
  71. });
  72. },
  73. selectAddress() {
  74. wx.navigateTo({
  75. url: '/pages/shopping/address/address',
  76. })
  77. },
  78. addAddress() {
  79. wx.navigateTo({
  80. url: '/pages/shopping/addressAdd/addressAdd',
  81. })
  82. },
  83. onReady: function () {
  84. // 页面渲染完成
  85. },
  86. onShow: function () {
  87. this.getCouponData()
  88. // 页面显示
  89. wx.showLoading({
  90. title: '加载中...',
  91. })
  92. this.getCheckoutInfo();
  93. try {
  94. setTimeout(()=>{
  95. var addressId = wx.getStorageSync('addressId');
  96. if (addressId) {
  97. this.setData({
  98. 'addressId': addressId
  99. });
  100. }
  101. this.getAddressDetail(addressId)
  102. })
  103. } catch (e) {
  104. // Do something when catch error
  105. }
  106. },
  107. /**
  108. * 获取优惠券
  109. */
  110. getCouponData: function () {
  111. if (app.globalData.userCoupon == 'USE_COUPON') {
  112. this.setData({
  113. couponDesc: app.globalData.courseCouponCode.name,
  114. couponId: app.globalData.courseCouponCode.user_coupon_id,
  115. })
  116. } else if (app.globalData.userCoupon == 'NO_USE_COUPON') {
  117. this.setData({
  118. couponDesc: "不使用优惠券",
  119. couponId: '',
  120. })
  121. }
  122. },
  123. onHide: function () {
  124. // 页面隐藏
  125. },
  126. onUnload: function () {
  127. // 页面关闭
  128. },
  129. getAddressDetail(id) {
  130. if(!id) return
  131. util.request(api.AddressDetail, { id }).then( (res)=> {
  132. if (res.errno === 0) {
  133. if (res.data) {
  134. console.log('-------',res)
  135. this.setData({
  136. checkedAddress: res.data
  137. });
  138. }
  139. }
  140. });
  141. },
  142. /**
  143. * 选择可用优惠券
  144. */
  145. tapCoupon: function () {
  146. let that = this
  147. wx.navigateTo({
  148. url: '../selCoupon/selCoupon?buyType=' + that.data.buyType,
  149. })
  150. },
  151. submitOrder: function () {
  152. if (this.data.addressId <= 0) {
  153. util.showErrorToast('请选择收货地址');
  154. return false;
  155. }
  156. util.request(api.OrderSubmit, { addressId: this.data.addressId, couponId: this.data.couponId, type: this.data.buyType }, 'POST', 'application/json').then(res => {
  157. if (res.errno === 0) {
  158. const orderId = res.data.orderInfo.id;
  159. pay.payOrder(parseInt(orderId)).then(res => {
  160. wx.redirectTo({
  161. url: '/pages/payResult/payResult?status=1&orderId=' + orderId
  162. });
  163. }).catch(res => {
  164. wx.redirectTo({
  165. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  166. });
  167. });
  168. } else {
  169. util.showErrorToast('下单失败');
  170. }
  171. });
  172. }
  173. })