submit-order.js 14 KB

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