cart.vue 8.3 KB

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