123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <template>
- <s-layout :bgStyle="{ color: '#fff' }" tabbar="/pages/index/cart" title="购物车">
- <s-empty
- v-if="state.list.length === 0"
- icon="/static/cart-empty.png"
- text="购物车空空如也,快去逛逛吧~"
- />
- <!-- 头部 -->
- <view v-if="state.list.length" class="cart-box ss-flex ss-flex-col ss-row-between">
- <view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
- <view class="header-left ss-flex ss-col-center ss-font-26">
- 共
- <text class="goods-number ui-TC-Main ss-flex">{{ state.list.length }}</text>
- 件产品
- </view>
- <view class="header-right">
- <button v-if="state.editMode" class="ss-reset-button" @tap="state.editMode = false">
- 取消
- </button>
- <button v-else class="ss-reset-button ui-TC-Main" @tap="state.editMode = true">
- 编辑
- </button>
- </view>
- </view>
- <!-- 内容 -->
- <view class="cart-content ss-flex-1 ss-p-x-30 ss-m-b-40">
- <view v-for="item in state.list" :key="item.id" class="goods-box ss-r-10 ss-m-b-14">
- <view class="ss-flex ss-col-center">
- <label class="check-box ss-flex ss-col-center ss-p-l-10" @tap="onSelectSingle(item.id)">
- <radio
- :checked="state.selectedIds.includes(item.id)"
- color="var(--ui-BG-Main)"
- style="transform: scale(0.8)"
- @tap.stop="onSelectSingle(item.id)"
- />
- </label>
- <s-goods-item
- :img="item.spu.picUrl || item.goods.image"
- :price="item.sku.price"
- :skuText="
- item.sku.properties.length > 1
- ? item.sku.properties.reduce(
- (items2, items) => items2.valueName + ' ' + items.valueName,
- )
- : item.sku.properties[0].valueName
- "
- :title="item.spu.name"
- :titleWidth="400"
- priceColor="#FF3000"
- >
- <template v-if="!state.editMode" v-slot:tool>
- <su-number-box
- v-model="item.count"
- :max="item.sku.stock"
- :min="0"
- :step="1"
- @change="onNumberChange($event, item)"
- />
- </template>
- </s-goods-item>
- </view>
- </view>
- </view>
- <!-- 底部 -->
- <su-fixed v-if="state.list.length > 0" :isInset="false" :val="48" bottom placeholder>
- <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
- <view class="footer-left ss-flex ss-col-center">
- <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
- <radio
- :checked="state.isAllSelected"
- color="var(--ui-BG-Main)"
- style="transform: scale(0.8)"
- @tap.stop="onSelectAll"
- />
- <view class="ss-m-l-8"> 全选</view>
- </label>
- <text>合计:</text>
- <view class="text-price price-text">
- {{ fen2yuan(state.totalPriceSelected) }}
- </view>
- </view>
- <view class="footer-right">
- <button
- v-if="state.editMode"
- class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
- @tap="onDelete"
- >
- 删除
- </button>
- <button
- v-else
- class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
- @tap="onConfirm"
- >
- 去结算
- {{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
- </button>
- </view>
- </view>
- </su-fixed>
- </view>
- </s-layout>
- </template>
- <script setup>
- import sheep from '@/sheep';
- import SpuApi from '@/sheep/api/product/spu';
- import { computed, reactive } from 'vue';
- import { fen2yuan } from '@/sheep/hooks/useGoods';
- import { isEmpty } from '@/sheep/helper/utils';
- const sys_navBar = sheep.$platform.navbar;
- const cart = sheep.$store('cart');
- const state = reactive({
- editMode: false,
- list: computed(() => cart.list),
- selectedList: [],
- selectedIds: computed(() => cart.selectedIds),
- isAllSelected: computed(() => cart.isAllSelected),
- totalPriceSelected: computed(() => cart.totalPriceSelected),
- });
- // 单选选中
- function onSelectSingle(id) {
- cart.selectSingle(id);
- }
- // 全选
- function onSelectAll() {
- cart.selectAll(!state.isAllSelected);
- }
- // 结算
- async function onConfirm() {
- const items = [];
- state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
- state.selectedList.map((item) => {
- // 此处前端做出修改
- items.push({
- skuId: item.sku.id,
- count: item.count,
- cartId: item.id,
- categoryId: item.spu.categoryId,
- });
- });
- if (isEmpty(items)) {
- sheep.$helper.toast('请先选择产品');
- return;
- }
- await validateDeliveryType(state.selectedList.map((item) => item.spu).map((spu) => spu.id));
- sheep.$router.go('/pages/order/confirm', {
- data: JSON.stringify({
- items,
- }),
- });
- }
- /**
- * 校验配送方式冲突
- *
- * @param {string[]} spuIds - 产品ID数组
- * @returns {Promise<void>}
- * @throws {Error} 当配送方式冲突或获取产品信息失败时抛出错误
- */
- async function validateDeliveryType(spuIds) {
- // 获取产品信息
- const { data: spuList } = await SpuApi.getSpuListByIds(spuIds.join(','));
- if (isEmpty(spuList)) {
- sheep.$helper.toast('未找到产品信息');
- throw new Error('未找到产品信息');
- }
- // 获取所有产品的配送方式列表
- const deliveryTypesList = spuList.map(item => item.deliveryTypes);
- // 检查配送方式冲突
- const hasConflict = checkDeliveryConflicts(deliveryTypesList);
- if (hasConflict) {
- sheep.$helper.toast('选中产品支持的配送方式冲突,不允许提交');
- throw new Error('选中产品支持的配送方式冲突,不允许提交');
- }
- }
- /**
- * 检查配送方式列表中是否存在冲突
- * @description
- * 示例场景:
- * A 产品支持:[快递, 自提]
- * B 产品支持:[快递]
- * C 产品支持:[自提]
- *
- * 对比结果:
- * A 和 B:不冲突 (有交集:快递)
- * A 和 C:不冲突 (有交集:自提)
- * B 和 C:冲突 (无交集)
- * @param {Array<Array<number>>} deliveryTypesList - 配送方式列表的数组
- * @returns {boolean} 是否存在冲突
- */
- function checkDeliveryConflicts(deliveryTypesList) {
- for (let i = 0; i < deliveryTypesList.length - 1; i++) {
- const currentTypes = deliveryTypesList[i];
- for (let j = i + 1; j < deliveryTypesList.length; j++) {
- const nextTypes = deliveryTypesList[j];
- // 检查是否没有交集(即冲突)
- const hasNoIntersection = !currentTypes.some(type =>
- nextTypes.includes(type),
- );
- if (hasNoIntersection) {
- return true;
- }
- }
- }
- return false;
- }
- function onNumberChange(e, cartItem) {
- if (e === 0) {
- cart.delete(cartItem.id);
- return;
- }
- if (cartItem.goods_num === e) return;
- cartItem.goods_num = e;
- cart.update({
- goods_id: cartItem.id,
- goods_num: e,
- goods_sku_price_id: cartItem.goods_sku_price_id,
- });
- }
- async function onDelete() {
- cart.delete(state.selectedIds);
- }
- </script>
- <style lang="scss" scoped>
- :deep(.ui-fixed) {
- height: 72rpx;
- }
- .cart-box {
- width: 100%;
- .cart-header {
- height: 70rpx;
- background-color: #f6f6f6;
- width: 100%;
- position: fixed;
- left: 0;
- top: v-bind('sys_navBar') rpx;
- z-index: 1000;
- box-sizing: border-box;
- }
- .cart-footer {
- height: 100rpx;
- background-color: #fff;
- .pay-btn {
- width: 180rpx;
- height: 70rpx;
- font-size: 28rpx;
- line-height: 28rpx;
- font-weight: 500;
- border-radius: 40rpx;
- }
- }
- .cart-content {
- margin-top: 70rpx;
- .goods-box {
- background-color: #fff;
- }
- }
- }
- </style>
|