submit-order.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. platform:1
  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. platform:1
  346. },
  347. callBack: res => {
  348. wx.hideLoading();
  349. if(res.nextPay){
  350. this.calWeixinPay(res.orderNumbers);
  351. }else{
  352. wx.redirectTo({
  353. url: `/pages/pay-result/pay-result?sts=1&orderNumbers=${res.orderNumbers}&orderType=${this.data.orderType}`,
  354. })
  355. }
  356. },
  357. errCallBack: res => {
  358. wx.showModal({
  359. showCancel: false,
  360. title: '提示',
  361. content: res.data
  362. })
  363. }
  364. };
  365. http.request(params);
  366. },
  367. /**
  368. * 唤起微信支付
  369. */
  370. calWeixinPay: function (orderNumbers) {
  371. wx.showLoading({
  372. mask: true
  373. });
  374. var params = {
  375. url: "/p/order/pay",
  376. method: "POST",
  377. data: {
  378. payType: 1, //支付方式 (1:微信小程序支付 2:支付宝 3微信扫码支付 4 微信h5支付)
  379. orderNumbers: orderNumbers
  380. },
  381. callBack: (res) => {
  382. wx.hideLoading();
  383. if (this.data.isPurePoints) {
  384. wx.redirectTo({
  385. url: `/pages/pay-result/pay-result?sts=1&orderNumbers=${orderNumbers}&orderType=${this.data.orderType}`,
  386. })
  387. return
  388. }
  389. wx.requestPayment({
  390. timeStamp: res.timeStamp,
  391. nonceStr: res.nonceStr,
  392. package: res.package,
  393. signType: res.signType,
  394. paySign: res.paySign,
  395. success: e => {
  396. console.log('成功');
  397. wx.redirectTo({
  398. url: `/pages/pay-result/pay-result?sts=1&orderNumbers=${orderNumbers}&orderType=${this.data.orderType}`,
  399. })
  400. },
  401. fail: err => {
  402. console.log('失败',err);
  403. wx.redirectTo({
  404. url: `/pages/pay-result/pay-result?sts=0&orderNumbers=${orderNumbers}&orderType=${this.data.orderType}`,
  405. })
  406. }
  407. })
  408. }
  409. };
  410. http.request(params);
  411. },
  412. /**
  413. * 生命周期函数--监听页面初次渲染完成
  414. */
  415. onReady: function () {
  416. },
  417. /**
  418. * 生命周期函数--监听页面显示
  419. */
  420. onShow: function () {
  421. var pages = getCurrentPages();
  422. var currPage = pages[pages.length - 1];
  423. if (currPage.data.selAddress == "yes") {
  424. this.setData({ //将携带的参数赋值
  425. userAddr: currPage.data.item
  426. });
  427. }
  428. //获取订单数据
  429. this.loadOrderData();
  430. setTimeout(() => {
  431. this.getDeliveryAmount()
  432. }, 500)
  433. },
  434. /**
  435. * 生命周期函数--监听页面隐藏
  436. */
  437. onHide: function () {
  438. },
  439. /**
  440. * 生命周期函数--监听页面卸载
  441. */
  442. onUnload: function () {
  443. },
  444. /**
  445. * 页面相关事件处理函数--监听用户下拉动作
  446. */
  447. onPullDownRefresh: function () {
  448. },
  449. /**
  450. * 页面上拉触底事件的处理函数
  451. */
  452. onReachBottom: function () {
  453. },
  454. /**
  455. * 用户点击右上角分享
  456. */
  457. onShareAppMessage: function () {
  458. },
  459. // 店铺切换可用/不可用优惠券列表
  460. changeCouponSts: function (e) {
  461. this.setData({
  462. couponSts: e.currentTarget.dataset.sts
  463. });
  464. },
  465. // 店铺优惠券弹框
  466. showCouponPopup: function (e) {
  467. var index = e.currentTarget.dataset.index;
  468. var shopCartOrders = this.data.shopCartOrders;
  469. this.setData({
  470. showCoupons: index + '' === '-1' ? this.data.platformCoupons : shopCartOrders[index].shopCoupons,
  471. popupShow: true,
  472. });
  473. },
  474. closePopup: function () {
  475. this.setData({
  476. popupShow: false,
  477. // platPopupShow: false
  478. showScorePop: false
  479. });
  480. },
  481. /**
  482. * 去地址页面
  483. */
  484. toAddrListPage: function () {
  485. wx.navigateTo({
  486. url: '/pages/delivery-address/delivery-address?order=0',
  487. })
  488. },
  489. /**
  490. * 确定选择好的优惠券
  491. */
  492. choosedCoupon: function () {
  493. var couponIds = this.data.couponIds
  494. if (!this.data.showCoupons) {
  495. this.setData({
  496. popupShow: false
  497. })
  498. return
  499. }
  500. // 店铺优惠券单选操作
  501. var canUseCoupons = this.data.showCoupons.canUseCoupons;
  502. var checkedCouponId = ''
  503. for (var canUseCouponIndex in canUseCoupons) {
  504. var coupon = canUseCoupons[canUseCouponIndex];
  505. if (!coupon.choose && couponIds.indexOf(coupon.couponId) !== -1) {
  506. couponIds.splice(couponIds.indexOf(coupon.couponId), 1)
  507. }
  508. if (coupon.choose) {
  509. checkedCouponId = coupon.couponId
  510. }
  511. }
  512. couponIds.push(checkedCouponId)
  513. this.setData({
  514. couponIds: couponIds,
  515. popupShow: false
  516. });
  517. this.loadOrderData();
  518. },
  519. /**-
  520. * 优惠券子组件发过来
  521. */
  522. checkCoupon: function (e) {
  523. var showCoupons = this.data.showCoupons;
  524. // 店铺优惠券单选操作
  525. var canUseCoupons = showCoupons.canUseCoupons;
  526. for (var canUseCouponIndex in canUseCoupons) {
  527. if (e.detail.couponId === canUseCoupons[canUseCouponIndex].couponId && canUseCouponIndex == e.detail.index) {
  528. canUseCoupons[canUseCouponIndex].choose = !canUseCoupons[canUseCouponIndex].choose
  529. } else {
  530. canUseCoupons[canUseCouponIndex].choose = false
  531. }
  532. }
  533. this.setData({
  534. showCoupons: showCoupons,
  535. userChangeCoupon: 1
  536. });
  537. },
  538. /**
  539. * 输入备注
  540. */
  541. onRemarkIpt: function (e) {
  542. var index = e.currentTarget.dataset.index;
  543. var shopCartOrders = this.data.shopCartOrders;
  544. shopCartOrders[index].remarks = e.detail.value;
  545. this.setData({
  546. shopCartOrders: shopCartOrders
  547. });
  548. },
  549. /**
  550. * 积分输入弹窗显示
  551. */
  552. handleScorePop() {
  553. this.setData({
  554. showScorePop: true
  555. })
  556. },
  557. })