cart.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <s-layout :bgStyle="{ color: '#fff' }" tabbar="/pages/index/cart" title="购物车">
  3. <s-empty
  4. v-if="state.list.length === 0"
  5. icon="/static/cart-empty.png"
  6. text="购物车空空如也,快去逛逛吧~"
  7. />
  8. <!-- 头部 -->
  9. <view v-if="state.list.length" class="cart-box ss-flex ss-flex-col ss-row-between">
  10. <view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
  11. <view class="header-left ss-flex ss-col-center ss-font-26">
  12. <text class="goods-number ui-TC-Main ss-flex">{{ state.list.length }}</text>
  13. 件商品
  14. </view>
  15. <view class="header-right">
  16. <button v-if="state.editMode" class="ss-reset-button" @tap="onChangeEditMode(false)">
  17. 取消
  18. </button>
  19. <button v-else class="ss-reset-button ui-TC-Main" @tap="onChangeEditMode(true)">
  20. 编辑
  21. </button>
  22. </view>
  23. </view>
  24. <!-- 内容 -->
  25. <view class="cart-content ss-flex-1 ss-p-x-30 ss-m-b-40">
  26. <view v-for="item in state.list" :key="item.id" class="goods-box ss-r-10 ss-m-b-14">
  27. <view class="ss-flex ss-col-center">
  28. <label class="check-box ss-flex ss-col-center ss-p-l-10" @tap="onSelectSingle(item.id)">
  29. <radio
  30. :checked="state.selectedIds.includes(item.id)"
  31. color="var(--ui-BG-Main)"
  32. style="transform: scale(0.8)"
  33. @tap.stop="onSelectSingle(item.id)"
  34. />
  35. </label>
  36. <view v-if="item.spu?.status !== 1 && !state.editMode" class="down-box">
  37. 该商品已下架
  38. </view>
  39. <view v-else-if="item.spu?.stock <= 0 && !state.editMode" class="down-box">
  40. 该商品无库存
  41. </view>
  42. <s-goods-item
  43. :img="item.spu.picUrl || item.goods.image"
  44. :price="item.sku.price"
  45. :skuText="
  46. item.sku.properties.length > 1
  47. ? item.sku.properties.reduce(
  48. (items2, items) => items2.valueName + ' ' + items.valueName,
  49. )
  50. : item.sku.properties[0].valueName
  51. "
  52. :title="item.spu.name"
  53. :titleWidth="400"
  54. priceColor="#FF3000"
  55. >
  56. <template v-if="!state.editMode" v-slot:tool>
  57. <su-number-box
  58. v-model="item.count"
  59. :max="item.sku.stock"
  60. :min="0"
  61. :step="1"
  62. @change="onNumberChange($event, item)"
  63. />
  64. </template>
  65. </s-goods-item>
  66. </view>
  67. </view>
  68. </view>
  69. <!-- 底部 -->
  70. <su-fixed v-if="state.list.length > 0" :isInset="false" :val="48" bottom placeholder>
  71. <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
  72. <view class="footer-left ss-flex ss-col-center">
  73. <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
  74. <radio
  75. :checked="state.isAllSelected"
  76. color="var(--ui-BG-Main)"
  77. style="transform: scale(0.8)"
  78. @tap.stop="onSelectAll"
  79. />
  80. <view class="ss-m-l-8"> 全选</view>
  81. </label>
  82. <text>合计:</text>
  83. <view class="text-price price-text">
  84. {{ fen2yuan(state.totalPriceSelected) }}
  85. </view>
  86. </view>
  87. <view class="footer-right">
  88. <button
  89. v-if="state.editMode"
  90. class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  91. @tap="onDelete"
  92. >
  93. 删除
  94. </button>
  95. <button
  96. v-else
  97. class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  98. @tap="onConfirm"
  99. >
  100. 去结算
  101. {{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
  102. </button>
  103. </view>
  104. </view>
  105. </su-fixed>
  106. </view>
  107. </s-layout>
  108. </template>
  109. <script setup>
  110. import sheep from '@/sheep';
  111. import { onShow } from '@dcloudio/uni-app';
  112. import SpuApi from '@/sheep/api/product/spu';
  113. import { computed, reactive } from 'vue';
  114. import { fen2yuan } from '@/sheep/hooks/useGoods';
  115. import { isEmpty } from '@/sheep/helper/utils';
  116. // 隐藏原生tabBar
  117. uni.hideTabBar({
  118. fail: () => {},
  119. });
  120. const sys_navBar = sheep.$platform.navbar;
  121. const cart = sheep.$store('cart');
  122. const state = reactive({
  123. editMode: computed(() => cart.editMode),
  124. list: computed(() => cart.list),
  125. selectedList: [],
  126. selectedIds: computed(() => cart.selectedIds),
  127. isAllSelected: computed(() => cart.isAllSelected),
  128. totalPriceSelected: computed(() => cart.totalPriceSelected),
  129. });
  130. // 单选选中
  131. function onSelectSingle(id) {
  132. cart.selectSingle(id);
  133. }
  134. // 编辑、取消
  135. function onChangeEditMode(flag) {
  136. cart.onChangeEditMode(flag);
  137. }
  138. // 全选
  139. function onSelectAll() {
  140. cart.selectAll(!state.isAllSelected);
  141. }
  142. // 结算
  143. async function onConfirm() {
  144. const items = [];
  145. state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
  146. state.selectedList.map((item) => {
  147. // 此处前端做出修改
  148. items.push({
  149. skuId: item.sku.id,
  150. count: item.count,
  151. cartId: item.id,
  152. categoryId: item.spu.categoryId,
  153. });
  154. });
  155. if (isEmpty(items)) {
  156. sheep.$helper.toast('请先选择商品');
  157. return;
  158. }
  159. await validateDeliveryType(state.selectedList.map((item) => item.spu).map((spu) => spu.id));
  160. sheep.$router.go('/pages/order/confirm', {
  161. data: JSON.stringify({
  162. items,
  163. }),
  164. });
  165. }
  166. /**
  167. * 校验配送方式冲突
  168. *
  169. * @param {string[]} spuIds - 商品ID数组
  170. * @returns {Promise<void>}
  171. * @throws {Error} 当配送方式冲突或获取商品信息失败时抛出错误
  172. */
  173. async function validateDeliveryType(spuIds) {
  174. // 获取商品信息
  175. const { data: spuList } = await SpuApi.getSpuListByIds(spuIds.join(','));
  176. if (isEmpty(spuList)) {
  177. sheep.$helper.toast('未找到商品信息');
  178. throw new Error('未找到商品信息');
  179. }
  180. // 获取所有商品的配送方式列表
  181. const deliveryTypesList = spuList.map((item) => item.deliveryTypes);
  182. // 检查配送方式冲突
  183. const hasConflict = checkDeliveryConflicts(deliveryTypesList);
  184. if (hasConflict) {
  185. sheep.$helper.toast('选中商品支持的配送方式冲突,不允许提交');
  186. throw new Error('选中商品支持的配送方式冲突,不允许提交');
  187. }
  188. }
  189. /**
  190. * 检查配送方式列表中是否存在冲突
  191. * @description
  192. * 示例场景:
  193. * A 商品支持:[快递, 自提]
  194. * B 商品支持:[快递]
  195. * C 商品支持:[自提]
  196. *
  197. * 对比结果:
  198. * A 和 B:不冲突 (有交集:快递)
  199. * A 和 C:不冲突 (有交集:自提)
  200. * B 和 C:冲突 (无交集)
  201. * @param {Array<Array<number>>} deliveryTypesList - 配送方式列表的数组
  202. * @returns {boolean} 是否存在冲突
  203. */
  204. function checkDeliveryConflicts(deliveryTypesList) {
  205. for (let i = 0; i < deliveryTypesList.length - 1; i++) {
  206. const currentTypes = deliveryTypesList[i];
  207. for (let j = i + 1; j < deliveryTypesList.length; j++) {
  208. const nextTypes = deliveryTypesList[j];
  209. // 检查是否没有交集(即冲突)
  210. const hasNoIntersection = !currentTypes.some((type) => nextTypes.includes(type));
  211. if (hasNoIntersection) {
  212. return true;
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218. function onNumberChange(e, cartItem) {
  219. if (e === 0) {
  220. cart.delete(cartItem.id);
  221. return;
  222. }
  223. if (cartItem.goods_num === e) return;
  224. cartItem.goods_num = e;
  225. cart.update({
  226. goods_id: cartItem.id,
  227. goods_num: e,
  228. goods_sku_price_id: cartItem.goods_sku_price_id,
  229. });
  230. }
  231. async function onDelete() {
  232. cart.delete(state.selectedIds);
  233. }
  234. function getCartList() {
  235. cart.getList();
  236. }
  237. onShow(() => {
  238. getCartList();
  239. });
  240. </script>
  241. <style lang="scss" scoped>
  242. :deep(.ui-fixed) {
  243. height: 72rpx;
  244. }
  245. .cart-box {
  246. width: 100%;
  247. .cart-header {
  248. height: 70rpx;
  249. background-color: #f6f6f6;
  250. width: 100%;
  251. position: fixed;
  252. left: 0;
  253. top: v-bind('sys_navBar') rpx;
  254. z-index: 1000;
  255. box-sizing: border-box;
  256. }
  257. .cart-footer {
  258. height: 100rpx;
  259. background-color: #fff;
  260. .pay-btn {
  261. width: 180rpx;
  262. height: 70rpx;
  263. font-size: 28rpx;
  264. line-height: 28rpx;
  265. font-weight: 500;
  266. border-radius: 40rpx;
  267. }
  268. }
  269. .cart-content {
  270. margin-top: 70rpx;
  271. .goods-box {
  272. background-color: #fff;
  273. position: relative;
  274. }
  275. // 下架商品
  276. .down-box {
  277. position: absolute;
  278. left: 0;
  279. top: 0;
  280. width: 100%;
  281. height: 100%;
  282. background: rgba(#fff, 0.8);
  283. z-index: 2;
  284. display: flex;
  285. justify-content: center;
  286. align-items: center;
  287. color: #999;
  288. font-size: 32rpx;
  289. }
  290. }
  291. }
  292. </style>