checkout.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. return 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. that.selectCouponId = res.data.couponIdList
  53. //设置默认收获地址
  54. if (that.data.checkedAddress.id){
  55. let addressId = that.data.checkedAddress.id;
  56. if (addressId) {
  57. that.setData({ addressId: addressId });
  58. }
  59. }else{
  60. wx.showModal({
  61. title: '',
  62. content: '请添加默认收货地址!',
  63. success: function (res) {
  64. if (res.confirm) {
  65. that.selectAddress();
  66. }
  67. }
  68. })
  69. }
  70. }
  71. wx.hideLoading();
  72. });
  73. },
  74. selectAddress() {
  75. wx.navigateTo({
  76. url: '/pages/shopping/address/address',
  77. })
  78. },
  79. addAddress() {
  80. wx.navigateTo({
  81. url: '/pages/shopping/addressAdd/addressAdd',
  82. })
  83. },
  84. onReady: function () {
  85. // 页面渲染完成
  86. },
  87. onShow: function () {
  88. this.getCouponData()
  89. // 页面显示
  90. wx.showLoading({
  91. title: '加载中...',
  92. })
  93. this.getCheckoutInfo().then(res => {
  94. var addressId = wx.getStorageSync('addressId');
  95. if (addressId) {
  96. this.setData({
  97. 'addressId': addressId
  98. });
  99. }
  100. this.getAddressDetail(addressId)
  101. })
  102. },
  103. /**
  104. * 获取优惠券
  105. */
  106. getCouponData: function () {
  107. if (app.globalData.userCoupon == 'USE_COUPON') {
  108. this.setData({
  109. couponDesc: app.globalData.courseCouponCode.name,
  110. couponId: app.globalData.courseCouponCode.user_coupon_id,
  111. })
  112. } else if (app.globalData.userCoupon == 'NO_USE_COUPON') {
  113. this.setData({
  114. couponDesc: "不使用优惠券",
  115. couponId: '',
  116. })
  117. }
  118. },
  119. onHide: function () {
  120. // 页面隐藏
  121. },
  122. onUnload: function () {
  123. // 页面关闭
  124. },
  125. getAddressDetail(id) {
  126. if(!id) return
  127. util.request(api.AddressDetail, { id }).then( (res)=> {
  128. if (res.errno === 0) {
  129. if (res.data) {
  130. console.log('-------',res)
  131. this.setData({
  132. checkedAddress: res.data
  133. });
  134. }
  135. }
  136. });
  137. },
  138. /**
  139. * 选择可用优惠券
  140. */
  141. tapCoupon: function () {
  142. let that = this
  143. wx.navigateTo({
  144. url: '../selCoupon/selCoupon?buyType=' + that.data.buyType,
  145. })
  146. },
  147. submitOrder: function () {
  148. if (this.data.addressId <= 0) {
  149. util.showErrorToast('请选择收货地址');
  150. return false;
  151. }
  152. util.request(api.OrderSubmit, { addressId: this.data.addressId, couponIdList: this.selectCouponId, type: this.data.buyType }, 'POST', 'application/json').then(res => {
  153. if (res.errno === 0) {
  154. const orderId = res.data.orderInfo.id;
  155. pay.payOrder(parseInt(orderId)).then(res => {
  156. wx.redirectTo({
  157. url: '/pages/payResult/payResult?status=1&orderId=' + orderId
  158. });
  159. }).catch(res => {
  160. wx.redirectTo({
  161. url: '/pages/payResult/payResult?status=0&orderId=' + orderId
  162. });
  163. });
  164. } else {
  165. util.showErrorToast('下单失败');
  166. }
  167. });
  168. }
  169. })