cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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"></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. </view>
  85. </template>
  86. <script>
  87. import { fetchCartInfo } from '@/services/api/cart.js'
  88. import { fetchCouponListByProductIds } from '@/services/api/coupon.js'
  89. import { arrayUnique } from '@/common/utils.js'
  90. import { mapGetters, mapActions } from 'vuex'
  91. import CouponUtils from '@/common/couponUtils.js'
  92. // 业务帮助函数
  93. import {
  94. totalAllCheckedProduct,
  95. computeTotalPrice,
  96. initFormatCouponList,
  97. makeCouponUseTip
  98. } from '@/common/business.helper.js'
  99. const resetData = () => ({
  100. isRequest: true,
  101. isDeleted: false,
  102. expiredProducts: [], // 失效商品列表
  103. shopList: [], // 供应商&&商品列表
  104. checkedShopMap: {},
  105. isChekedAll: false,
  106. removeModal: false,
  107. // 商品价格
  108. checkedProductList: [],
  109. allPrice: 0, // 商品总价
  110. // 优惠券列表
  111. visiable: false,
  112. currentTab: 0,
  113. receiveCouponList: [], // 已领取优惠券
  114. ableCouponList: [], // 可领取优惠券
  115. couponTabs: [],
  116. currentCoupon: null,
  117. nextCoupon: null,
  118. couponTipText: '', // 可用优惠券提示
  119. // 保存变动
  120. changeRef: null
  121. })
  122. export default {
  123. data() {
  124. return resetData()
  125. },
  126. computed: {
  127. ...mapGetters(['userId', 'kindCount']),
  128. couponList() {
  129. return this.currentTab === 0 ? this.receiveCouponList : this.ableCouponList
  130. },
  131. navbarData() {
  132. const result = {
  133. finallyPrice: 0, // 实际应付
  134. couponAmount: 0,
  135. allPrice: 0,
  136. discountedPrice: 0,
  137. count: 0
  138. }
  139. if (this.currentCoupon) {
  140. result.couponAmount = this.currentCoupon.couponAmount
  141. result.discountedPrice = this.currentCoupon.couponAmount
  142. }
  143. result.allPrice = this.allPrice
  144. if (this.allPrice - result.discountedPrice > 0) {
  145. result.finallyPrice = this.allPrice - result.couponAmount
  146. }
  147. result.count = this.checkedProductList.length
  148. return result
  149. }
  150. },
  151. onPullDownRefresh() {
  152. this.resetData()
  153. this.fetchCartInfo()
  154. },
  155. onShow() {
  156. this.resetData()
  157. this.fetchCartInfo()
  158. this.fetchCartKindCount()
  159. },
  160. methods: {
  161. ...mapActions('cart', ['removeFromCart', 'fetchCartKindCount']),
  162. // 初始化数据
  163. resetData() {
  164. const data = resetData()
  165. for (let key in data) {
  166. this[key] = data[key]
  167. }
  168. },
  169. // 提交订单
  170. onSumit() {
  171. if (this.checkedProductList.length <= 0) {
  172. return this.$toast.error('请选择商品')
  173. }
  174. const params = {}
  175. params.allPrice = this.allPrice
  176. params.allCount = this.checkedProductList.length
  177. params.cartIds = this.checkedProductList.map(product => product.cartId)
  178. params.productIds = this.checkedProductList.map(product => product.productId)
  179. params.productCount = ''
  180. uni.setStorageSync('COMMIT_CART_INFO', params)
  181. this.$router.navigateTo('order/order-create')
  182. },
  183. // 优惠券列表类型更换
  184. onCouponChange(index) {
  185. this.currentTab = index
  186. },
  187. // 优惠券点击事件
  188. onCouponClick(coupon) {
  189. if (coupon.controlType === 'receive') {
  190. this.fetchCouponList()
  191. } else {
  192. this.visiable = false
  193. }
  194. },
  195. // 设置couponTab数据
  196. makeCouponTabs() {
  197. return [
  198. {
  199. name: '已领取优惠券',
  200. num: this.receiveCouponList.length,
  201. isDot: false,
  202. disabled: false
  203. },
  204. {
  205. name: '可领取优惠券',
  206. num: this.ableCouponList.length,
  207. isDot: false,
  208. disabled: false
  209. }
  210. ]
  211. },
  212. // 获取购物车商品可用优惠券
  213. async fetchCouponList() {
  214. const productIds = []
  215. this.shopList.forEach(shop => shop.productList.forEach(product => productIds.push(product.productId)))
  216. try {
  217. const res = await fetchCouponListByProductIds({ userId: this.userId, productIds: productIds.join(',') })
  218. this.receiveCouponList = initFormatCouponList(res.data.receiveCouponList, 'search', true)
  219. this.ableCouponList = initFormatCouponList(res.data.ableCouponList, 'receive', true)
  220. this.couponTabs = this.makeCouponTabs()
  221. } catch (e) {
  222. console.log(e)
  223. } finally {
  224. this.isRequest = false
  225. }
  226. },
  227. // 获取购物车信息
  228. async fetchCartInfo() {
  229. try {
  230. const res = await fetchCartInfo({ userId: this.userId })
  231. this.expiredProducts = res.data.products
  232. this.shopList = res.data.shopList
  233. if (this.shopList.length > 0) {
  234. // 获取优惠券列表
  235. this.fetchCouponList()
  236. }
  237. this.isRequest = false
  238. return res
  239. } catch (e) {
  240. return e
  241. } finally {
  242. uni.stopPullDownRefresh()
  243. }
  244. },
  245. // 确认删除
  246. async onConfirmRemove({ index }) {
  247. if (!index) return (this.removeModal = false)
  248. const cartIds = this.checkedProductList.map(product => product.cartId)
  249. try {
  250. await this.removeFromCart(cartIds)
  251. this.fetchCartInfo()
  252. } catch (e) {
  253. console.log(e)
  254. } finally {
  255. this.isDeleted = false
  256. this.removeModal = false
  257. }
  258. },
  259. // 选中/取消全部商品
  260. onCheckedAll() {
  261. if (this.isChekedAll) {
  262. this.$refs.supplierArea.forEach(item => item.unSelectAll())
  263. } else {
  264. this.$refs.supplierArea.forEach(item => item.selectAll())
  265. }
  266. },
  267. // 选中商品变化
  268. checkedProductChange(e) {
  269. this.changeRef = e
  270. this.handleCartManager()
  271. },
  272. // 修改商品checked属性
  273. updatePorudctChecked(row) {
  274. const shopInfo = this.shopList.find(item => row.shopId === item.shopId)
  275. if (!shopInfo) return
  276. shopInfo.productList.forEach(item => {
  277. if (row.checkedList.includes(item.productId.toString())) {
  278. this.$set(item, 'checked', true)
  279. } else {
  280. this.$set(item, 'checked', false)
  281. }
  282. })
  283. },
  284. // 购物车数据处理器
  285. handleCartManager() {
  286. if (!this.changeRef) return
  287. this.updatePorudctChecked(this.changeRef)
  288. // 保存选中商品id与供应商id的k-v关系
  289. this.$set(this.checkedShopMap, this.changeRef.shopId, this.changeRef.checkedList)
  290. this.isChekedAll = this.validateIsChekedAll()
  291. // 获取已选商品列表
  292. this.checkedProductList = totalAllCheckedProduct(this.shopList)
  293. // 计算商品总价
  294. this.allPrice = computeTotalPrice(this.checkedProductList)
  295. const couponUtils = new CouponUtils(this.receiveCouponList, this.checkedProductList)
  296. this.currentCoupon = couponUtils.bestCoupon
  297. this.nextCoupon = couponUtils.secondCoupon
  298. this.couponTipText = makeCouponUseTip(this.currentCoupon, this.nextCoupon, this.allPrice)
  299. },
  300. // 商品数量变化
  301. async onCountChange(e) {
  302. await this.$store.dispatch('cart/updateProductCount', {
  303. cartId: e.cartId,
  304. productCount: e.count
  305. })
  306. await this.fetchCartInfo()
  307. this.handleCartManager()
  308. },
  309. // 验证是否全选
  310. validateIsChekedAll() {
  311. return this.shopList.every(item => {
  312. const checkedList = this.checkedShopMap[item.shopId] || []
  313. return item.productList.length === checkedList.length
  314. })
  315. }
  316. }
  317. }
  318. </script>
  319. <style scoped lang="scss">
  320. .goods-total {
  321. @extend .cm-flex-between;
  322. background: #f7f7f7;
  323. padding: 0 24rpx;
  324. height: 80rpx;
  325. .total {
  326. font-size: 30rpx;
  327. color: #333;
  328. }
  329. }
  330. .receive-coupon {
  331. @extend .cm-flex-between;
  332. background: #fff;
  333. padding: 0 24rpx;
  334. height: 80rpx;
  335. .tip-text {
  336. font-size: 26rpx;
  337. color: #ff457b;
  338. white-space: nowrap;
  339. overflow: hidden;
  340. text-overflow: ellipsis;
  341. }
  342. }
  343. .supplier-area {
  344. margin-bottom: 24rpx;
  345. }
  346. </style>