cart.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <s-layout title="购物车" tabbar="/pages/index/cart" :bgStyle="{ color: '#fff' }">
  3. <s-empty v-if="state.list.length === 0" text="购物车空空如也,快去逛逛吧~" icon="/static/cart-empty.png" />
  4. <!-- 头部 -->
  5. <view class="cart-box ss-flex ss-flex-col ss-row-between" v-if="state.list.length">
  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. <view class="ss-m-t-70">
  21. </view>
  22. <!-- 内容 -->
  23. <view class="cart-content ss-flex-1 ss-p-x-30" v-for="(shopItems, shopName, index) in processedValidList" :key="shopName">
  24. <view class="ss-m-b-14 bg-white goods-box">
  25. <view class="title-text ss-p-x-10 ss-p-t-20 ss-flex ss-col-center">
  26. <!-- <label class="check-box ss-flex ss-col-center" @tap="onSelectSingle()">
  27. <radio color="var(--ui-BG-Main)" style="transform: scale(0.8)" />
  28. </label> -->
  29. <text class="ss-m-l-10">{{ shopName }}</text>
  30. </view>
  31. <!-- 注意这里v-for的对象是shopItems -->
  32. <view class="ss-r-10" v-for="(product, productIndex) in shopItems" :key="product.id">
  33. <view class="ss-flex ss-col-center">
  34. <label class="check-box ss-flex ss-col-center ss-p-l-10" @tap="onSelectSingle(product.id)">
  35. <radio :checked="state.selectedIds.includes(product.id)" color="var(--ui-BG-Main)" style="transform: scale(0.8)" @tap.stop="onSelectSingle(product.id)" />
  36. </label>
  37. <s-goods-item :title="product.spu.name" :img="product.spu.picUrl || product.goods.image" :price="product.sku.price"
  38. :skuText="product.sku.properties.length > 1 ? product.sku.properties.reduce((items2, items) => items2.valueName + ' ' + items.valueName) : product.sku.properties[0].valueName"
  39. priceColor="#FF3000" :titleWidth="400">
  40. <template v-if="!state.editMode" v-slot:tool>
  41. <su-number-box :min="0" :max="product.sku.stock" :step="1" v-model="product.count" @change="onNumberChange($event, product)"></su-number-box>
  42. </template>
  43. </s-goods-item>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 底部 -->
  49. <su-fixed bottom :val="48" placeholder v-if="state.list.length > 0" :isInset="false">
  50. <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
  51. <view class="footer-left ss-flex ss-col-center">
  52. <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
  53. <radio :checked="state.isAllSelected" color="var(--ui-BG-Main)"
  54. style="transform: scale(0.8)" @tap.stop="onSelectAll" />
  55. <view class="ss-m-l-8"> 全选 </view>
  56. </label>
  57. <text>合计:</text>
  58. <view class="text-price price-text">
  59. {{ fen2yuan(state.totalPriceSelected)}}
  60. </view>
  61. </view>
  62. <view class="footer-right">
  63. <button v-if="state.editMode" class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  64. @tap="onDelete">
  65. 删除
  66. </button>
  67. <button v-else class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  68. @tap="onConfirm">
  69. 去结算
  70. {{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
  71. </button>
  72. </view>
  73. </view>
  74. </su-fixed>
  75. </view>
  76. </s-layout>
  77. </template>
  78. <script setup>
  79. import sheep from '@/sheep';
  80. import {
  81. computed,
  82. reactive,
  83. unref
  84. } from 'vue';
  85. import {
  86. fen2yuan
  87. } from '@/sheep/hooks/useGoods';
  88. const sys_navBar = sheep.$platform.navbar;
  89. const cart = sheep.$store('cart');
  90. const state = reactive({
  91. editMode: false,
  92. list: computed(() => cart.list),
  93. shopList: computed(() => cart.shopNameMap),
  94. skuList: computed(() => cart.shopSkuMap),
  95. selectedList: [],
  96. selectedIds: computed(() => cart.selectedIds),
  97. isAllSelected: computed(() => cart.isAllSelected),
  98. totalPriceSelected: computed(() => cart.totalPriceSelected),
  99. });
  100. // 处理后端返回来的数据
  101. const processedValidList = computed(() => {
  102. // 初始化一个空对象,用于按店铺分组商品
  103. const groups = {};
  104. // 遍历validList,将每个商品分配到对应的店铺分组中
  105. state.list.forEach(item => {
  106. // 找到当前SKU所属的店铺ID
  107. const shopId = Object.keys(state.skuList).find(key => state.skuList[key].includes(item.sku.id));
  108. const shopName = state.shopList[shopId] || '未知店铺';
  109. // 如果该店铺分组不存在,则初始化
  110. if (!groups[shopName]) {
  111. groups[shopName] = [];
  112. }
  113. // 将当前商品加入到对应店铺的分组中
  114. groups[shopName].push(item);
  115. });
  116. return groups;
  117. });
  118. // 单选选中
  119. function onSelectSingle(id) {
  120. console.log('单选')
  121. cart.selectSingle(id);
  122. }
  123. // 全选
  124. function onSelectAll() {
  125. cart.selectAll(!state.isAllSelected);
  126. }
  127. // 结算
  128. function onConfirm() {
  129. let items = []
  130. let goods_list = [];
  131. state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
  132. state.selectedList.map((item) => {
  133. // console.log(item, '便利');
  134. // 此处前端做出修改
  135. items.push({
  136. skuId: item.sku.id,
  137. count: item.count,
  138. cartId: item.id,
  139. })
  140. goods_list.push({
  141. // goods_id: item.goods_id,
  142. goods_id: item.spu.id,
  143. // goods_num: item.goods_num,
  144. goods_num: item.count,
  145. // 商品价格id真没有
  146. // goods_sku_price_id: item.goods_sku_price_id,
  147. });
  148. });
  149. // return;
  150. if (goods_list.length === 0) {
  151. sheep.$helper.toast('请选择商品');
  152. return;
  153. }
  154. sheep.$router.go('/pages/order/confirm', {
  155. data: JSON.stringify({
  156. // order_type: 'goods',
  157. // goods_list,
  158. items,
  159. // from: 'cart',
  160. pointStatus: false,
  161. // 从购物车这里提交订单的 一定是实体商品和用人民币买的 所以可以直接写死
  162. deliveryType: 1,
  163. spuPayType:1,
  164. spuType:1,
  165. }),
  166. });
  167. }
  168. function onNumberChange(e, cartItem) {
  169. if (e === 0) {
  170. cart.delete(cartItem.id);
  171. return;
  172. }
  173. if (cartItem.goods_num === e) return;
  174. cartItem.goods_num = e;
  175. cart.update({
  176. goods_id: cartItem.id,
  177. goods_num: e,
  178. goods_sku_price_id: cartItem.goods_sku_price_id,
  179. });
  180. }
  181. async function onDelete() {
  182. cart.delete(state.selectedIds);
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. :deep(.ui-fixed) {
  187. height: 72rpx;
  188. }
  189. .title-text {
  190. font-size: 30rpx;
  191. font-weight: bold;
  192. line-height: 42rpx;
  193. }
  194. .cart-box {
  195. width: 100%;
  196. .cart-header {
  197. height: 70rpx;
  198. background-color: #f6f6f6;
  199. width: 100%;
  200. position: fixed;
  201. left: 0;
  202. top: v-bind('sys_navBar') rpx;
  203. z-index: 1000;
  204. box-sizing: border-box;
  205. }
  206. .cart-footer {
  207. height: 100rpx;
  208. background-color: #fff;
  209. .pay-btn {
  210. width: 180rpx;
  211. height: 70rpx;
  212. font-size: 28rpx;
  213. line-height: 28rpx;
  214. font-weight: 500;
  215. border-radius: 40rpx;
  216. }
  217. }
  218. .cart-content {
  219. .goods-box {
  220. border-radius: 20rpx;
  221. }
  222. }
  223. }
  224. </style>