submit-order.js 15 KB

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