cart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <template>
  2. <view class="cart">
  3. <tui-skeleton v-if="isRequest" :loadingType="3" :isLoading="true"></tui-skeleton>
  4. <!-- 购物车列表为空 -->
  5. <template v-if="shopList.length === 0 && expiredProducts.length === 0">
  6. <tui-no-data :imgUrl="staticUrl + 'icon-empty-cart.png'" :imgHeight="230" :imgWidth="290">
  7. <view class="empty-tip">购物车空空的,快去逛逛吧~</view>
  8. </tui-no-data>
  9. </template>
  10. <template v-else>
  11. <!-- 顶部 -->
  12. <template v-if="!isDeleted">
  13. <view class="sticky-top">
  14. <view class="goods-total">
  15. <view class="total">共{{ kindCount }}件商品</view>
  16. <tui-button
  17. :size="24"
  18. width="88rpx"
  19. height="42rpx"
  20. shape="circle"
  21. :plain="true"
  22. type="base"
  23. @click="isDeleted = true"
  24. >
  25. 删除
  26. </tui-button>
  27. </view>
  28. <view class="receive-coupon">
  29. <view class="tip-text" v-text="couponTipText"></view>
  30. <tui-button
  31. :size="24"
  32. width="88rpx"
  33. height="42rpx"
  34. shape="circle"
  35. type="base"
  36. @click="visiable = true"
  37. >
  38. 领券
  39. </tui-button>
  40. </view>
  41. </view>
  42. </template>
  43. <view class="list-content">
  44. <!-- 有效商品 -->
  45. <view class="supplier-area" v-for="item in shopList" :key="item.shopId">
  46. <cm-cart-supplier-area
  47. :shopInfo="item"
  48. @change="checkedProductChange"
  49. @countChange="onCountChange"
  50. ref="supplierArea"
  51. ></cm-cart-supplier-area>
  52. </view>
  53. <!-- 失效商品 -->
  54. <view class="supplier-area" v-if="expiredProducts.length > 0">
  55. <cm-cart-expired-area :expiredList="expiredProducts" @clear="onClearExp"></cm-cart-expired-area>
  56. </view>
  57. </view>
  58. <view style="height: 100rpx;"></view>
  59. <!-- 购物车导航 -->
  60. <cm-cart-navbar
  61. :isCheckedAll="isChekedAll"
  62. :isDeleted="isDeleted"
  63. :data="navbarData"
  64. @all="onCheckedAll"
  65. @submit="onSumit"
  66. @cancel="isDeleted = false"
  67. @remove="removeModal = true"
  68. ></cm-cart-navbar>
  69. </template>
  70. <!-- 优惠券列表 -->
  71. <cm-coupon-popup
  72. :visiable="visiable"
  73. :list="couponList"
  74. :couponTabs="couponTabs"
  75. :hasTabs="true"
  76. :hasConfirm="false"
  77. :hasSafeArea="false"
  78. @close="visiable = false"
  79. @change="onCouponChange"
  80. @couponClick="onCouponClick"
  81. ></cm-coupon-popup>
  82. <!-- 确认弹框 -->
  83. <tui-modal :show="removeModal" content="确认删除选中的商品吗?" @click="onConfirmRemove"></tui-modal>
  84. <!-- 清空失效商品确认 -->
  85. <tui-modal :show="removeExpModal" content="确认清空失效商品?" @click="onConfirmRemoveExp"></tui-modal>
  86. </view>
  87. </template>
  88. <script>
  89. import { fetchCartInfo } from '@/services/api/cart.js'
  90. import { fetchCouponListByProductIds } from '@/services/api/coupon.js'
  91. import { arrayUnique } from '@/common/utils.js'
  92. import { mapGetters, mapActions } from 'vuex'
  93. import CouponUtils from '@/common/couponUtils.js'
  94. // 业务帮助函数
  95. import {
  96. totalAllCheckedProduct,
  97. computeTotalPrice,
  98. initFormatCouponList,
  99. makeCouponUseTip
  100. } from '@/common/business.helper.js'
  101. const resetData = () => ({
  102. isRequest: true,
  103. isDeleted: false,
  104. expiredProducts: [], // 失效商品列表
  105. shopList: [], // 供应商&&商品列表
  106. checkedShopMap: {},
  107. isChekedAll: false,
  108. removeModal: false,
  109. removeExpModal: false,
  110. // 商品价格
  111. checkedProductList: [],
  112. allPrice: 0, // 商品总价
  113. // 优惠券列表
  114. visiable: false,
  115. currentTab: 0,
  116. receiveCouponList: [], // 已领取优惠券
  117. ableCouponList: [], // 可领取优惠券
  118. couponTabs: [],
  119. currentCoupon: null,
  120. nextCoupon: null,
  121. couponTipText: '', // 可用优惠券提示
  122. // 保存变动
  123. changeRef: null
  124. })
  125. export default {
  126. data() {
  127. return resetData()
  128. },
  129. computed: {
  130. ...mapGetters(['userId', 'kindCount']),
  131. couponList() {
  132. return this.currentTab === 0 ? this.receiveCouponList : this.ableCouponList
  133. },
  134. navbarData() {
  135. const result = {
  136. finallyPrice: 0, // 实际应付
  137. couponAmount: 0,
  138. allPrice: 0,
  139. discountedPrice: 0,
  140. count: 0
  141. }
  142. if (this.currentCoupon) {
  143. result.couponAmount = this.currentCoupon.couponAmount
  144. result.discountedPrice = this.currentCoupon.couponAmount
  145. }
  146. result.allPrice = this.allPrice
  147. if (this.allPrice - result.discountedPrice > 0) {
  148. result.finallyPrice = this.allPrice - result.couponAmount
  149. }
  150. result.count = this.checkedProductList.length
  151. return result
  152. }
  153. },
  154. onPullDownRefresh() {
  155. this.resetData()
  156. this.fetchCartInfo()
  157. },
  158. onShow() {
  159. this.resetData()
  160. this.fetchCartInfo()
  161. this.fetchCartKindCount()
  162. },
  163. methods: {
  164. ...mapActions('cart', ['removeFromCart', 'fetchCartKindCount']),
  165. // 初始化数据
  166. resetData() {
  167. const data = resetData()
  168. for (let key in data) {
  169. this[key] = data[key]
  170. }
  171. },
  172. // 清空失效商品
  173. onClearExp() {
  174. this.removeExpModal = true
  175. },
  176. // 确认清空失效商品
  177. async onConfirmRemoveExp(e) {
  178. // removeProductFromCart
  179. if (!e.index) return
  180. try {
  181. await this.removeFromCart(this.expiredProducts.map(product => product.cartId))
  182. this.resetData()
  183. this.fetchCartInfo()
  184. } catch (e) {
  185. console.log('删除失效商品失败')
  186. } finally {
  187. this.removeExpModal = false
  188. }
  189. },
  190. // 提交订单
  191. onSumit() {
  192. if (this.checkedProductList.length <= 0) {
  193. return this.$toast.error('请选择商品')
  194. }
  195. const params = {}
  196. params.allPrice = this.allPrice
  197. params.allCount = this.checkedProductList.length
  198. params.cartIds = this.checkedProductList.map(product => product.cartId)
  199. params.productIds = this.checkedProductList.map(product => product.productId)
  200. params.productCount = ''
  201. uni.setStorageSync('COMMIT_CART_INFO', params)
  202. this.$router.navigateTo('order/order-create')
  203. },
  204. // 优惠券列表类型更换
  205. onCouponChange(index) {
  206. this.currentTab = index
  207. },
  208. // 优惠券点击事件
  209. onCouponClick(coupon) {
  210. if (coupon.controlType === 'receive') {
  211. this.fetchCouponList()
  212. } else {
  213. this.visiable = false
  214. }
  215. },
  216. // 设置couponTab数据
  217. makeCouponTabs() {
  218. return [
  219. {
  220. name: '已领取优惠券',
  221. num: this.receiveCouponList.length,
  222. isDot: false,
  223. disabled: false
  224. },
  225. {
  226. name: '可领取优惠券',
  227. num: this.ableCouponList.length,
  228. isDot: false,
  229. disabled: false
  230. }
  231. ]
  232. },
  233. // 获取购物车商品可用优惠券
  234. async fetchCouponList() {
  235. const productIds = []
  236. this.shopList.forEach(shop => shop.productList.forEach(product => productIds.push(product.productId)))
  237. try {
  238. const res = await fetchCouponListByProductIds({ userId: this.userId, productIds: productIds.join(',') })
  239. this.receiveCouponList = initFormatCouponList(res.data.receiveCouponList, 'search', true)
  240. this.ableCouponList = initFormatCouponList(res.data.ableCouponList, 'receive', true)
  241. this.couponTabs = this.makeCouponTabs()
  242. } catch (e) {
  243. console.log(e)
  244. } finally {
  245. this.isRequest = false
  246. }
  247. },
  248. // 获取购物车信息
  249. async fetchCartInfo() {
  250. try {
  251. const res = await fetchCartInfo({ userId: this.userId })
  252. this.expiredProducts = res.data.products
  253. this.shopList = res.data.shopList
  254. if (this.shopList.length > 0) {
  255. // 获取优惠券列表
  256. this.fetchCouponList()
  257. }
  258. this.isRequest = false
  259. return res
  260. } catch (e) {
  261. return e
  262. } finally {
  263. uni.stopPullDownRefresh()
  264. }
  265. },
  266. // 确认删除
  267. async onConfirmRemove({ index }) {
  268. if (!index) return (this.removeModal = false)
  269. const cartIds = this.checkedProductList.map(product => product.cartId)
  270. try {
  271. await this.removeFromCart(cartIds)
  272. this.fetchCartInfo()
  273. } catch (e) {
  274. console.log(e)
  275. } finally {
  276. this.isDeleted = false
  277. this.removeModal = false
  278. }
  279. },
  280. // 选中/取消全部商品
  281. onCheckedAll() {
  282. if (this.isChekedAll) {
  283. this.$refs.supplierArea.forEach(item => item.unSelectAll())
  284. } else {
  285. this.$refs.supplierArea.forEach(item => item.selectAll())
  286. }
  287. },
  288. // 选中商品变化
  289. checkedProductChange(e) {
  290. this.changeRef = e
  291. this.handleCartManager()
  292. },
  293. // 修改商品checked属性
  294. updatePorudctChecked(row) {
  295. const shopInfo = this.shopList.find(item => row.shopId === item.shopId)
  296. if (!shopInfo) return
  297. shopInfo.productList.forEach(item => {
  298. if (row.checkedList.includes(item.productId.toString())) {
  299. this.$set(item, 'checked', true)
  300. } else {
  301. this.$set(item, 'checked', false)
  302. }
  303. })
  304. },
  305. // 购物车数据处理器
  306. handleCartManager() {
  307. if (!this.changeRef) return
  308. this.updatePorudctChecked(this.changeRef)
  309. // 保存选中商品id与供应商id的k-v关系
  310. this.$set(this.checkedShopMap, this.changeRef.shopId, this.changeRef.checkedList)
  311. this.isChekedAll = this.validateIsChekedAll()
  312. // 获取已选商品列表
  313. this.checkedProductList = totalAllCheckedProduct(this.shopList)
  314. // 计算商品总价
  315. this.allPrice = computeTotalPrice(this.checkedProductList)
  316. const couponUtils = new CouponUtils(this.receiveCouponList, this.checkedProductList)
  317. this.currentCoupon = couponUtils.bestCoupon
  318. this.nextCoupon = couponUtils.secondCoupon
  319. this.couponTipText = makeCouponUseTip(this.currentCoupon, this.nextCoupon, this.allPrice)
  320. },
  321. // 商品数量变化
  322. async onCountChange(e) {
  323. await this.$store.dispatch('cart/updateProductCount', {
  324. cartId: e.cartId,
  325. productCount: e.count
  326. })
  327. await this.fetchCartInfo()
  328. this.handleCartManager()
  329. },
  330. // 验证是否全选
  331. validateIsChekedAll() {
  332. return this.shopList.every(item => {
  333. const checkedList = this.checkedShopMap[item.shopId] || []
  334. return item.productList.length === checkedList.length
  335. })
  336. }
  337. }
  338. }
  339. </script>
  340. <style scoped lang="scss">
  341. .goods-total {
  342. @extend .cm-flex-between;
  343. background: #f7f7f7;
  344. padding: 0 24rpx;
  345. height: 80rpx;
  346. .total {
  347. font-size: 30rpx;
  348. color: #333;
  349. }
  350. }
  351. .receive-coupon {
  352. @extend .cm-flex-between;
  353. background: #fff;
  354. padding: 0 24rpx;
  355. height: 80rpx;
  356. .tip-text {
  357. font-size: 26rpx;
  358. color: #ff457b;
  359. white-space: nowrap;
  360. overflow: hidden;
  361. text-overflow: ellipsis;
  362. }
  363. }
  364. .supplier-area {
  365. margin-bottom: 24rpx;
  366. }
  367. </style>