cart.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <s-layout :bgStyle="{ backgroundColor: '#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. import { DeliveryTypeEnum } from '@/sheep/util/const';
  110. const sys_navBar = sheep.$platform.navbar;
  111. const cart = sheep.$store('cart');
  112. const state = reactive({
  113. editMode: false,
  114. list: computed(() => cart.list),
  115. selectedList: [],
  116. selectedIds: computed(() => cart.selectedIds),
  117. isAllSelected: computed(() => cart.isAllSelected),
  118. totalPriceSelected: computed(() => cart.totalPriceSelected),
  119. });
  120. // 单选选中
  121. function onSelectSingle(id) {
  122. cart.selectSingle(id);
  123. }
  124. // 全选
  125. function onSelectAll() {
  126. cart.selectAll(!state.isAllSelected);
  127. }
  128. // 结算
  129. async function onConfirm() {
  130. const items = [];
  131. state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
  132. state.selectedList.map((item) => {
  133. // 此处前端做出修改
  134. items.push({
  135. skuId: item.sku.id,
  136. count: item.count,
  137. cartId: item.id,
  138. categoryId: item.spu.categoryId,
  139. });
  140. });
  141. if (isEmpty(items)) {
  142. sheep.$helper.toast('请先选择商品');
  143. return;
  144. }
  145. await validateDeliveryType(state.selectedList.map((item) => item.spu).map((spu) => spu.id));
  146. sheep.$router.go('/pages/order/confirm', {
  147. data: JSON.stringify({
  148. items,
  149. }),
  150. });
  151. }
  152. /** 校验配送方式 */
  153. function validateDeliveryType(spuIds) {
  154. return new Promise(async (resolve, reject) => {
  155. const { data } = await SpuApi.getSpuListByIds(spuIds.join(','));
  156. if (isEmpty(data)) {
  157. reject('获取商品信息失败!!!');
  158. return;
  159. }
  160. let onlyExpress = false; // 只快递
  161. let onlyPickup = false; // 只自提
  162. // TODO @puhui999:这里需要比对,A 商品支持自提、B 商品支持快递,这样导致 A 和 B 无法一起下单。
  163. const deliveryTypes = data.map((item) => item.deliveryTypes);
  164. for (const deliveryType of deliveryTypes) {
  165. // 情况一:两种配送方式都支持
  166. if (deliveryType.length > 1) {
  167. continue;
  168. }
  169. // 情况二:只支持一种
  170. if (deliveryType[0] === DeliveryTypeEnum.EXPRESS.type) {
  171. onlyExpress = true;
  172. } else if (deliveryType[0] === DeliveryTypeEnum.PICK_UP.type) {
  173. onlyPickup = true;
  174. }
  175. }
  176. if (onlyExpress || onlyPickup) {
  177. reject('选中商品存在只支持特定配送方式的情况不允许提交!!!');
  178. sheep.$helper.toast('选中商品存在只支持特定配送方式的情况不允许提交!!!');
  179. return;
  180. }
  181. resolve();
  182. });
  183. }
  184. function onNumberChange(e, cartItem) {
  185. if (e === 0) {
  186. cart.delete(cartItem.id);
  187. return;
  188. }
  189. if (cartItem.goods_num === e) return;
  190. cartItem.goods_num = e;
  191. cart.update({
  192. goods_id: cartItem.id,
  193. goods_num: e,
  194. goods_sku_price_id: cartItem.goods_sku_price_id,
  195. });
  196. }
  197. async function onDelete() {
  198. cart.delete(state.selectedIds);
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. :deep(.ui-fixed) {
  203. height: 72rpx;
  204. }
  205. .cart-box {
  206. width: 100%;
  207. .cart-header {
  208. height: 70rpx;
  209. background-color: #f6f6f6;
  210. width: 100%;
  211. position: fixed;
  212. left: 0;
  213. top: v-bind('sys_navBar') rpx;
  214. z-index: 1000;
  215. box-sizing: border-box;
  216. }
  217. .cart-footer {
  218. height: 100rpx;
  219. background-color: #fff;
  220. .pay-btn {
  221. width: 180rpx;
  222. height: 70rpx;
  223. font-size: 28rpx;
  224. line-height: 28rpx;
  225. font-weight: 500;
  226. border-radius: 40rpx;
  227. }
  228. }
  229. .cart-content {
  230. margin-top: 70rpx;
  231. .goods-box {
  232. background-color: #fff;
  233. }
  234. }
  235. }
  236. </style>