cart.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <s-layout :bgStyle="{ backgroundColor: '#fff' }" tabbar="/pages/index/cart" title="购物车">
  3. <s-empty v-if="state.list.length === 0" icon="/static/cart-empty.png" text="购物车空空如也,快去逛逛吧~" />
  4. <!-- 头部 -->
  5. <view v-if="state.list.length" class="cart-box ss-flex ss-flex-col ss-row-between">
  6. <view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
  7. <view class="header-left ss-flex ss-col-center ss-font-26">
  8. <text class="goods-number ui-TC-Main ss-flex">{{ state.list.length }}</text>
  9. 件商品
  10. </view>
  11. <view class="header-right">
  12. <button v-if="state.editMode" class="ss-reset-button" @tap="state.editMode = false">
  13. 取消
  14. </button>
  15. <button v-else class="ss-reset-button ui-TC-Main" @tap="state.editMode = true">
  16. 编辑
  17. </button>
  18. </view>
  19. </view>
  20. <!-- 内容 -->
  21. <view class="cart-content ss-flex-1 ss-p-x-30 ss-m-b-40">
  22. <view v-for="item in state.list" :key="item.id" class="goods-box ss-r-10 ss-m-b-14">
  23. <view class="ss-flex ss-col-center">
  24. <label class="check-box ss-flex ss-col-center ss-p-l-10" @tap="onSelectSingle(item.id)">
  25. <radio :checked="state.selectedIds.includes(item.id)" color="var(--ui-BG-Main)"
  26. style="transform: scale(0.8)" @tap.stop="onSelectSingle(item.id)" />
  27. </label>
  28. <s-goods-item :img="item.spu.picUrl || item.goods.image" :price="item.sku.price"
  29. :skuText="item.sku.properties.length>1? item.sku.properties.reduce((items2,items)=>items2.valueName+' '+items.valueName):item.sku.properties[0].valueName"
  30. :title="item.spu.name"
  31. :titleWidth="400" priceColor="#FF3000">
  32. <template v-if="!state.editMode" v-slot:tool>
  33. <su-number-box v-model="item.count" :max="item.sku.stock" :min="0" :step="1"
  34. @change="onNumberChange($event, item)" />
  35. </template>
  36. </s-goods-item>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 底部 -->
  41. <su-fixed v-if="state.list.length > 0" :isInset="false" :val="48" bottom placeholder>
  42. <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
  43. <view class="footer-left ss-flex ss-col-center">
  44. <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
  45. <radio :checked="state.isAllSelected" color="var(--ui-BG-Main)"
  46. style="transform: scale(0.8)" @tap.stop="onSelectAll" />
  47. <view class="ss-m-l-8"> 全选</view>
  48. </label>
  49. <text>合计:</text>
  50. <view class="text-price price-text">
  51. {{ fen2yuan(state.totalPriceSelected) }}
  52. </view>
  53. </view>
  54. <view class="footer-right">
  55. <button v-if="state.editMode" class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  56. @tap="onDelete">
  57. 删除
  58. </button>
  59. <button v-else class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  60. @tap="onConfirm">
  61. 去结算
  62. {{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
  63. </button>
  64. </view>
  65. </view>
  66. </su-fixed>
  67. </view>
  68. </s-layout>
  69. </template>
  70. <script setup>
  71. import sheep from '@/sheep';
  72. import SpuApi from '@/sheep/api/product/spu';
  73. import { computed, reactive } from 'vue';
  74. import { fen2yuan } from '@/sheep/hooks/useGoods';
  75. import { isEmpty } from '@/sheep/helper/utils';
  76. import { DeliveryTypeEnum } from '@/sheep/util/const';
  77. const sys_navBar = sheep.$platform.navbar;
  78. const cart = sheep.$store('cart');
  79. const state = reactive({
  80. editMode: false,
  81. list: computed(() => cart.list),
  82. selectedList: [],
  83. selectedIds: computed(() => cart.selectedIds),
  84. isAllSelected: computed(() => cart.isAllSelected),
  85. totalPriceSelected: computed(() => cart.totalPriceSelected),
  86. });
  87. // 单选选中
  88. function onSelectSingle(id) {
  89. cart.selectSingle(id);
  90. }
  91. // 全选
  92. function onSelectAll() {
  93. cart.selectAll(!state.isAllSelected);
  94. }
  95. // 结算
  96. async function onConfirm() {
  97. const items = [];
  98. state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
  99. state.selectedList.map((item) => {
  100. // 此处前端做出修改
  101. items.push({
  102. skuId: item.sku.id,
  103. count: item.count,
  104. cartId: item.id,
  105. categoryId: item.spu.categoryId,
  106. });
  107. });
  108. if (isEmpty(items)) {
  109. sheep.$helper.toast('请先选择商品');
  110. return;
  111. }
  112. await validateDeliveryType(state.selectedList.map((item) => item.spu).map((spu) => spu.id));
  113. sheep.$router.go('/pages/order/confirm', {
  114. data: JSON.stringify({
  115. items,
  116. }),
  117. });
  118. }
  119. /** 校验配送方式 */
  120. function validateDeliveryType(spuIds) {
  121. return new Promise(async (resolve, reject) => {
  122. const { data } = await SpuApi.getSpuListByIds(spuIds.join(','));
  123. if (isEmpty(data)) {
  124. reject('获取商品信息失败!!!');
  125. return;
  126. }
  127. let onlyExpress = false; // 只快递
  128. let onlyPickup = false; // 只自提
  129. const deliveryTypes = data.map((item) => item.deliveryTypes);
  130. for (const deliveryType of deliveryTypes) {
  131. // 情况一:两种配送方式都支持
  132. if (deliveryType.length > 1) {
  133. continue;
  134. }
  135. // 情况二:只支持一种
  136. if (deliveryType[0] === DeliveryTypeEnum.EXPRESS.type) {
  137. onlyExpress = true;
  138. } else if (deliveryType[0] === DeliveryTypeEnum.PICK_UP.type) {
  139. onlyPickup = true;
  140. }
  141. }
  142. if (onlyExpress || onlyPickup) {
  143. reject('选中商品存在只支持特定配送方式的情况不允许提交!!!');
  144. sheep.$helper.toast('选中商品存在只支持特定配送方式的情况不允许提交!!!');
  145. return;
  146. }
  147. resolve();
  148. });
  149. }
  150. function onNumberChange(e, cartItem) {
  151. if (e === 0) {
  152. cart.delete(cartItem.id);
  153. return;
  154. }
  155. if (cartItem.goods_num === e) return;
  156. cartItem.goods_num = e;
  157. cart.update({
  158. goods_id: cartItem.id,
  159. goods_num: e,
  160. goods_sku_price_id: cartItem.goods_sku_price_id,
  161. });
  162. }
  163. async function onDelete() {
  164. cart.delete(state.selectedIds);
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. :deep(.ui-fixed) {
  169. height: 72rpx;
  170. }
  171. .cart-box {
  172. width: 100%;
  173. .cart-header {
  174. height: 70rpx;
  175. background-color: #f6f6f6;
  176. width: 100%;
  177. position: fixed;
  178. left: 0;
  179. top: v-bind('sys_navBar') rpx;
  180. z-index: 1000;
  181. box-sizing: border-box;
  182. }
  183. .cart-footer {
  184. height: 100rpx;
  185. background-color: #fff;
  186. .pay-btn {
  187. width: 180rpx;
  188. height: 70rpx;
  189. font-size: 28rpx;
  190. line-height: 28rpx;
  191. font-weight: 500;
  192. border-radius: 40rpx;
  193. }
  194. }
  195. .cart-content {
  196. margin-top: 70rpx;
  197. .goods-box {
  198. background-color: #fff;
  199. }
  200. }
  201. }
  202. </style>