submit-order.js 15 KB

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