goodsOrderConfirm.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // pages/goodsOrderConfirm/goodsOrderConfirm.js
  2. import {
  3. get,
  4. post
  5. } from '../../utils/http'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. selectMakeupList: [], //商品列表
  12. couponList: [], //优惠券列表
  13. selectCouponList: [], //选中的优惠券
  14. showMoneyGood: 0, //原价
  15. selectCouponPrice: 0, //券后价格
  16. couponPrice: 0, //总优惠券金额
  17. inserllAmount: 0,
  18. pay_methon: 1,
  19. store_name: "",
  20. storeId: wx.getStorageSync('store_id'),
  21. page: 1,
  22. limit: 20,
  23. visible: false,
  24. payNow: false,
  25. currentCoupon: -1,
  26. coupon_id: '',
  27. orderId: '',
  28. projectData: null, // 跳转商品数据
  29. productId: null, // 产品id
  30. isShow: null, // 产品详情订单标识
  31. source: '', // 产品类型
  32. isDirectOrder: false, //直接下单
  33. listNum: 0, //订单产品总数
  34. discountList: [], //折扣列表
  35. discountSet: 0, //折扣扣除金额
  36. discount_total: 0, //折扣后总金额
  37. discount_id: '', //折扣id
  38. goods_ids: [], //商品id
  39. type: '', //直接购买还是购物车跳转
  40. CardDiscount: '', //次卡购买获取美妆折扣
  41. activity_list: [], //活动折扣列表
  42. activity_discount: '', //活动折扣优惠后的金额
  43. activity_level: false, //是否有活动折扣
  44. activity_select_money: '', //活动折扣优惠的金额
  45. exchangeList: [], //加购产品列表
  46. exchangePrice: '', //选中加购产品后的价格
  47. exchangeSelect: false, //是否选中加购产品
  48. exchangeInfo: '', //需要加购多少商品的价格信息
  49. toExchangePrice: '', //选中加购产品的总价格
  50. level: '', //用户充值折扣等级 (0是未拥有等级)
  51. levelList: {
  52. 1: '0.98',
  53. 4: '0.95',
  54. 7: '0.90',
  55. }, //折扣列表
  56. levelPrice: '', //充值等级折扣优惠价格
  57. toLevelPrice: '', //充值折扣扣除金额
  58. notSalePrice: 0, //不是秒杀产品的总价
  59. salePrice: 0, //秒杀产品总价
  60. meet_discount: 0 //满足什么折扣 0无折扣 1品牌 2充值余额折扣
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad(options) {
  66. // 获取跳转过来商品的id
  67. this.setData({
  68. goods_ids: options.goods_ids,
  69. type: options.type
  70. })
  71. let isDirectOrder = false
  72. if (options.isDirectOrder) {
  73. isDirectOrder = true
  74. }
  75. this.setData({
  76. productId: options.productId,
  77. isShow: options.isShow,
  78. source: options.source,
  79. isDirectOrder: isDirectOrder,
  80. })
  81. if (this.data.isShow) {
  82. this.getProductInfo()
  83. } else {
  84. return
  85. }
  86. },
  87. /**
  88. * 生命周期函数--监听页面初次渲染完成
  89. */
  90. onReady() {
  91. if (this.data.isDirectOrder) {
  92. this.onConfirm()
  93. } else {
  94. this.needAddGood()
  95. }
  96. this.getCoupon()
  97. if (this.data.isDirectOrder) {
  98. this.getActivityDiscount()
  99. }
  100. },
  101. /**
  102. * 生命周期函数--监听页面显示
  103. */
  104. onShow() {
  105. // 获取加购列表
  106. this.getExchangeInfo()
  107. this.setData({
  108. storeId: wx.getStorageSync('store_id'),
  109. })
  110. },
  111. // 计算品牌折扣 和 充值折扣中的最优折扣
  112. calculateDiscount() {
  113. // 如果充值等级为0折扣金额也为0
  114. if (this.data.level == 0) {
  115. this.setData({
  116. toLevelPrice: 0
  117. })
  118. }
  119. console.log(this.data.discountSet, this.data.toLevelPrice, 33333);
  120. if (this.data.discountSet == 0 && this.data.toLevelPrice == 0) {
  121. this.setData({
  122. meet_discount: 0
  123. })
  124. return
  125. }
  126. // 如果品牌折扣优惠大就用品牌
  127. if (Number(this.data.discountSet) > Number(this.data.toLevelPrice) || Number(this.data.discountSet) == Number(this.data.toLevelPrice)) {
  128. this.setData({
  129. level: 0,
  130. meet_discount: 1
  131. })
  132. } else {
  133. this.setData({
  134. ['discountList.discount_money']: 0,
  135. meet_discount: 2
  136. })
  137. }
  138. },
  139. // 获取用户充值折扣等级
  140. getLevel(sale) {
  141. console.log(sale);
  142. get('/v2/api/user/discount_level', {}, (res) => {
  143. if (res.code == 200) {
  144. if (res.data == 0 || sale == 1) {
  145. this.setData({
  146. level: 0
  147. })
  148. return
  149. }
  150. let getValue = (item, val) => {
  151. return item[val]
  152. }
  153. this.setData({
  154. // 优惠的等级
  155. level: Number(getValue(this.data.levelList, res.data)),
  156. })
  157. this.setData({
  158. // 优惠的折扣 (等级*10)
  159. tolevel: (this.data.level * 10).toFixed(1)
  160. })
  161. }
  162. // 计算充值折扣
  163. this.rechargeDiscount()
  164. })
  165. },
  166. // 计算充值折扣
  167. rechargeDiscount() {
  168. // 计算折扣
  169. this.setData({
  170. // 优惠完的价格
  171. levelPrice: (this.data.notSalePrice * this.data.level + Number(this.data.salePrice) + Number(this.data.toExchangePrice) || Number(0)).toFixed(2),
  172. })
  173. this.setData({
  174. // 优惠了多少
  175. toLevelPrice: (this.data.selectCouponPrice - this.data.levelPrice + Number(this.data.toExchangePrice) || Number(0)).toFixed(2)
  176. })
  177. },
  178. // 获取分类需要加购多少商品的价格
  179. getExchangeInfo() {
  180. post('v2/api/car/plus_project', {
  181. store_id: this.data.storeId || wx.getStorageSync('store_id'),
  182. goods_id: this.data.isDirectOrder ? this.data.goods_ids : '',
  183. type: this.data.isDirectOrder ? 2 : 1
  184. }, (res) => {
  185. if (res.code == 200) {
  186. this.setData({
  187. exchangeInfo: res.data
  188. })
  189. this.getExchangeList(res.data.category_plus_id)
  190. }
  191. })
  192. },
  193. // 获取加购列表
  194. getExchangeList(id) {
  195. post('/v2/api/car/plus_list', {
  196. store_id: this.data.storeId || wx.getStorageSync('store_id'),
  197. category_plus_id: id || '',
  198. type: this.data.isDirectOrder ? 2 : 1,
  199. goods_id: this.data.goods_ids || ''
  200. }, (res) => {
  201. if (res.code == 200) {
  202. res.data.list.forEach(item => {
  203. item.status = false
  204. })
  205. this.setData({
  206. exchangeList: res.data.list
  207. })
  208. }
  209. })
  210. },
  211. // 获取10月活动商品折扣
  212. getActivityDiscount(data) {
  213. let activity_goods_ids = []
  214. let activity_num = []
  215. if (!this.data.isDirectOrder) {
  216. data.forEach(item => {
  217. activity_goods_ids.push(item.goods_id)
  218. activity_num.push(item.num)
  219. })
  220. }
  221. post('/v2/api/car/activity_discount', {
  222. goods_ids: this.data.isDirectOrder ? this.data.goods_ids : activity_goods_ids.toString(),
  223. nums: this.data.isDirectOrder ? 1 : activity_num.toString(),
  224. store_id: this.data.storeId || wx.getStorageSync('store_id')
  225. }, (res) => {
  226. if (res.code == 200) {
  227. this.setData({
  228. activity_list: res.data,
  229. activity_discount: res.data.discount_money
  230. })
  231. let discount = '' //计算是否有折扣
  232. let activity_price = '' //计算最终付钱
  233. res.data.discount_list.forEach(item => {
  234. activity_price = Number(activity_price) + Number(item.price)
  235. discount = Number(discount) + Number(item.discount)
  236. })
  237. if (discount > 0) {
  238. this.setData({
  239. activity_level: true,
  240. activity_discount: activity_price.toFixed(2),
  241. activity_select_money: (Number(this.data.showMoneyGood) - Number(activity_price)).toFixed(2)
  242. })
  243. }
  244. }
  245. })
  246. },
  247. // 获取产品最低折扣
  248. getDiscount(goods_ids, type) {
  249. post('v2/api/car/discount', {
  250. store_id: this.data.storeId,
  251. goods_ids: goods_ids,
  252. type: type
  253. }, (res) => {
  254. if (res.code == 200) {
  255. this.setData({
  256. discountList: res.data,
  257. discount_id: res.data.id,
  258. discountSet: res.data.discount_money,
  259. })
  260. // 计算总金额
  261. this.countDiscount()
  262. // 计算品牌折扣和充值折扣最低折扣
  263. this.calculateDiscount()
  264. }
  265. })
  266. },
  267. // 计算折扣价格
  268. countDiscount() {
  269. let discountList = this.data.discountList
  270. let discount_total = this.data.discount_total
  271. discount_total = Number(this.data.showMoneyGood) - Number(discountList.discount_money) + Number(this.data.toExchangePrice)
  272. this.setData({
  273. discount_total: discount_total.toFixed(2)
  274. })
  275. },
  276. // 判断订单里的秒杀产品统计
  277. salePrice(list) {
  278. let notSalePrice = '' //非秒杀产品总价格
  279. let salePrice = '' // 秒杀产品总价格
  280. let sale = 1
  281. list.forEach(item => {
  282. if (item.sale == 0) {
  283. sale = 0
  284. return
  285. }
  286. })
  287. list.forEach(item => {
  288. if (item.sale == 0) {
  289. notSalePrice = Number(notSalePrice) + Number(item.price) * Number(item.num)
  290. } else {
  291. salePrice = Number(salePrice) + Number(item.price) * Number(item.num)
  292. }
  293. })
  294. this.setData({
  295. notSalePrice: notSalePrice,
  296. salePrice: salePrice
  297. })
  298. // 获取用户折扣等级
  299. this.getLevel(sale)
  300. },
  301. needAddGood() {
  302. let params = {
  303. store_id: this.data.storeId,
  304. page: this.data.page,
  305. limit: this.data.limit
  306. }
  307. get('v2/api/car/list', params, (res) => {
  308. if (res.code == 200) {
  309. let selectProductCarData = []
  310. selectProductCarData = selectProductCarData.concat(res.data.list)
  311. this.setData({
  312. selectMakeupList: selectProductCarData,
  313. showMoneyGood: res.data.total_money,
  314. selectCouponPrice: this.data.isShow ? this.data.projectData.is_member == 1 ? this.data.projectData.member_price : this.data.projectData.price : res.data.total_money,
  315. store_name: res.data.store_name,
  316. exchangePrice: res.data.total_money,
  317. })
  318. // 判断订单里的秒杀产品统计
  319. this.salePrice(res.data.list)
  320. // 获取活动折扣
  321. this.getActivityDiscount(selectProductCarData)
  322. // 计算充值折扣
  323. // this.rechargeDiscount()
  324. // 获取最低折扣
  325. this.getDiscount(this.data.goods_ids, this.data.type)
  326. // 计算产品总数量
  327. let num = 0
  328. selectProductCarData.map(item => {
  329. num += item.num
  330. })
  331. this.setData({
  332. listNum: num
  333. })
  334. if (this.data.selectMakeupList < res.data.count) {
  335. this.setData({
  336. page: this.data.page + 1
  337. })
  338. this.needAddGood()
  339. }
  340. }
  341. })
  342. },
  343. onConfirm() {
  344. let params = {
  345. goods_ids: this.data.productId,
  346. store_id: this.data.storeId,
  347. type: 2
  348. }
  349. post('v2/api/goods_order/confirm', params, (res) => {
  350. if (res.code == 200) {
  351. let selectProductCarData = []
  352. selectProductCarData = selectProductCarData.concat(res.data.list)
  353. this.setData({
  354. selectMakeupList: selectProductCarData,
  355. showMoneyGood: res.data.total_price,
  356. selectCouponPrice: this.data.isShow ? this.data.projectData.is_member == 1 ? this.data.projectData.member_price : this.data.projectData.price : res.data.total_money,
  357. store_name: res.data.store_name,
  358. exchangePrice: res.data.total_price
  359. })
  360. // 计算充值折扣
  361. // this.rechargeDiscount()
  362. // 获取最低折扣
  363. this.getDiscount(this.data.goods_ids, this.data.type)
  364. }
  365. })
  366. },
  367. onClosePay() {
  368. this.setData({
  369. payNow: false
  370. })
  371. },
  372. pay_methonw() {
  373. this.setData({
  374. pay_methon: 1
  375. })
  376. },
  377. pay_methonc() {
  378. this.setData({
  379. pay_methon: 2
  380. })
  381. },
  382. /**
  383. * 获取我的储值
  384. * api/user
  385. */
  386. getAmount() {
  387. get('api/user/amount', {}, (res) => {
  388. if (res.data) {
  389. this.setData({
  390. inserllAmount: res.data.amount
  391. })
  392. }
  393. })
  394. },
  395. getCoupon() {
  396. post('v2/api/car/coupon', {
  397. page: 1,
  398. limit: 50,
  399. store_id: this.storeId
  400. }, res => {
  401. if (res.code == 200) {
  402. res.data.list.forEach(item => {
  403. item["checkStatus"] = false
  404. })
  405. this.setData({
  406. couponList: res.data.list
  407. })
  408. }
  409. })
  410. },
  411. // 提交订单
  412. onCardgoodsPay() {
  413. // 计算充值折扣
  414. this.rechargeDiscount()
  415. let goods_ids = [],
  416. coupon_id = []
  417. this.data.selectMakeupList.forEach(res => {
  418. if (this.data.isDirectOrder) {
  419. goods_ids.push(res.id)
  420. } else {
  421. goods_ids.push(res.goods_id)
  422. }
  423. })
  424. this.data.selectCouponList.forEach(res => {
  425. coupon_id.push(res.id)
  426. })
  427. // 筛选添加换购产品
  428. let plus = []
  429. this.data.exchangeList.forEach(item => {
  430. let obj = {}
  431. if (item.status) {
  432. obj.plus_type = item.type
  433. obj.plus_id = item.id
  434. obj.plus_goods_id = item.plus_goods_id
  435. plus.push(obj)
  436. }
  437. })
  438. let discount_money = 0
  439. if(this.data.discountList.balance_discount>this.data.discountList.discount_money){
  440. discount_money = this.data.discountList.balance_discount
  441. }else{
  442. discount_money = this.data.discountList.discount_money
  443. }
  444. console.log(plus);
  445. let params = {
  446. goods_ids: this.data.isshow ? this.data.projectData : goods_ids.toString(),
  447. store_id: this.data.storeId,
  448. coupon_id: coupon_id.toString(),
  449. type: this.data.isShow ? 2 : 1,
  450. discount_id: this.data.discount_id || '',
  451. discount_money: discount_money,
  452. plus,
  453. meet_discount: this.data.meet_discount
  454. }
  455. post('v2/api/car/closing', params, res => {
  456. if (res.code == 200) {
  457. if (res.data.pay_status == 1) {
  458. wx.showToast({
  459. title: '支付成功',
  460. icon: 'success'
  461. })
  462. // 只有项目购买,需要订阅消息
  463. wx.reLaunch({
  464. url: `/pages/paySuccess/paySuccess?current=1&source=goods&order_id=${res.data.order_id}`
  465. })
  466. } else {
  467. this.setData({
  468. payNow: true,
  469. orderId: res.data.order_id
  470. })
  471. this.getAmount()
  472. }
  473. }
  474. })
  475. },
  476. onCouponOpen() {
  477. this.setData({
  478. visible: true
  479. })
  480. },
  481. onCancelCoupon() {
  482. this.setData({
  483. visible: false
  484. })
  485. },
  486. onCouponChange(e) {
  487. this.setData({
  488. currentCoupon: e.currentTarget.dataset.index,
  489. })
  490. this.data.couponList.forEach((res, index) => {
  491. let currentCouponItem = `couponList[${index}].checkStatus`
  492. if (e.currentTarget.dataset.index == index && e.detail.checkStatus) {
  493. this.setData({
  494. [currentCouponItem]: e.detail.checkStatus
  495. })
  496. } else {
  497. this.setData({
  498. [currentCouponItem]: false
  499. })
  500. }
  501. })
  502. },
  503. onConfirmCoupon() {
  504. let coupon_id = '',
  505. selectCouponList = [],
  506. selectCouponPrice = Number(this.data.showMoneyGood),
  507. couponPrice = 0
  508. this.data.couponList.forEach(res => {
  509. if (res.checkStatus) {
  510. selectCouponList.push(res)
  511. if (res.coupon_type == 3) {
  512. couponPrice = selectCouponPrice
  513. selectCouponPrice = 0
  514. } else {
  515. selectCouponPrice = selectCouponPrice - Number(res.price)
  516. couponPrice = couponPrice + Number(res.price)
  517. }
  518. }
  519. })
  520. if (this.data.currentCoupon != '' && this.data.couponList[this.data.currentCoupon].checkStatus) {
  521. coupon_id = this.data.couponList[this.data.currentCoupon].id
  522. }
  523. if (selectCouponPrice < 0) {
  524. selectCouponPrice = 0
  525. }
  526. this.setData({
  527. coupon_id: coupon_id,
  528. visible: false,
  529. selectCouponList: selectCouponList,
  530. selectCouponPrice: selectCouponPrice,
  531. couponPrice: couponPrice
  532. })
  533. },
  534. /**
  535. * 订单支付
  536. */
  537. payOrder() {
  538. let that = this
  539. let uri = 'v2/api/order/goods/pay'
  540. post(
  541. uri, {
  542. pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
  543. order_id: this.data.orderId
  544. },
  545. (res) => {
  546. that.setData({
  547. payNow: false
  548. })
  549. if (that.data.pay_methon == 2) {
  550. that.goToOrder('支付成功')
  551. return
  552. }
  553. wx.requestPayment({
  554. timeStamp: res.data.pay_data.timeStamp,
  555. nonceStr: res.data.pay_data.nonceStr,
  556. package: res.data.pay_data.package,
  557. signType: res.data.pay_data.signType,
  558. paySign: res.data.pay_data.paySign,
  559. success(res) {
  560. if (res.errMsg == 'requestPayment:ok') {
  561. that.goToOrder('支付成功')
  562. }
  563. },
  564. fail(res) {
  565. that.goToOrder('支付失败')
  566. }
  567. })
  568. }
  569. )
  570. },
  571. /**
  572. * 跳转订单列表
  573. */
  574. goToOrder(text) {
  575. let that = this
  576. wx.showToast({
  577. title: text,
  578. icon: text == '支付成功' ? 'success' : 'error'
  579. })
  580. if (text == '支付成功') {
  581. // 只有项目购买,需要订阅消息
  582. wx.reLaunch({
  583. url: `/pages/paySuccess/paySuccess?current=1&source=${this.data.source}&order_id=${that.data.orderId}`
  584. })
  585. // 获取下发权限
  586. wx.requestSubscribeMessage({
  587. tmplIds: ['RNife3ZhTYYpUuT26ylGQj8W0v07IPo2TGe941iR3y8'],
  588. success(res) {
  589. console.log(res);
  590. let status = ''
  591. if (res['RNife3ZhTYYpUuT26ylGQj8W0v07IPo2TGe941iR3y8'] == 'reject') {
  592. status = 2
  593. } else {
  594. status = 1
  595. }
  596. get('/v2/api/activity/push', {
  597. type: 2,
  598. status,
  599. }, res => {})
  600. },
  601. })
  602. return
  603. } else {
  604. let failUrl = `/pages/goods/orderList/orderList?current=0`
  605. setTimeout(() => {
  606. wx.reLaunch({
  607. url: failUrl
  608. })
  609. }, 1000)
  610. }
  611. },
  612. // 获取产品详情
  613. getProductInfo() {
  614. get(
  615. '/api/product/info', {
  616. store_id: wx.getStorageSync('store_id'),
  617. product_id: this.data.productId,
  618. type: 3
  619. },
  620. (res) => {
  621. this.setData({
  622. projectData: res.data,
  623. sale: res.data.sale,
  624. notSalePrice: res.data.price,
  625. selectCouponPrice:res.data.price
  626. })
  627. // 获取用户折扣等级
  628. this.getLevel(res.data.sale)
  629. },
  630. )
  631. },
  632. // 获取购买次卡享受的折扣
  633. getCardDiscount() {
  634. get('', {}, (res) => {
  635. this.setData({
  636. CardDiscount: ''
  637. })
  638. })
  639. },
  640. // 选中加购产品
  641. selectExchange(e) {
  642. let index = e.currentTarget.dataset.index
  643. let sum = 0
  644. // 修改加购产品选中状态
  645. this.setData({
  646. [`exchangeList[${index}].status`]: this.data.exchangeList[index].status ? false : true
  647. })
  648. //选中加购产品价格
  649. this.data.exchangeList[index].status ? sum = Number(this.data.exchangeList[index].price) : sum = Number(-this.data.exchangeList[index].price)
  650. // 是否选择加购产品
  651. let exchangeSelect = false
  652. this.data.exchangeList.forEach(item => {
  653. if (item.status) {
  654. exchangeSelect = true
  655. }
  656. })
  657. let exchangePrice = Number(this.data.exchangePrice) + Number(sum)
  658. this.setData({
  659. exchangeSelect: exchangeSelect,
  660. exchangePrice: exchangePrice.toFixed(2),
  661. toExchangePrice: Number(this.data.toExchangePrice) + Number(sum)
  662. })
  663. // 计算总金额折扣
  664. this.countDiscount()
  665. },
  666. })