submit-order.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // pages/submit-order/submit-order.js
  2. var http = require("../../utils/http.js");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. popupShow: false,
  9. couponSts: 1,
  10. couponList: [],
  11. // 订单入口 0购物车 1立即购买
  12. orderEntry: "0",
  13. userAddr: null,
  14. orderItems: [],
  15. coupon: {
  16. totalLength: 0,
  17. canUseCoupons: [],
  18. noCanUseCoupons: []
  19. },
  20. actualTotal: 0,
  21. total: 0,
  22. totalCount: 0,
  23. transfee: 0,
  24. reduceAmount: 0,
  25. remark: "",
  26. couponIds: []
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function(options) {
  32. this.setData({
  33. orderEntry: options.orderEntry,
  34. });
  35. },
  36. //加载订单数据
  37. loadOrderData: function() {
  38. var addrId = 0;
  39. if (this.data.userAddr != null) {
  40. addrId = this.data.userAddr.addrId;
  41. }
  42. wx.showLoading({
  43. mask: true
  44. });
  45. var params = {
  46. url: "/p/order/confirm",
  47. method: "POST",
  48. data: {
  49. addrId: addrId,
  50. orderItem: this.data.orderEntry === "1" ? JSON.parse(wx.getStorageSync("orderItem")) : undefined,
  51. basketIds: this.data.orderEntry === "0" ? JSON.parse(wx.getStorageSync("basketIds")) : undefined,
  52. couponIds: this.data.couponIds,
  53. userChangeCoupon: 1
  54. },
  55. callBack: res => {
  56. wx.hideLoading();
  57. let orderItems = [];
  58. res.shopCartOrders[0].shopCartItemDiscounts.forEach(itemDiscount => {
  59. orderItems = orderItems.concat(itemDiscount.shopCartItems)
  60. })
  61. if (res.shopCartOrders[0].coupons) {
  62. let canUseCoupons = []
  63. let unCanUseCoupons = []
  64. res.shopCartOrders[0].coupons.forEach(coupon => {
  65. if (coupon.canUse) {
  66. canUseCoupons.push(coupon)
  67. } else {
  68. unCanUseCoupons.push(coupon)
  69. }
  70. })
  71. this.setData({
  72. coupons: {
  73. totalLength: res.shopCartOrders[0].coupons.length,
  74. canUseCoupons: canUseCoupons,
  75. unCanUseCoupons: unCanUseCoupons
  76. }
  77. })
  78. }
  79. this.setData({
  80. orderItems: orderItems,
  81. actualTotal: res.actualTotal,
  82. total: res.total,
  83. totalCount: res.totalCount,
  84. userAddr: res.userAddr,
  85. transfee: res.shopCartOrders[0].transfee,
  86. shopReduce: res.shopCartOrders[0].shopReduce,
  87. });
  88. },
  89. errCallBack: res => {
  90. wx.hideLoading();
  91. this.chooseCouponErrHandle(res)
  92. }
  93. };
  94. http.request(params);
  95. },
  96. /**
  97. * 优惠券选择出错处理方法
  98. */
  99. chooseCouponErrHandle(res) {
  100. // 优惠券不能共用处理方法
  101. if (res.statusCode == 601) {
  102. wx.showToast({
  103. title: res.data,
  104. icon: "none",
  105. duration: 3000,
  106. success: res => {
  107. this.setData({
  108. couponIds: []
  109. })
  110. }
  111. })
  112. setTimeout(() => {
  113. this.loadOrderData();
  114. }, 2500)
  115. }
  116. },
  117. onRemarksInput: function (e) {
  118. this.setData({
  119. remarks: e.detail.value
  120. });
  121. },
  122. /**
  123. * 提交订单
  124. */
  125. toPay: function() {
  126. if (!this.data.userAddr) {
  127. wx.showToast({
  128. title: '请选择地址',
  129. icon: "none"
  130. })
  131. return;
  132. }
  133. this.submitOrder();
  134. },
  135. submitOrder: function() {
  136. wx.showLoading({
  137. mask: true
  138. });
  139. var params = {
  140. url: "/p/order/submit",
  141. method: "POST",
  142. data: {
  143. orderShopParam: [{
  144. remarks: this.data.remark,
  145. shopId: 1
  146. }]
  147. },
  148. callBack: res => {
  149. wx.hideLoading();
  150. this.calWeixinPay(res.orderNumbers);
  151. }
  152. };
  153. http.request(params);
  154. },
  155. /**
  156. * 唤起微信支付
  157. */
  158. calWeixinPay: function(orderNumbers) {
  159. wx.showLoading({
  160. mask: true
  161. });
  162. var params = {
  163. url: "/p/order/pay",
  164. method: "POST",
  165. data: {
  166. payType: 1,
  167. orderNumbers: orderNumbers
  168. },
  169. callBack: function(res) {
  170. wx.hideLoading();
  171. wx.requestPayment({
  172. timeStamp: res.timeStamp,
  173. nonceStr: res.nonceStr,
  174. package: res.packageValue,
  175. signType: res.signType,
  176. paySign: res.paySign,
  177. success: e => {
  178. // console.log("支付成功");
  179. wx.navigateTo({
  180. url: '/pages/pay-result/pay-result?sts=1&orderNumbers=' + orderNumbers + "&orderType=" + this.data.orderType,
  181. })
  182. },
  183. fail: err => {
  184. wx.navigateTo({
  185. url: '/pages/pay-result/pay-result?sts=0&orderNumbers=' + orderNumbers + "&orderType=" + this.data.orderType,
  186. })
  187. }
  188. })
  189. }
  190. };
  191. http.request(params);
  192. },
  193. /**
  194. * 生命周期函数--监听页面初次渲染完成
  195. */
  196. onReady: function() {
  197. },
  198. /**
  199. * 生命周期函数--监听页面显示
  200. */
  201. onShow: function() {
  202. var pages = getCurrentPages();
  203. var currPage = pages[pages.length - 1];
  204. if (currPage.data.selAddress == "yes") {
  205. this.setData({ //将携带的参数赋值
  206. userAddr: currPage.data.item
  207. });
  208. }
  209. //获取订单数据
  210. this.loadOrderData();
  211. },
  212. /**
  213. * 生命周期函数--监听页面隐藏
  214. */
  215. onHide: function() {
  216. },
  217. /**
  218. * 生命周期函数--监听页面卸载
  219. */
  220. onUnload: function() {
  221. },
  222. /**
  223. * 页面相关事件处理函数--监听用户下拉动作
  224. */
  225. onPullDownRefresh: function() {
  226. },
  227. /**
  228. * 页面上拉触底事件的处理函数
  229. */
  230. onReachBottom: function() {
  231. },
  232. /**
  233. * 用户点击右上角分享
  234. */
  235. onShareAppMessage: function() {
  236. },
  237. changeCouponSts: function(e) {
  238. this.setData({
  239. couponSts: e.currentTarget.dataset.sts
  240. });
  241. },
  242. showCouponPopup: function() {
  243. this.setData({
  244. popupShow: true
  245. });
  246. },
  247. closePopup: function() {
  248. this.setData({
  249. popupShow: false
  250. });
  251. },
  252. /**
  253. * 去地址页面
  254. */
  255. toAddrListPage: function() {
  256. wx.navigateTo({
  257. url: '/pages/delivery-address/delivery-address?order=0',
  258. })
  259. },
  260. /**
  261. * 确定选择好的优惠券
  262. */
  263. choosedCoupon: function() {
  264. this.loadOrderData();
  265. this.setData({
  266. popupShow: false
  267. });
  268. },
  269. /**
  270. * 优惠券子组件发过来
  271. */
  272. checkCoupon: function(e) {
  273. var ths = this;
  274. let index = ths.data.couponIds.indexOf(e.detail.couponId);
  275. if (index === -1) {
  276. ths.data.couponIds.push(e.detail.couponId)
  277. } else {
  278. ths.data.couponIds.splice(index, 1)
  279. }
  280. }
  281. })