cart.vue 7.4 KB

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