cartUtils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const NET = require('../../../utils/request')
  2. const API = require('../../../config/api')
  3. import { showLoading, hideLoading } from '@/utils/plugIn/globalLoading.js'
  4. /**
  5. * 根据选中的购物车数据获取价格和过滤后的用于结算购物车的post数据
  6. * @param dataList
  7. * @return {Promise<{shopList: *[], money: number}>}
  8. */
  9. export async function getPriceBySelect(dataList) {
  10. showLoading()
  11. const addCart = []
  12. // 遍历店铺,获取选中的sku组装数据
  13. for (let i = 0; i < dataList.length; i++) {
  14. const shopObj = {}
  15. const theCurrentShop = dataList[i]
  16. shopObj.shopId = theCurrentShop.shopId
  17. shopObj.skus = []
  18. for (let j = 0; j < theCurrentShop.skus.length; j++) {
  19. const theCurrentSku = dataList[i].skus[j]
  20. // 如果是选中的才往当前店铺塞
  21. if (theCurrentSku.selected) {
  22. const skusObj = {}
  23. skusObj.ifLogistics = theCurrentSku.ifLogistics
  24. skusObj.number = theCurrentSku.number
  25. skusObj.selected = theCurrentSku.selected
  26. skusObj.skuId = theCurrentSku.skuId
  27. shopObj.skus.push(skusObj)
  28. }
  29. }
  30. // 过滤掉sku为空的店铺
  31. if (shopObj.skus.length > 0) {
  32. addCart.push(shopObj)
  33. }
  34. }
  35. try {
  36. const postData = {
  37. type: 2,
  38. shops: addCart,
  39. voucherTotalAll: 0,
  40. isVoucher: false,
  41. voucherId: 0
  42. }
  43. const res = await NET.request(API.Settlement, postData, 'POST')
  44. const money = res.data.shops.reduce((previousValue, currentValue) => previousValue + currentValue.total, 0)
  45. return {
  46. money: money.toFixed(2),
  47. shopList: addCart
  48. }
  49. } finally {
  50. hideLoading()
  51. }
  52. }
  53. /**
  54. * 根据选中的购物车数据获取购物车数量
  55. * @param dataList
  56. * @return {Promise<{allNumber: number, isAllCheck: boolean}>}
  57. */
  58. export async function getCartNumberBySelect(dataList) {
  59. let allNumber = 0; let checkNumber = 0; let
  60. isAllCheck = true
  61. // 遍历店铺
  62. for (let i = 0; i < dataList.length; i++) {
  63. // 当前店铺
  64. const shopObj = dataList[i]
  65. // 遍历店铺内部的商品
  66. for (let j = 0; j < shopObj.skus.length; j++) {
  67. const good = dataList[i].skus[j]
  68. allNumber += good.number
  69. if (good.selected === 1) {
  70. checkNumber += +good.number
  71. } else {
  72. // 如果商品没有选中,但是又勾选了全选
  73. if (isAllCheck) {
  74. isAllCheck = false
  75. }
  76. }
  77. }
  78. }
  79. // 更新缓存
  80. uni.setStorageSync('allCartNum', allNumber)
  81. // 设置导航条
  82. if (allNumber > 0) {
  83. uni.setTabBarBadge({
  84. index: 3,
  85. text: allNumber.toString()
  86. })
  87. } else {
  88. uni.removeTabBarBadge({
  89. index: 3
  90. })
  91. }
  92. return {
  93. allNumber, checkNumber, isAllCheck
  94. }
  95. }
  96. /**
  97. * 默认购物车,供骨架屏使用
  98. * @type {[{skus: [{image: string, number: number, productId: number, price: number, productName: string, selected: number},{image: string, number: number, productId: number, price: number, productName: string, selected: number},{image: string, number: number, productId: number, price: number, productName: string, selected: number}], shopName: string, shopId: number, selected: number}]}
  99. */
  100. export const defaultCartList = [
  101. {
  102. shopId: 1,
  103. shopName: '12312312321312312',
  104. selected: 0,
  105. skus: [
  106. {
  107. productId: 1,
  108. productName: '',
  109. image: '',
  110. price: 0,
  111. number: 0,
  112. selected: 0
  113. },
  114. {
  115. productId: 2,
  116. productName: '',
  117. image: '',
  118. price: 0,
  119. number: 0,
  120. selected: 0
  121. },
  122. {
  123. productId: 3,
  124. productName: '',
  125. image: '',
  126. price: 0,
  127. number: 0,
  128. selected: 0
  129. }
  130. ]
  131. }
  132. ]