submit-order.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. // 订单入口 0购物车 1立即购买
  10. orderEntry: "0",
  11. userAddr: null,
  12. orderItems: [],
  13. shopCartOrders: [], //所有店铺的数据
  14. couponSts: 1,
  15. platformCoupons: {}, // 平台优惠券数据
  16. showCoupons: {
  17. canUseCoupons: [],
  18. unCanUseCoupons: []
  19. },
  20. actualTotal: 0,
  21. total: 0,
  22. totalCount: 0,
  23. transfee: 0,
  24. reduceAmount: 0, //减免金额
  25. couponIds: [],
  26. orderReduce: 0,
  27. userChangeCoupon: 0, // 用户有没有对优惠券进行改变
  28. orderReduce: 0,
  29. choose: true,
  30. totalScoreAmount: 0, //积分抵扣金额
  31. maxUsableScore: 0, //整个订单可以使用的积分数
  32. isScorePay: 0, //用户是否选择积分抵现(0不使用 1使用 默认不使用)
  33. isChecked: false, //是否选择会员积分抵现
  34. totalLevelAmount: 0, //等级抵扣金额
  35. freeTransfee: 0, //用户等级免运费金额
  36. userUseScore: '', // 用于抵扣的金额数量
  37. scorePopPlaceholder: '', // 积分弹窗输入框占位符
  38. isPurePoints: false,
  39. address_id: 0,
  40. shop_id: 0,
  41. deliveryAmount_num: 0,
  42. shop_weight: 0,
  43. is_distance:false
  44. },
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad: function (options) {
  49. this.setData({
  50. orderEntry: options.orderEntry,
  51. });
  52. // 请求用户可用积分
  53. this.getUserScore()
  54. },
  55. /**
  56. * 获取用户可用积分
  57. */
  58. getUserScore() {
  59. var params = {
  60. url: '/p/user/userInfo',
  61. method: 'GET',
  62. callBack: res => {
  63. this.setData({
  64. accountScore: res.score
  65. })
  66. }
  67. }
  68. http.request(params)
  69. },
  70. // 会员积分抵现选择
  71. useMemberPoints: function () {
  72. this.data.isChecked = !this.data.isChecked
  73. if (this.data.maxUsableScore > 0) { // maxUsableScore 整个订单可以使用的积分数
  74. this.setData({
  75. isScorePay: this.data.isChecked ? 1 : 0,
  76. userUseScore: this.data.isChecked ? this.data.userUseScore : ''
  77. })
  78. this.loadOrderData()
  79. }
  80. },
  81. /**
  82. * 积分抵扣输入框
  83. */
  84. handleScoreInput(e) {
  85. var maxScore = 0
  86. if (this.data.maxUsableScore > this.data.accountScore) {
  87. maxScore = this.data.accountScore
  88. } else {
  89. maxScore = this.data.maxUsableScore
  90. }
  91. this.setData({
  92. userUseScore: e.detail.value > 0 ? e.detail.value > maxScore ? maxScore : e.detail.value : ''
  93. })
  94. },
  95. /**
  96. * 修改积分确定弹窗
  97. */
  98. confirmScore() {
  99. this.loadOrderData()
  100. this.closePopup()
  101. },
  102. //加载订单数据
  103. loadOrderData: function () {
  104. var addrId = 0;
  105. if (this.data.userAddr != null) {
  106. addrId = this.data.userAddr.addrId;
  107. }
  108. wx.showLoading({
  109. mask: true
  110. });
  111. var params = {
  112. url: "/p/order/confirm",
  113. method: "POST",
  114. data: {
  115. addrId: addrId,
  116. orderItem: this.data.orderEntry === "1" ? JSON.parse(wx.getStorageSync("orderItem")) : undefined,
  117. basketIds: this.data.orderEntry === "0" ? JSON.parse(wx.getStorageSync("basketIds")) : undefined,
  118. couponIds: this.data.couponIds,
  119. userChangeCoupon: this.data.userChangeCoupon,
  120. isScorePay: this.data.isScorePay,
  121. userUseScore: this.data.userUseScore ? this.data.userUseScore : 0,
  122. },
  123. callBack: res => {
  124. this.setData({
  125. address_id: res.userAddr.addrId,
  126. shop_weight: res.weight
  127. })
  128. wx.hideLoading();
  129. var shopCartOrders = res.shopCartOrders; //购物车店铺商品信息列表
  130. var couponIds = []
  131. let img=''
  132. shopCartOrders.forEach(shopCart => {
  133. shopCart.shopCartItemDiscounts.forEach(e=>{
  134. e.shopCartItems.forEach(item=>{
  135. img=item.pic.split(',')
  136. item.pic=img[0]
  137. })
  138. })
  139. this.setData({
  140. shop_id: shopCart.shopId
  141. })
  142. shopCart.remarks = '';
  143. shopCart.shopCoupons = this.splitCouponAndPushCouponIds(shopCart.coupons, couponIds)
  144. })
  145. // 平台优惠券
  146. var platformCoupons = this.splitCouponAndPushCouponIds(res.coupons, couponIds) //平台优惠券
  147. // 平台优惠券end
  148. this.setData({
  149. platformCoupons: platformCoupons, //整个订单可以使用的优惠券列表
  150. shopCartOrders: shopCartOrders, //所有的店铺的购物车信息
  151. actualTotal: res.actualTotal, //实际总值
  152. total: res.total, //商品总值
  153. totalCount: res.totalCount, //商品总数
  154. userAddr: res.userAddr, //地址Dto
  155. transfee: res.totalTransfee, //总运费
  156. orderReduce: res.orderReduce, //订单优惠金额(所有店铺优惠金额和使用积分抵现相加)
  157. totalScoreAmount: res.totalScoreAmount, //积分抵扣金额
  158. totalUsableScore: res.totalUsableScore, //用于抵扣的积分数量
  159. isScorePay: res.isScorePay, //用户是否选择积分抵现(0不使用 1使用 默认不使用)
  160. totalLevelAmount: res.totalLevelAmount, //等级抵扣金额
  161. freeTransfee: res.freeTransfee, //用户等级免运费金额
  162. couponIds: couponIds,
  163. maxUsableScore: res.maxUsableScore, //整个订单可以使用的积分数
  164. scorePopPlaceholder: `剩余积分${this.data.accountScore},此订单最多可用${res.maxUsableScore}`,
  165. scorePlaceholder: `使用${res.totalUsableScore}积分抵扣${res.totalScoreAmount}元`,
  166. shopUseScore: res.shopUseScore, // 店铺积分抵扣比例;
  167. isPurePoints: res.actualTotal == 0 ? true : false,
  168. });
  169. this.getDeliveryAmount()
  170. },
  171. errCallBack: res => {
  172. setTimeout(()=>{
  173. wx.showToast({
  174. title:res.data,
  175. icon: "none"
  176. })
  177. },1)
  178. wx.hideLoading();
  179. }
  180. };
  181. http.request(params);
  182. },
  183. /**
  184. * 分割优惠券成
  185. * 1. canUseCoupons 可使用优惠券列表
  186. * 2. unCanUseCoupons 不可使用优惠券列表
  187. * 3. couponAmount 选中的优惠券可优惠金额
  188. * 4. 将选中的优惠券ids保存起来
  189. * @param {*} coupons 优惠券列表
  190. * @param {*} couponIds 选中的优惠券id
  191. */
  192. splitCouponAndPushCouponIds(coupons, couponIds) {
  193. if (!coupons || !coupons.length) {
  194. return
  195. }
  196. let canUseCoupons = []
  197. let unCanUseCoupons = []
  198. let couponAmount = 0;
  199. coupons.forEach(coupon => {
  200. if (coupon.canUse) {
  201. canUseCoupons.push(coupon)
  202. } else {
  203. unCanUseCoupons.push(coupon)
  204. }
  205. if (coupon.choose) {
  206. couponIds.push(coupon.couponId)
  207. couponAmount = coupon.reduceAmount;
  208. }
  209. })
  210. return {
  211. canUseLength: canUseCoupons.length,
  212. couponAmount: couponAmount,
  213. canUseCoupons: canUseCoupons,
  214. unCanUseCoupons: unCanUseCoupons,
  215. }
  216. },
  217. /**
  218. * 提交订单校验
  219. */
  220. toPay: function () {
  221. if (this.data.actualTotal < this.data.deliveryAmount_num) {
  222. wx.showToast({
  223. title: '商品总额需大于起送价~',
  224. icon: "none"
  225. })
  226. return;
  227. }
  228. if (!this.data.userAddr) {
  229. wx.showToast({
  230. title: '请选择地址',
  231. icon: "none"
  232. })
  233. return;
  234. }
  235. if (this.data.shop_weight > 20000) {
  236. wx.showToast({
  237. title: '已超过单次配送限制20kg,请减少商品数',
  238. icon: "none"
  239. })
  240. return
  241. }
  242. if(this.data.is_distance==false){
  243. wx.showToast({
  244. title: '超出配送范围',
  245. icon: "none"
  246. })
  247. return
  248. }
  249. this.submitOrder();
  250. },
  251. /**
  252. * 获取起送价
  253. */
  254. getDeliveryAmount: function () {
  255. var params = {
  256. url: "/p/address/deliveryAmount/" + this.data.shop_id + '/' + this.data.address_id,
  257. method: "GET",
  258. callBack: res => {
  259. this.setData({
  260. deliveryAmount_num: res.deliveryAmount,
  261. is_distance:true
  262. })
  263. },
  264. errCallBack:(err)=>{
  265. this.setData({
  266. is_distance:false
  267. })
  268. }
  269. }
  270. http.request(params);
  271. },
  272. /**
  273. * 获取配送距离
  274. */
  275. // getaddress_transports: function () {
  276. // let _this = this
  277. // var params = {
  278. // url: "/p/address/transport2",
  279. // method: "GET",
  280. // data: {
  281. // shopId: this.data.shop_id,
  282. // addrId:this.data.address_id
  283. // },
  284. // callBack: function (res) {
  285. // wx.setStorageSync('DISTANCE', res.distance)
  286. // _this.setData({
  287. // })
  288. // }
  289. // };
  290. // http.request(params);
  291. // },
  292. // 提交订单
  293. submitOrder: function () {
  294. wx.showLoading({
  295. mask: true
  296. });
  297. var shopCartOrders = this.data.shopCartOrders;
  298. var orderShopParam = [];
  299. shopCartOrders.forEach(shopCart => {
  300. orderShopParam.push({
  301. remarks: shopCart.remarks,
  302. shopId: shopCart.shopId
  303. });
  304. })
  305. var params = {
  306. url: "/p/order/submit",
  307. method: "POST",
  308. data: {
  309. orderShopParam: orderShopParam,
  310. // coupons: this.data.platformCoupons,
  311. coupons: this.data.coupons,
  312. isScorePay: this.data.isScorePay,
  313. },
  314. callBack: res => {
  315. wx.hideLoading();
  316. this.calWeixinPay(res.orderNumbers);
  317. },
  318. errCallBack: res => {
  319. wx.showModal({
  320. showCancel: false,
  321. title: '提示',
  322. content: res.data
  323. })
  324. }
  325. };
  326. http.request(params);
  327. },
  328. /**
  329. * 唤起微信支付
  330. */
  331. calWeixinPay: function (orderNumbers) {
  332. wx.showLoading({
  333. mask: true
  334. });
  335. var params = {
  336. url: "/p/order/pay",
  337. method: "POST",
  338. data: {
  339. payType: 1, //支付方式 (1:微信小程序支付 2:支付宝 3微信扫码支付 4 微信h5支付)
  340. orderNumbers: orderNumbers
  341. },
  342. callBack: (res) => {
  343. wx.hideLoading();
  344. if (this.data.isPurePoints) {
  345. wx.redirectTo({
  346. url: `/pages/pay-result/pay-result?sts=1&orderNumbers=${orderNumbers}&orderType=${this.data.orderType}`,
  347. })
  348. return
  349. }
  350. wx.requestPayment({
  351. timeStamp: res.timeStamp,
  352. nonceStr: res.nonceStr,
  353. package: res.packageValue,
  354. signType: res.signType,
  355. paySign: res.paySign,
  356. success: e => {
  357. wx.redirectTo({
  358. url: `/pages/pay-result/pay-result?sts=1&orderNumbers=${orderNumbers}&orderType=${this.data.orderType}`,
  359. })
  360. },
  361. fail: err => {
  362. wx.redirectTo({
  363. url: `/pages/pay-result/pay-result?sts=0&orderNumbers=${orderNumbers}&orderType=${this.data.orderType}`,
  364. })
  365. }
  366. })
  367. }
  368. };
  369. http.request(params);
  370. },
  371. /**
  372. * 生命周期函数--监听页面初次渲染完成
  373. */
  374. onReady: function () {
  375. },
  376. /**
  377. * 生命周期函数--监听页面显示
  378. */
  379. onShow: function () {
  380. var pages = getCurrentPages();
  381. var currPage = pages[pages.length - 1];
  382. if (currPage.data.selAddress == "yes") {
  383. this.setData({ //将携带的参数赋值
  384. userAddr: currPage.data.item
  385. });
  386. }
  387. //获取订单数据
  388. this.loadOrderData();
  389. setTimeout(() => {
  390. this.getDeliveryAmount()
  391. }, 500)
  392. },
  393. /**
  394. * 生命周期函数--监听页面隐藏
  395. */
  396. onHide: function () {
  397. },
  398. /**
  399. * 生命周期函数--监听页面卸载
  400. */
  401. onUnload: function () {
  402. },
  403. /**
  404. * 页面相关事件处理函数--监听用户下拉动作
  405. */
  406. onPullDownRefresh: function () {
  407. },
  408. /**
  409. * 页面上拉触底事件的处理函数
  410. */
  411. onReachBottom: function () {
  412. },
  413. /**
  414. * 用户点击右上角分享
  415. */
  416. onShareAppMessage: function () {
  417. },
  418. // 店铺切换可用/不可用优惠券列表
  419. changeCouponSts: function (e) {
  420. this.setData({
  421. couponSts: e.currentTarget.dataset.sts
  422. });
  423. },
  424. // 店铺优惠券弹框
  425. showCouponPopup: function (e) {
  426. var index = e.currentTarget.dataset.index;
  427. var shopCartOrders = this.data.shopCartOrders;
  428. this.setData({
  429. showCoupons: index + '' === '-1' ? this.data.platformCoupons : shopCartOrders[index].shopCoupons,
  430. popupShow: true,
  431. });
  432. },
  433. closePopup: function () {
  434. this.setData({
  435. popupShow: false,
  436. // platPopupShow: false
  437. showScorePop: false
  438. });
  439. },
  440. /**
  441. * 去地址页面
  442. */
  443. toAddrListPage: function () {
  444. wx.navigateTo({
  445. url: '/pages/delivery-address/delivery-address?order=0',
  446. })
  447. },
  448. /**
  449. * 确定选择好的优惠券
  450. */
  451. choosedCoupon: function () {
  452. var couponIds = this.data.couponIds
  453. if (!this.data.showCoupons) {
  454. this.setData({
  455. popupShow: false
  456. })
  457. return
  458. }
  459. // 店铺优惠券单选操作
  460. var canUseCoupons = this.data.showCoupons.canUseCoupons;
  461. var checkedCouponId = ''
  462. for (var canUseCouponIndex in canUseCoupons) {
  463. var coupon = canUseCoupons[canUseCouponIndex];
  464. if (!coupon.choose && couponIds.indexOf(coupon.couponId) !== -1) {
  465. couponIds.splice(couponIds.indexOf(coupon.couponId), 1)
  466. }
  467. if (coupon.choose) {
  468. checkedCouponId = coupon.couponId
  469. }
  470. }
  471. couponIds.push(checkedCouponId)
  472. this.setData({
  473. couponIds: couponIds,
  474. popupShow: false
  475. });
  476. this.loadOrderData();
  477. },
  478. /**-
  479. * 优惠券子组件发过来
  480. */
  481. checkCoupon: function (e) {
  482. var showCoupons = this.data.showCoupons;
  483. // 店铺优惠券单选操作
  484. var canUseCoupons = showCoupons.canUseCoupons;
  485. for (var canUseCouponIndex in canUseCoupons) {
  486. if (e.detail.couponId === canUseCoupons[canUseCouponIndex].couponId && canUseCouponIndex == e.detail.index) {
  487. canUseCoupons[canUseCouponIndex].choose = !canUseCoupons[canUseCouponIndex].choose
  488. } else {
  489. canUseCoupons[canUseCouponIndex].choose = false
  490. }
  491. }
  492. this.setData({
  493. showCoupons: showCoupons,
  494. userChangeCoupon: 1
  495. });
  496. },
  497. /**
  498. * 输入备注
  499. */
  500. onRemarkIpt: function (e) {
  501. var index = e.currentTarget.dataset.index;
  502. var shopCartOrders = this.data.shopCartOrders;
  503. shopCartOrders[index].remarks = e.detail.value;
  504. this.setData({
  505. shopCartOrders: shopCartOrders
  506. });
  507. },
  508. /**
  509. * 积分输入弹窗显示
  510. */
  511. handleScorePop() {
  512. this.setData({
  513. showScorePop: true
  514. })
  515. },
  516. })