submit-order.js 15 KB

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