confirmOrder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. const app = getApp()
  2. const api = require('../../../../../request/api');
  3. const http = require('../../../../../request/http');
  4. const utils = require('../../../../../utils/util.js');
  5. const storeKeys = require('../../../../../utils/storageKeys.js');
  6. Page({
  7. data: {
  8. isIphoneX: app.globalData.isIphoneX,
  9. isLoading: true, //是否处于加载
  10. mode: 'buyNow', //页面来源
  11. dataParams: {}, //参数对象
  12. model_addr: false,
  13. goodsObj: {}, //商品返回数据
  14. addressId: '', //地址id
  15. isDisabled: false, //是否禁用按钮
  16. remark: '', //备注
  17. orderId: '',
  18. buyNumber: 1, //购买数量
  19. buySelfNumber: 1, //自提购买数量
  20. cardModel: false,
  21. riceList: [],
  22. riceCardId: 0, //确认选中的礼品卡
  23. riceCardCheckItem: {}, //选择使用的礼品卡
  24. isCommit: false,
  25. orderType: 10, //配送方式 默认10 10-快递配送 20-门店自提
  26. join_id: 0, //拼团id
  27. formeType: 1, //1:拼团 2:裂变拼团
  28. couponModel: false, //是否显示优惠卷弹窗
  29. couponList: [], //优惠卷列表
  30. couponId: 0, //确认选中的优惠卷id
  31. couponCheckItem: {}, //确认选中的优惠卷
  32. couponName: '', //确认选中的优惠卷名字
  33. discountModel: false, //是否显示优惠明细弹窗
  34. kimmyModel: false, //金米粒抵扣弹窗
  35. isUseGoldRice: -1, //是否使用金米粒 -1禁用 0不使用 1使用
  36. },
  37. onLoad: function (options) {
  38. if (options.data) {
  39. let dataObj = JSON.parse(options.data)
  40. // let dataObj = {
  41. // activityId: 61,
  42. // addressId: 0,
  43. // delivery: 10,
  44. // goodsId: 10075,
  45. // goodsNum: 1,
  46. // goodsSkuId: 0,
  47. // mode: "groupbuy"
  48. // }
  49. if(dataObj.mode==='groupbuylb'){
  50. this.data.formeType = 2
  51. }
  52. console.log(dataObj)
  53. this.setData({
  54. dataParams: dataObj,
  55. mode: dataObj.mode,
  56. buyNumber: dataObj.goodsNum,
  57. buySelfNumber: dataObj.goodsNum,
  58. })
  59. }
  60. },
  61. //切换配送方式
  62. changeTitleTab(e) {
  63. let index = e.currentTarget.dataset.index;
  64. if (index == this.data.orderType) {
  65. return;
  66. }
  67. this.setData({
  68. orderType: index,
  69. ['dataParams.goodsNum']: index == 10 ? this.data.buyNumber : this.data.buySelfNumber
  70. })
  71. this.getData();
  72. },
  73. //增加/删减购买数量 线上物流
  74. changeGoodsNumber: utils.throttle(function (e) {
  75. let state = e.currentTarget.dataset.state;
  76. let item = e.currentTarget.dataset.item;
  77. let buyNumber = this.data.buyNumber;
  78. // console.log(item)
  79. // state:1增加 0删减
  80. if (state == 1) {
  81. if (item.skuInfo.stock_num <= 0) {
  82. utils.toast("库存不足")
  83. return;
  84. }
  85. if (buyNumber >= item.skuInfo.stock_num) {
  86. utils.toast("不能再加了,达到商品最大库存了")
  87. return;
  88. }
  89. if (buyNumber >= item.limit_mount) {
  90. let limit_mount = item.limit_mount
  91. utils.toast("每人限购"+limit_mount+"件哦~")
  92. return;
  93. }
  94. buyNumber++
  95. } else {
  96. if (buyNumber <= 1) {
  97. utils.toast("不能再减了")
  98. return;
  99. }
  100. buyNumber--
  101. }
  102. this.setData({
  103. buyNumber,
  104. ['dataParams.goodsNum']: buyNumber
  105. })
  106. this.getData()
  107. }, 500),
  108. //增加/删减购买数量 门店自提
  109. changeGoodsSelfNumber: utils.throttle(function (e) {
  110. let state = e.currentTarget.dataset.state;
  111. let item = e.currentTarget.dataset.item;
  112. let buySelfNumber = this.data.buySelfNumber;
  113. if (state == 1) {
  114. if (item.skuInfo.stock_num <= 0) {
  115. utils.toast("库存不足")
  116. return;
  117. }
  118. if (buySelfNumber >= item.skuInfo.stock_num) {
  119. utils.toast("不能再加了,达到商品最大库存了")
  120. return;
  121. }
  122. if (buySelfNumber >= item.limit_mount) {
  123. utils.toast("不能再加了,达到商品最大购买量了")
  124. return;
  125. }
  126. buySelfNumber++
  127. } else {
  128. if (buySelfNumber <= 1) {
  129. utils.toast("不能再减了")
  130. return;
  131. }
  132. buySelfNumber--
  133. }
  134. this.setData({
  135. buySelfNumber,
  136. ['dataParams.goodsNum']: buySelfNumber
  137. })
  138. this.getData()
  139. }, 500),
  140. //监听商品数量输入 线上物流
  141. changeBuyNumber(e) {
  142. let buyNumber = e.detail.value.replace(/\s+/g, '');
  143. this.setData({
  144. buyNumber,
  145. ['dataParams.goodsNum']: buyNumber
  146. })
  147. },
  148. //监听商品数量输入 门店自提
  149. changeBuySelfNumber(e) {
  150. let buySelfNumber = e.detail.value.replace(/\s+/g, '');
  151. this.setData({
  152. buySelfNumber,
  153. ['dataParams.goodsNum']: buySelfNumber
  154. })
  155. },
  156. //商品数量输入失去焦点 线上物流
  157. numberBlur(e) {
  158. //输入的数量大于库存数时
  159. let item = e.currentTarget.dataset.item,
  160. that = this;
  161. if (parseInt(this.data.buyNumber) > parseInt(item.skuInfo.stock_num) || parseInt(this.data.buyNumber) > parseInt(item.limit_mount)) {
  162. let minNum = Math.min(parseInt(item.skuInfo.stock_num), parseInt(item.limit_mount))
  163. this.setData({
  164. buyNumber: minNum,
  165. ['dataParams.goodsNum']: minNum
  166. })
  167. if (item.skuInfo.stock_num == 0) {
  168. utils.toast("库存不足")
  169. return
  170. }
  171. }
  172. setTimeout(() => {
  173. that.getData()
  174. }, 200)
  175. },
  176. //商品数量输入失去焦点 门店自提
  177. selfnumberBlur(e) {
  178. //输入的数量大于库存数时
  179. let item = e.currentTarget.dataset.item,
  180. that = this;
  181. if (parseInt(this.data.buySelfNumber) > parseInt(item.skuInfo.stock_num) || parseInt(this.data.buySelfNumber) > parseInt(item.limit_mount)) {
  182. let minNum = Math.min(parseInt(item.skuInfo.stock_num), parseInt(item.limit_mount))
  183. this.setData({
  184. buySelfNumber: minNum,
  185. ['dataParams.goodsNum']: minNum
  186. })
  187. }
  188. setTimeout(() => {
  189. that.getData()
  190. }, 200)
  191. },
  192. toAddr() {
  193. wx.navigateTo({
  194. url: "/pages/user/pages/addressList/addressList?sourceState=1"
  195. })
  196. },
  197. detailInput: function (e) {
  198. this.setData({
  199. remark: e.detail.value
  200. })
  201. },
  202. //获取订单列表
  203. getData() {
  204. let that = this;
  205. wx.showLoading({
  206. title: "加载中",
  207. mask: true
  208. });
  209. let url = '',
  210. dataParams = this.data.dataParams
  211. if (!dataParams.goodsSkuId) {
  212. dataParams.goodsSkuId = 0
  213. }
  214. //本地分享人id 107446
  215. let staffUserId = dataParams.joinId || utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || "";
  216. // if(this.data.isUseGoldRice == -1){
  217. // this.data.isUseGoldRice = 1;
  218. // }
  219. // 推广监控ID
  220. let promotionMonitorId = utils.getStorageSync(storeKeys.SOURECID) || 0;
  221. url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId +
  222. '&goodsNum=' + dataParams.goodsNum + '&addressId=' + this.data.addressId + '&riceCardId=' + this.data.riceCardId +
  223. '&delivery=' + this.data.orderType +"&promotionMonitorId="+promotionMonitorId
  224. if (dataParams.joinId) {
  225. url += '&joinId=' + staffUserId
  226. } else {
  227. url += '&activityId=' + dataParams.activityId
  228. }
  229. console.log(url);
  230. http.request({
  231. url: api.URL + url,
  232. method: 'GET',
  233. token: utils.getStorageSync(storeKeys.TOKEN),
  234. success: (res) => {
  235. console.log(res)
  236. wx.hideLoading();
  237. let orderObj = res.data.data.order;
  238. if (this.data.orderType == 20 && !orderObj.isPickup) {
  239. this.setData({
  240. orderType: 10,
  241. ['goodsObj.isPickup']: false
  242. })
  243. return;
  244. }
  245. //来源直接购买 切换时库存大于输入库存时 重新赋值输入的值
  246. if (this.data.mode == "buyNow") {
  247. let goodsList = orderObj.goodsList || []
  248. let buyNumber = this.data.buyNumber;
  249. let buySelfNumber = this.data.buySelfNumber;
  250. for (let i = 0; i < goodsList.length; i++) {
  251. let item = goodsList[i];
  252. if (!item.stock_enough) {
  253. // 商品类型,10普通商品;20赠品;30套餐商品
  254. if (item.goods_type != 20) {
  255. if (this.data.orderType == 20) {
  256. this.setData({
  257. buySelfNumber: item.skuInfo.stock_num || buySelfNumber
  258. })
  259. } else {
  260. this.setData({
  261. buyNumber: item.skuInfo.stock_num || buyNumber
  262. })
  263. }
  264. this.setData({
  265. ['dataParams.goodsNum']: item.skuInfo.stock_num
  266. })
  267. }
  268. break;
  269. }
  270. }
  271. }
  272. //选中的优惠价id
  273. let couponId = res.data.data.order.couponId;
  274. let couponCheckItem = {
  275. couponId
  276. };
  277. //选中的礼品卡id
  278. let riceCardId = res.data.data.order.riceCardId;
  279. let riceCardCheckItem = {
  280. id: riceCardId
  281. }
  282. this.setData({
  283. goodsObj: res.data.data.order,
  284. // couponName:res.data.data.order.couponName,
  285. // couponId,
  286. riceCardId,
  287. couponCheckItem,
  288. riceCardCheckItem,
  289. isLoading: false,
  290. riceList: res.data.data.order.userRiceCardList,
  291. // couponList: res.data.data.order.couponList,
  292. couponPrice: utils.toDecimal2(parseFloat(res.data.data.order.orderTotalPrice) - parseFloat(res.data.data.order.orderTotalGroupPrice))
  293. })
  294. // if(!orderObj.address || orderObj.errorStatus==423 || orderObj.hasError || Number(orderObj.orderPayPrice)<=0 ){
  295. // (!orderObj.address && this.data.orderType == 10) ||
  296. if (orderObj.errorStatus == 423 || orderObj.hasError) {
  297. this.setData({
  298. isDisabled: true
  299. })
  300. } else {
  301. this.setData({
  302. isDisabled: false
  303. })
  304. }
  305. // if (orderObj.errorStatus == 422) {
  306. // this.setData({
  307. // isDisabled: false
  308. // })
  309. // utils.toast(orderObj.errorMsg, 2000)
  310. // }
  311. if (orderObj.errorStatus == 423) {
  312. this.setData({
  313. model_addr: true
  314. })
  315. } else {
  316. this.setData({
  317. model_addr: false
  318. })
  319. }
  320. },
  321. error: (res) => {
  322. // console.log("errr");
  323. // this.setData({
  324. // buySelfNumber: item.skuInfo.stock_num || buySelfNumber,
  325. // buyNumber: item.skuInfo.stock_num || buyNumber
  326. // })
  327. // if (this.data.goodsObj) {
  328. // let goodsList = this.data.goodsObj.goodsList || []
  329. // let buyNumber = this.data.buyNumber;
  330. // let buySelfNumber = this.data.buySelfNumber;
  331. // for (let i = 0; i < goodsList.length; i++) {
  332. // let item = goodsList[i];
  333. // if (!item.stock_enough) {
  334. // // 商品类型,10普通商品;20赠品;30套餐商品
  335. // if(item.goods_type != 20){
  336. // if(this.data.orderType == 20){
  337. // this.setData({
  338. // buySelfNumber: item.skuInfo.stock_num || buySelfNumber
  339. // })
  340. // }else{
  341. // this.setData({
  342. // buyNumber: item.skuInfo.stock_num || buyNumber
  343. // })
  344. // }
  345. // this.setData({
  346. // ['dataParams.goodsNum']: item.skuInfo.stock_num
  347. // })
  348. // }
  349. // break;
  350. // }
  351. // }
  352. // }
  353. },
  354. })
  355. },
  356. // 清除无货商品
  357. cleanInvalidGoods: utils.throttle(function (e) {
  358. let invalidGoodsList = this.data.goodsObj.invalidGoodsList || [];
  359. console.log(invalidGoodsList)
  360. let cartIds = [];
  361. invalidGoodsList && invalidGoodsList.forEach((item) => {
  362. cartIds.push(item.cart_id);
  363. })
  364. console.log(cartIds)
  365. let data = {
  366. cartIds
  367. }
  368. wx.showLoading({
  369. title: "加载中"
  370. });
  371. http.request({
  372. url: api.URL + '/api/cart/delete',
  373. method: 'POST',
  374. data,
  375. token: utils.getStorageSync(storeKeys.TOKEN),
  376. success: (res) => {
  377. this.getData();
  378. },
  379. error: (res) => {
  380. wx.hideLoading();
  381. utils.toast(res.data.message)
  382. },
  383. })
  384. }),
  385. clickModel() {
  386. this.setData({
  387. model_addr: false
  388. })
  389. },
  390. commit: utils.throttle(function (e) {
  391. if (this.data.orderType == 20 && this.data.goodsObj.invalidGoodsList && this.data.goodsObj.invalidGoodsList.length > 0) {
  392. utils.toast("请先清除无货商品!")
  393. return;
  394. }
  395. let that = this;
  396. if (this.data.orderType == 10 && !this.data.goodsObj.address) {
  397. utils.toast("请先填写收货地址")
  398. return;
  399. }
  400. // if (this.data.goodsObj.errorStatus == 422) {
  401. // utils.toast(this.data.goodsObj.errorMsg, 2000)
  402. // return
  403. // }
  404. wx.showLoading({
  405. title: "加载中"
  406. });
  407. let dataParams = this.data.dataParams
  408. //本地分享人id
  409. let staffUserId = dataParams.joinId || "";
  410. dataParams.addressId = this.data.addressId
  411. dataParams.remark = this.data.remark
  412. dataParams.riceCardId = this.data.riceCardId
  413. // dataParams.couponId = this.data.couponId
  414. dataParams.delivery = this.data.orderType
  415. // dataParams.isUseGoldRice=this.data.isUseGoldRice
  416. dataParams.staffUserId = utils.getStorageSync(storeKeys.SHAREID)
  417. if (!dataParams.goodsSkuId) {
  418. dataParams.goodsSkuId = 0
  419. }
  420. if (dataParams.joinId) {
  421. dataParams.joinId = staffUserId
  422. }
  423. console.log(dataParams)
  424. console.log("request params:" + JSON.stringify(dataParams));
  425. // if(this.data.mode=='cart'){
  426. // dataParams.cartIds = dataParams.goodsId
  427. // delete dataParams.goodsId
  428. // }
  429. // 推广监控ID
  430. let promotionMonitorId = utils.getStorageSync(storeKeys.SOURECID)
  431. console.log("下单监控id"+promotionMonitorId)
  432. if(promotionMonitorId){
  433. dataParams.promotionMonitorId = promotionMonitorId
  434. }
  435. console.log(dataParams)
  436. http.request({
  437. url: api.URL + '/api/checkout/order',
  438. method: 'POST',
  439. token: utils.getStorageSync(storeKeys.TOKEN),
  440. data: dataParams,
  441. success: (res) => {
  442. wx.hideLoading();
  443. let orderId = res.data.data.order_id
  444. that.data.join_id = res.data.data.extra?res.data.data.extra.join_id:0
  445. if (res.data.data.pay_type == 20) {
  446. that.setData({
  447. isCommit: true
  448. })
  449. that.wxPayment(res.data.data.payment, orderId);
  450. }
  451. if (res.data.data.pay_type == 30) {
  452. utils.toast('支付成功');
  453. let formeType = that.data.formeType
  454. console.log(formeType)
  455. // app.getCartNumber();
  456. wx.reLaunch({
  457. url: "/pages/cart/pages/paySuccess/paySuccess?orderType=" + that.data.orderType + "&type="+formeType + "&joinId="+ that.data.join_id
  458. })
  459. }
  460. },
  461. error: (res) => {
  462. console.log(res)
  463. that.setData({
  464. isDisabled: true
  465. })
  466. },
  467. })
  468. }, 800),
  469. //调起微信支付
  470. wxPayment(data, orderId) {
  471. let _this = this;
  472. let dataParams = this.data.dataParams
  473. //本地分享人id
  474. let staffUserId = dataParams.joinId || "";
  475. wx.requestPayment({
  476. nonceStr: data.nonceStr,
  477. package: 'prepay_id=' + data.prepay_id,
  478. paySign: data.paySign,
  479. signType: "MD5",
  480. timeStamp: data.timeStamp,
  481. success(res) {
  482. if (res.errMsg == "requestPayment:ok") {
  483. // app.getCartNumber();
  484. utils.toast('支付成功');
  485. let formeType = _this.data.formeType
  486. console.log(formeType)
  487. wx.reLaunch({
  488. url: "/pages/cart/pages/paySuccess/paySuccess?orderType=" + _this.data.orderType+ "&type="+formeType +"&joinId="+ _this.data.join_id
  489. })
  490. }
  491. },
  492. fail(res) {
  493. if (res.errMsg == "requestPayment:fail cancel") {
  494. // app.getCartNumber();
  495. utils.toast('已取消支付');
  496. // wx.reLaunch({
  497. // url: "/pages/cart/pages/noPayment/noPayment?orderId=" + orderId
  498. // })
  499. http.request({
  500. url: api.URL + '/api/groupbuy.Activity/cancel',
  501. method: 'POST',
  502. token: utils.getStorageSync(storeKeys.TOKEN),
  503. data: {
  504. "order_id": orderId
  505. },
  506. success: (res) => {},
  507. error: (res) => {}
  508. })
  509. }
  510. }
  511. })
  512. },
  513. // 选择礼品卡
  514. toCheckRiceCard() {
  515. this.setData({
  516. cardModel: true
  517. })
  518. },
  519. hideModel() {
  520. this.setData({
  521. cardModel: false
  522. })
  523. },
  524. checkRiceCard(e) {
  525. let item0 = {
  526. id: -1
  527. }
  528. let item = e.currentTarget.dataset.item || item0;
  529. console.log(item)
  530. this.setData({
  531. riceCardCheckItem: item
  532. })
  533. },
  534. confirmRiceCard() {
  535. console.log(this.data.riceCardCheckItem.id)
  536. if (!this.data.riceCardCheckItem.id) {
  537. utils.toast("请选择要使用的礼品卡")
  538. return;
  539. }
  540. this.setData({
  541. cardModel: false,
  542. riceCardId: this.data.riceCardCheckItem.id
  543. })
  544. this.getData()
  545. },
  546. //选择优惠卷
  547. checkCoupon(e) {
  548. let couponId = e.currentTarget.dataset.id || -1;
  549. let couponCheckItem = {
  550. couponId
  551. }
  552. this.setData({
  553. couponCheckItem
  554. })
  555. },
  556. //确认优惠卷
  557. confirmCoupon() {
  558. if (!this.data.couponCheckItem.couponId) {
  559. utils.toast("请选择要使用的优惠券")
  560. return;
  561. }
  562. this.setData({
  563. couponModel: false,
  564. couponId: this.data.couponCheckItem.couponId
  565. })
  566. this.getData()
  567. },
  568. onShow: function () {
  569. if (this.data.isCommit) {
  570. return;
  571. }
  572. this.getData()
  573. },
  574. //弹出选择优惠卷
  575. couponModel() {
  576. if (!this.data.couponList || this.data.couponList.length <= 0) {
  577. return;
  578. }
  579. this.setData({
  580. couponModel: true
  581. })
  582. },
  583. //隐藏选择优惠卷
  584. hideCouponModel() {
  585. this.setData({
  586. couponModel: false
  587. })
  588. },
  589. //取消下单
  590. cancel: utils.throttle(function (e) {
  591. wx.navigateBack({
  592. delta: 1
  593. })
  594. }),
  595. //显示优惠详情
  596. showDiscountModel() {
  597. this.setData({
  598. discountModel: true
  599. })
  600. },
  601. //隐藏优惠详情
  602. hideDiscountModel() {
  603. this.setData({
  604. discountModel: false
  605. })
  606. },
  607. //显示选择金米粒
  608. showKimmyModel() {
  609. this.setData({
  610. kimmyModel: true,
  611. isUseGoldRice: this.data.goodsObj.isUseGoldRice
  612. })
  613. },
  614. //隐藏选择金米粒
  615. hideKimmyModel() {
  616. this.setData({
  617. kimmyModel: false
  618. })
  619. },
  620. //切换选择金米粒
  621. checkKimmy(e) {
  622. //禁用金米粒
  623. if (this.data.goodsObj.isUseGoldRice == -1) {
  624. return;
  625. }
  626. let isUseGoldRice = e.currentTarget.dataset.id || 0;
  627. this.setData({
  628. isUseGoldRice
  629. })
  630. },
  631. //确认金米粒
  632. confirmKimmy() {
  633. this.setData({
  634. kimmyModel: false
  635. })
  636. //禁用金米粒
  637. if (this.data.goodsObj.isUseGoldRice == -1) {
  638. return;
  639. }
  640. console.log(this.data.isUseGoldRice)
  641. this.getData()
  642. },
  643. })