submit-order.js 15 KB

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