submit-order.js 15 KB

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