424 lines
12 KiB
Vue
424 lines
12 KiB
Vue
<template>
|
||
<s-pages
|
||
tabbar="/pages/cart/cart"
|
||
title="购物车"
|
||
:showLeftButton="false"
|
||
color="#EEF1FE"
|
||
>
|
||
<template #navRight>
|
||
<view v-if="state.list.length" class="cart-nav-right">
|
||
<view v-if="state.editMode" class="cart-nav-right__btn" @tap="onChangeEditMode(false)">
|
||
取消
|
||
</view>
|
||
<view v-else class="cart-nav-right__btn f500" @tap="onChangeEditMode(true)">管理</view>
|
||
</view>
|
||
</template>
|
||
<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-content ss-flex-1 ss-p-x-30 ss-m-b-40 m-b-100">
|
||
<view
|
||
v-for="item in state.list"
|
||
:key="item.id"
|
||
class="goods-box ss-r-10"
|
||
@click="goInfo(item.spu.id)"
|
||
>
|
||
<view class="ss-flex ss-col-center">
|
||
<label
|
||
class="check-box ss-flex ss-col-center ss-p-l-10"
|
||
@tap.stop="onSelectSingle(item.id)"
|
||
>
|
||
<radio
|
||
:checked="state.selectedIds.includes(item.id)"
|
||
color="var(--color32)"
|
||
style="transform: scale(0.8)"
|
||
backgroundColor="var(--color31)"
|
||
@tap.stop="onSelectSingle(item.id)"
|
||
/>
|
||
</label>
|
||
|
||
<s-goods-item
|
||
:img="item.spu.picUrl || item.goods.image"
|
||
:price="item.sku?.price || 0"
|
||
: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>
|
||
<view
|
||
v-if="item.spu?.status !== 1 && !state.editMode"
|
||
class="down-box size24"
|
||
style="text-align: left; color: #999"
|
||
>
|
||
该商品已下架
|
||
</view>
|
||
<view
|
||
v-else-if="item.spu?.stock <= 0 && !state.editMode"
|
||
class="down-box size24"
|
||
style="text-align: left; color: #999"
|
||
>
|
||
该商品无库存
|
||
</view>
|
||
<su-number-box
|
||
v-else
|
||
v-model="item.count"
|
||
:max="item.sku?.stock"
|
||
:min="0"
|
||
:step="1"
|
||
@change="onNumberChange($event, item)"
|
||
background="#ffffff"
|
||
color="#333333"
|
||
/>
|
||
</template>
|
||
</s-goods-item>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
<!-- 底部 -->
|
||
<su-fixed
|
||
v-if="state.list.length > 0"
|
||
:isInset="false"
|
||
:val="valNum"
|
||
:zIndex="70"
|
||
bottom
|
||
placeholder
|
||
>
|
||
<view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
|
||
<view class="ss-flex ss-col-center flex1">
|
||
<label class="check-box ss-flex ss-col-center ss-p-r-30 flex1" @tap="onSelectAll">
|
||
<radio
|
||
:checked="state.isAllSelected"
|
||
color="var(--color32)"
|
||
style="transform: scale(0.8)"
|
||
backgroundColor="var(--color31)"
|
||
@tap.stop="onSelectAll"
|
||
/>
|
||
<view class="ss-m-l-8 c0"> 全选</view>
|
||
</label>
|
||
<view class="flex-center" style="margin-right: 14rpx">
|
||
<text class="size26 c0">总计:</text>
|
||
|
||
<view class="price-text c13">
|
||
<span class="f500">¥</span>
|
||
<span class="priceNum">
|
||
{{ fen2yuan(state.totalPriceSelected) }}
|
||
</span>
|
||
</view>
|
||
<!-- <view class="text-price price-text size34" style="color: #ee0a24">
|
||
{{ fen2yuan(state.totalPriceSelected) }}
|
||
</view> -->
|
||
</view>
|
||
</view>
|
||
<view class="footer-right">
|
||
<button
|
||
v-if="state.editMode"
|
||
class="ss-reset-button pay-btn ui-Shadow-Main"
|
||
@tap="onDelete"
|
||
>
|
||
删除
|
||
</button>
|
||
<button
|
||
v-else
|
||
class="ss-reset-button pay-btn ui-Shadow-Main checkout-btn"
|
||
@tap="onConfirm"
|
||
>
|
||
去结算
|
||
{{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
|
||
</button>
|
||
</view>
|
||
</view>
|
||
</su-fixed>
|
||
</view>
|
||
</s-pages>
|
||
</template>
|
||
<script setup>
|
||
import { ref } from 'vue';
|
||
import sheep from '@/sheep';
|
||
import { onShow, onLoad } from '@dcloudio/uni-app';
|
||
import SpuApi from '@/sheep/api/product/spu';
|
||
import { computed, reactive } from 'vue';
|
||
import { fen2yuan } from '@/sheep/hooks/useGoods';
|
||
import { isEmpty } from '@/sheep/helper/utils';
|
||
onLoad(() => {
|
||
uni.hideNavigationBarLoading();
|
||
});
|
||
// 隐藏原生tabBar
|
||
// uni.hideTabBar({
|
||
// fail: () => {},
|
||
// });
|
||
|
||
const sys_navBar = sheep.$platform.navbar;
|
||
const cart = sheep.$store('cart');
|
||
|
||
const state = reactive({
|
||
editMode: computed(() => cart.editMode),
|
||
list: computed(() => cart.list),
|
||
selectedList: [],
|
||
selectedIds: computed(() => cart.selectedIds),
|
||
isAllSelected: computed(() => cart.isAllSelected),
|
||
totalPriceSelected: computed(() => cart.totalPriceSelected),
|
||
});
|
||
|
||
// 单选选中
|
||
function onSelectSingle(id) {
|
||
cart.selectSingle(id);
|
||
}
|
||
|
||
// 编辑、取消
|
||
function onChangeEditMode(flag) {
|
||
cart.onChangeEditMode(flag);
|
||
}
|
||
|
||
// 全选
|
||
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;
|
||
}
|
||
// if (fen2yuan(state.totalPriceSelected) >= 5000) {
|
||
// sheep.$helper.toast('订单金额不能超过5000元, 请重新选择商品');
|
||
// 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) {
|
||
//console.log(e);
|
||
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);
|
||
}
|
||
|
||
function getCartList() {
|
||
cart.getList();
|
||
}
|
||
|
||
onShow(() => {
|
||
getCartList();
|
||
});
|
||
const goInfo = (id) => {
|
||
uni.navigateTo({
|
||
url: '/pages/goods/info?id=' + id,
|
||
});
|
||
};
|
||
const valNum = ref(uni.upx2px(100));
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
:deep(.ui-fixed) {
|
||
height: 72rpx;
|
||
}
|
||
|
||
.cart-nav-right {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-end;
|
||
height: 100%;
|
||
}
|
||
|
||
.cart-nav-right__btn {
|
||
font-size: 28rpx;
|
||
line-height: 1;
|
||
padding: 10rpx 8rpx;
|
||
}
|
||
|
||
.cart-box {
|
||
width: 100%;
|
||
box-sizing: border-box;
|
||
border-radius: 0 0 16rpx 16rpx;
|
||
overflow: hidden;
|
||
|
||
.cart-header {
|
||
height: 70rpx;
|
||
width: 100%;
|
||
// position: fixed;
|
||
// left: 0;
|
||
// top: v-bind('sys_navBar') rpx;
|
||
z-index: 1000;
|
||
box-sizing: border-box;
|
||
border-radius: 16rpx 16rpx 0 0;
|
||
}
|
||
|
||
.cart-footer {
|
||
height: 100rpx;
|
||
background-color: var(--color16);
|
||
box-shadow: 0px 0px 18px 0px rgba(0, 0, 0, 0.04);
|
||
|
||
.pay-btn {
|
||
width: 180rpx;
|
||
height: 70rpx;
|
||
font-size: 28rpx;
|
||
line-height: 28rpx;
|
||
font-weight: 500;
|
||
border-radius: 40rpx;
|
||
}
|
||
}
|
||
|
||
.cart-content {
|
||
.goods-box {
|
||
position: relative;
|
||
margin-bottom: 22rpx;
|
||
}
|
||
}
|
||
}
|
||
.price-text {
|
||
.priceNum {
|
||
font-size: 36rpx;
|
||
font-weight: 600;
|
||
}
|
||
}
|
||
.ss-reset-button {
|
||
color: #ffffff;
|
||
}
|
||
.ss-reset-button,
|
||
.ss-reset-button.checkout-btn {
|
||
background: linear-gradient(
|
||
to bottom right,
|
||
var(--accent-grad-start) 0%,
|
||
var(--accent-grad-end) 100%
|
||
);
|
||
}
|
||
|
||
:deep(.uni-numbox) {
|
||
height: 48rpx;
|
||
border-radius: 999rpx;
|
||
border: 2rpx solid rgba(0, 0, 0, 0.12);
|
||
background: #ffffff;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
:deep(.uni-numbox__value) {
|
||
height: 44rpx;
|
||
line-height: 44rpx;
|
||
margin: 0;
|
||
width: 60rpx;
|
||
background: transparent !important;
|
||
font-size: 26rpx;
|
||
color: #333333 !important;
|
||
}
|
||
|
||
:deep(.uni-numbox .move-round),
|
||
:deep(.uni-numbox .add-round) {
|
||
width: 44rpx;
|
||
height: 44rpx;
|
||
line-height: 44rpx;
|
||
background: transparent !important;
|
||
font-size: 30rpx;
|
||
color: #333333 !important;
|
||
border-radius: 50%;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
:deep(.uni-numbox .move-round) {
|
||
border-right: 2rpx solid rgba(0, 0, 0, 0.12);
|
||
border-radius: 999rpx 0 0 999rpx;
|
||
}
|
||
|
||
:deep(.uni-numbox .add-round) {
|
||
border-left: 2rpx solid rgba(0, 0, 0, 0.12);
|
||
border-radius: 0 999rpx 999rpx 0;
|
||
}
|
||
|
||
</style>
|