cart.vue 6.1 KB

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