submit-order.js 16 KB

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