611 lines
18 KiB
Vue
Raw Normal View History

2025-04-11 17:32:55 +08:00
<template>
2025-05-20 13:05:37 +08:00
<div>
<common current-name="agricultural">
<template #main>
<div class="my-shoping-car-warp">
2025-06-11 15:02:43 +08:00
<userHeader :title="'我的购物车'"></userHeader>
2025-05-20 13:05:37 +08:00
<div class="page-content-warp">
<div class="fix-top">
<div class="do-all" @click="toCheckAll">
<div class="do-all-pos">
<ischeck :value="isAll"></ischeck>
<span class="all-txt">全选</span>
</div>
2025-04-11 17:32:55 +08:00
</div>
2025-05-20 13:05:37 +08:00
<div class="batch-del" @click="doBatchDel">
<el-icon><Delete /></el-icon>
<span class="del-txt">批量删除</span>
2025-04-11 17:32:55 +08:00
</div>
</div>
2025-06-06 13:28:38 +08:00
<div v-if="data.length <= 0" class="empty">
<img style="width: 300px; height: 300px" src="../../assets/images/empty.png" alt="" />
</div>
<div v-else class="conetnt-warp">
2025-05-20 13:05:37 +08:00
<div class="content-item-warp">
2025-06-06 13:28:38 +08:00
<div v-for="(n, index) in data" :key="n.id" class="content-item">
2025-06-11 14:35:58 +08:00
<!-- <div class="shop-img">
2025-05-20 13:05:37 +08:00
<costomImg
:url="'images/ecommerce/' + 'pic.png'"
:preview-list="[getAssetsFile('images/ecommerce/' + 'pic.png')?.href ?? '']"
:is-view="false"
></costomImg>
2025-06-10 13:54:50 +08:00
</div> -->
2025-06-11 14:35:58 +08:00
<!-- <div class="shop-info" @click="toCheckShop(index)">
<div class="shop-do">
<ischeck :value="n.ischeck"></ischeck>
</div>
2025-06-06 13:28:38 +08:00
<span class="shop-name txt-ellipsis clamp2">{{ n.cartName }}</span>
2025-06-11 14:35:58 +08:00
</div> -->
2025-05-20 13:05:37 +08:00
2025-06-06 13:28:38 +08:00
<div v-if="n.cartDetails && n.cartDetails.length > 0" class="good-list">
<div v-for="(g, indexg) in n.cartDetails" :key="indexg" class="good-item">
2025-05-20 13:05:37 +08:00
<div class="good-do" @click="toCheckGood(index, indexg)">
<div class="good-do-pos">
<ischeck :value="g.ischeck" size="24px"></ischeck>
</div>
</div>
<div class="good-img" @click="toCheckGood(index, indexg)">
2025-06-06 13:28:38 +08:00
<!-- <costomImg :url="g.productImgUrl" :is-view="false"></costomImg> -->
<img class="good-img" style="border-radius: 5px" :src="g.productImgUrl.split('?')[0]" alt="" />
2025-05-20 13:05:37 +08:00
</div>
<div class="good-info" @click="toCheckGood(index, indexg)">
<div class="good-info-pos">
2025-06-06 13:28:38 +08:00
<div class="txt-ellipsis clamp2">{{ g.productName || '--' }}</div>
2025-05-20 13:05:37 +08:00
</div>
</div>
<div class="good-price-num">
<div class="good-price-num-pos">
<div class="price" @click="toCheckGood(index, indexg)">{{ g.price }} / {{ g.unit }}</div>
2025-06-06 13:28:38 +08:00
<div class="total" @click="toCheckGood(index, indexg)">{{ (g.price * g.quantity).toFixed(2) }}</div>
2025-05-20 13:05:37 +08:00
<div class="num">
<div class="right-item">
2025-06-06 13:28:38 +08:00
<el-input-number v-model="g.quantity" :min="1" @change="numberChange(g.quantity, index, indexg)">
2025-05-20 13:05:37 +08:00
<template #suffix>
<span>{{ g.unit }}</span>
</template>
</el-input-number>
</div>
</div>
<div class="good-del" @click="doSingleDel(index, indexg)">
<span>删除</span>
</div>
</div>
2025-04-11 17:32:55 +08:00
</div>
</div>
</div>
</div>
</div>
</div>
2025-05-20 13:05:37 +08:00
<div class="fix-bottom">
<div class="bottom-total">
<span class="tips">合计</span>
<span class="total">{{ totalAmout.toFixed(2) }}</span>
</div>
<div class="bottom-do">
2025-06-06 13:28:38 +08:00
<el-button :disabled="data.length == 0" :type="data.length != 0 ? 'primary' : 'info'" @click="toSettlement">结算</el-button>
2025-05-20 13:05:37 +08:00
</div>
</div>
</div>
2025-04-11 17:32:55 +08:00
</div>
2025-05-20 13:05:37 +08:00
</template>
</common>
2025-04-11 17:32:55 +08:00
</div>
</template>
<script setup>
2025-05-20 13:05:37 +08:00
import common from './components/common.vue';
2025-06-06 13:28:38 +08:00
import { ref, reactive, computed, onMounted } from 'vue';
2025-04-11 17:32:55 +08:00
import { isEmpty, getAssetsFile } from '@/utils';
import { useRoute, useRouter } from 'vue-router';
import userHeader from './components/userHeader.vue';
import ischeck from './components/ischeck.vue';
import costomImg from '@/components/costomImg.vue';
import { useApp } from '@/hooks';
2025-06-06 13:28:38 +08:00
import { shoppingCart, deleteChooseGoods, addToCart, delCartGood, setGoodsCount, commitOrder } from '../../apis/user';
2025-04-11 17:32:55 +08:00
const app = useApp();
const route = useRoute();
const router = useRouter();
2025-06-06 13:28:38 +08:00
let number = ref(1);
2025-04-11 17:32:55 +08:00
let total = ref(99);
let isAll = ref(false);
2025-06-06 13:28:38 +08:00
let data = ref([]);
let page = reactive({
current: 1,
2025-06-11 13:53:06 +08:00
size: 100,
2025-06-06 13:28:38 +08:00
});
let ids = ref([]);
2025-04-11 17:32:55 +08:00
let datalist = reactive([
{
id: '01',
shop: '银河生态农产品有限公司',
shopimg: '',
ischeck: false,
goodlist: [
{ id: '001', title: '耿马镇 原生态 有机 西红柿', price: 4.9, unit: '份', num: 2, ischeck: false },
{ id: '002', title: '耿马镇 原生态 有机 西蓝花', price: 2.6, unit: '份', num: 100, ischeck: false },
],
},
{
id: '02',
shop: '方立生态农产品有限公司',
shopimg: '',
ischeck: false,
goodlist: [
{ id: '001', title: '勐撒镇 原生态 有机 大白菜', price: 4.9, unit: '/份', num: 2, ischeck: false },
{ id: '002', title: '勐撒镇 原生态 有机 生姜', price: 2.6, unit: '/份', num: 100, ischeck: false },
],
},
{
id: '03',
shop: '佳佳生态农产品有限公司',
shopimg: '',
ischeck: false,
goodlist: [
{ id: '001', title: '勐简镇 原生态 有机 花香蓝莓', price: 4.9, unit: '/份', num: 2, ischeck: false },
{ id: '002', title: '勐简镇 原生态古树茶', price: 2.6, unit: '/份', num: 100, ischeck: false },
],
},
]);
2025-06-06 13:28:38 +08:00
onMounted(() => {
shoppingCart(page).then((res) => {
console.log(res.data.records);
data.value = res.data.records;
addIsCheckProperty(data);
});
});
function addIsCheckProperty(data) {
if (Array.isArray(data)) {
data.forEach((item) => {
item.ischeck = false; // 为当前层级添加属性
if (item.cartDetails) {
// 处理子集
addIsCheckProperty(item.cartDetails);
}
if (item.records) {
// 处理父级(如存在嵌套结构)
addIsCheckProperty(item.records);
}
});
} else if (typeof data === 'object' && data !== null) {
data.ischeck = false;
for (let key in data) {
if (typeof data[key] === 'object') {
addIsCheckProperty(data[key]); // 递归处理嵌套对象
}
}
}
}
const numberChange = (value, index, indexg) => {
console.log(value, index, indexg);
setGoodsCount({ goodsId: data.value[index].cartDetails[indexg].id, count: value });
};
2025-04-11 17:32:55 +08:00
let totalAmout = computed(() => {
let num = 0;
let list = [];
2025-06-06 13:28:38 +08:00
if (data && data.value.length > 0) {
list = data.value
2025-04-11 17:32:55 +08:00
.map((m) => {
2025-06-06 13:28:38 +08:00
return m.cartDetails;
2025-04-11 17:32:55 +08:00
})
.flat();
if (list && list.length > 0) {
num = list.reduce((acc, current) => {
2025-06-06 13:28:38 +08:00
return acc + (current.ischeck ? current.price * current.quantity : 0);
2025-04-11 17:32:55 +08:00
}, 0);
}
}
return num;
});
2025-04-14 10:59:16 +08:00
const toSettlement = () => {
2025-06-06 13:28:38 +08:00
commitOrder({ chooseChartDetails: ids.value }).then((res) => {
// router.push('/sub-operation-service/sureOrder');
router.push({
path: '/sub-operation-service/sureOrder',
query: { id: res.data },
});
});
2025-04-14 10:59:16 +08:00
};
2025-04-11 17:32:55 +08:00
const toCheckAll = () => {
isAll.value = !isAll.value;
console.info('操作全选', isAll.value);
2025-06-06 13:28:38 +08:00
if (data && data.value.length > 0) {
data.value.forEach((m) => {
2025-04-11 17:32:55 +08:00
m.ischeck = isAll.value;
2025-06-06 13:28:38 +08:00
if (m.cartDetails && m.cartDetails.length > 0) {
m.cartDetails = setCheck(m.cartDetails, isAll.value);
2025-06-11 14:35:58 +08:00
if (isAll.value) {
m.cartDetails.forEach((res, index) => {
ids.value.push(res.id);
});
} else {
ids.value = [];
}
2025-04-11 17:32:55 +08:00
}
});
}
2025-06-11 14:35:58 +08:00
console.log(ids.value);
2025-04-11 17:32:55 +08:00
};
const setCheck = (data, val) => {
let list = [];
if (data && data.length > 0) {
list = data.map((m) => {
return { ...m, ischeck: val };
});
}
return list;
};
const toCheckShop = (index) => {
if (index > -1) {
2025-06-06 13:28:38 +08:00
data.value[index].ischeck = !data.value[index].ischeck;
if (data.value[index].cartDetails && data.value[index].cartDetails.length > 0) {
data.value[index].cartDetails = setCheck(data.value[index].cartDetails, data.value[index].ischeck);
2025-04-11 17:32:55 +08:00
}
setIsAll();
}
};
const toCheckGood = (indexP, index) => {
2025-06-06 13:28:38 +08:00
const indexs = ids.value.indexOf(data.value[indexP].cartDetails[index].id);
if (indexs !== -1) {
// 如果值存在则删除
ids.value.splice(indexs, 1);
} else {
// 如果值不存在则添加
ids.value.push(data.value[indexP].cartDetails[index].id);
}
2025-06-11 13:53:06 +08:00
console.log(ids.value);
2025-04-11 17:32:55 +08:00
if (indexP > -1 && index > -1) {
2025-06-06 13:28:38 +08:00
let list = data.value[indexP].cartDetails;
2025-04-11 17:32:55 +08:00
list[index].ischeck = !list[index].ischeck;
let len = list.length || 0;
let checkNum = list.reduce((acc, current) => {
return acc + (current.ischeck ? 1 : 0);
}, 0);
if (checkNum > 0 && checkNum < len) {
2025-06-06 13:28:38 +08:00
data.value[indexP].ischeck = false;
2025-04-11 17:32:55 +08:00
} else if (checkNum > 0 && checkNum == len) {
2025-06-06 13:28:38 +08:00
data.value[indexP].ischeck = true;
2025-04-11 17:32:55 +08:00
} else {
2025-06-06 13:28:38 +08:00
data.value[indexP].ischeck = false;
2025-04-11 17:32:55 +08:00
}
setIsAll();
}
};
const setIsAll = () => {
2025-06-06 13:28:38 +08:00
let len = data.value.length || 0;
let checkNum = data.value.reduce((acc, current) => {
2025-04-11 17:32:55 +08:00
return acc + (current.ischeck ? 1 : 0);
}, 0);
if (checkNum > 0 && checkNum < len) {
isAll.value = false;
} else if (checkNum > 0 && checkNum == len) {
isAll.value = true;
} else {
isAll.value = false;
}
};
const doSingleDel = (indexP, index) => {
2025-06-11 13:53:06 +08:00
let list = [];
list.push(data.value[indexP].cartDetails[index].id);
2025-04-11 17:32:55 +08:00
if (indexP > -1 && index > -1) {
app
.$confirm(`删除后信息将不可查看,确认要删除吗?`, '确定删除', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
2025-06-11 13:53:06 +08:00
deleteChooseGoods({ goodsIds: list.join(',') });
2025-06-06 13:28:38 +08:00
data.value[indexP].cartDetails.splice(index, 1);
2025-04-11 17:32:55 +08:00
})
2025-06-11 13:53:06 +08:00
.catch((err) => {
console.log(err);
});
2025-04-11 17:32:55 +08:00
}
};
2025-05-20 13:05:37 +08:00
//批量删除
const doBatchDel = () => {
2025-06-06 13:28:38 +08:00
const allUnchecked = isAllUnchecked(data.value);
2025-05-20 13:05:37 +08:00
console.log(allUnchecked); // true 或 false
if (allUnchecked) {
app.$message({
message: '请先选择要删除的商品',
type: 'warning',
});
} else {
app
.$confirm(`删除后信息将不可查看,确认要删除吗?`, '确定删除', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
// 删除逻辑
2025-06-11 13:53:06 +08:00
// const filteredData = removeCheckedItems(data);
2025-05-20 13:05:37 +08:00
// console.log(filteredData);
2025-06-11 13:53:06 +08:00
// data.value = filteredData;
2025-06-11 14:35:58 +08:00
deleteChooseGoods({ goodsIds: ids.value.join(',') });
setTimeout(() => {
shoppingCart(page).then((res) => {
console.log(res.data.records);
data.value = res.data.records;
addIsCheckProperty(data);
});
}, 500);
2025-05-20 13:05:37 +08:00
})
.catch(() => {});
}
};
// 判断是否全部未选中
function isAllUnchecked(data) {
return data.every((item) => {
// 检查当前项的 ischeck 是否为 false
const currentUnchecked = item.ischeck === false;
// 如果有子项 goodlist递归检查子项
2025-06-06 13:28:38 +08:00
const childrenUnchecked = item.cartDetails ? item.cartDetails.every((child) => child.ischeck === false) : true;
2025-05-20 13:05:37 +08:00
return currentUnchecked && childrenUnchecked;
});
}
//批量删除逻辑
function removeCheckedItems(data) {
return data.filter((item) => {
// 如果父项的 ischeck 为 true直接过滤掉包括所有子项
if (item.ischeck === true) {
return false;
}
// 如果父项的 ischeck 为 false但子项可能存在 ischeck 为 true 的情况
2025-06-06 13:28:38 +08:00
if (item.cartDetails) {
item.cartDetails = item.cartDetails.filter((child) => child.ischeck !== true);
2025-05-20 13:05:37 +08:00
}
return true;
});
}
2025-04-11 17:32:55 +08:00
</script>
<style lang="scss" scoped>
2025-06-06 13:28:38 +08:00
.empty {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 500px;
}
2025-04-11 17:32:55 +08:00
.my-shoping-car-warp {
width: 100%;
.page-content-warp {
position: relative;
2025-04-14 10:59:16 +08:00
overflow: hidden;
2025-06-10 13:54:50 +08:00
margin: 16px 0 0;
2025-05-20 13:05:37 +08:00
width: 100%;
height: calc(100vh - 135px);
border-radius: 16px;
background: $color-fff;
2025-04-11 17:32:55 +08:00
.fix-top,
.fix-bottom {
position: absolute;
left: 0;
z-index: 1;
2025-05-20 13:05:37 +08:00
padding: 16px;
width: 100%;
2025-04-11 17:32:55 +08:00
background: $color-fff;
}
.fix-top {
top: 0;
display: inline-flex;
justify-content: space-between;
.do-all,
.batch-del {
display: inline-block;
vertical-align: middle;
2025-05-20 13:05:37 +08:00
cursor: pointer;
2025-04-11 17:32:55 +08:00
}
.do-all {
display: inline-flex;
flex-direction: column;
justify-content: center;
.all-txt {
display: inline-block;
padding-left: 16px;
2025-05-20 13:05:37 +08:00
font-size: 16px;
vertical-align: middle;
2025-04-11 17:32:55 +08:00
}
}
.batch-del {
padding: 8px 16px;
font-size: 16px;
2025-05-20 13:05:37 +08:00
border: 1px solid $color-333;
border-radius: 16px;
2025-04-11 17:32:55 +08:00
.el-icon {
font-size: 18px;
}
.del-txt,
.el-icon {
display: inline-block;
vertical-align: middle;
}
.del-txt {
padding-left: 16px;
}
}
}
.fix-bottom {
bottom: 0;
display: inline-flex;
justify-content: space-between;
width: 100%;
.bottom-total,
.bottom-do {
display: inline-block;
vertical-align: middle;
}
.bottom-total {
.tips,
.total {
display: inline-block;
vertical-align: middle;
}
.tips {
font-size: 30px;
}
.total {
2025-05-20 13:05:37 +08:00
padding-left: 16px;
2025-04-11 17:32:55 +08:00
font-size: 42px;
color: $color-main;
}
.total::before {
content: '¥';
}
}
.bottom-do {
display: inline-flex;
2025-05-20 13:05:37 +08:00
justify-content: center;
flex-direction: column;
2025-04-11 17:32:55 +08:00
::v-deep() {
.el-button {
2025-05-20 13:05:37 +08:00
display: inline-block !important;
2025-04-11 17:32:55 +08:00
padding: 0 40px !important;
height: 42px !important;
2025-05-20 13:05:37 +08:00
font-size: 18px !important;
line-height: 42px !important;
2025-04-11 17:32:55 +08:00
}
}
}
}
.conetnt-warp {
overflow-y: auto;
2025-05-20 13:05:37 +08:00
padding: 80px 16px 96px;
width: 100%;
2025-04-11 17:32:55 +08:00
height: 100%;
.content-item-warp {
width: 100%;
.content-item {
margin-bottom: 16px;
2025-05-20 13:05:37 +08:00
width: 100%;
2025-04-11 17:32:55 +08:00
cursor: pointer;
.shop-info {
display: inline-flex;
justify-content: flex-start;
2025-05-20 13:05:37 +08:00
width: 100%;
2025-04-11 17:32:55 +08:00
gap: 16px;
.shop-do,
.shop-img,
.shop-name {
display: inline-block;
vertical-align: middle;
}
.shop-img {
display: inline-flex;
justify-content: center;
2025-05-20 13:05:37 +08:00
width: 32px;
height: 32px;
2025-04-11 17:32:55 +08:00
border-radius: 4px;
2025-05-20 13:05:37 +08:00
flex-direction: column;
2025-04-11 17:32:55 +08:00
}
.shop-do {
display: inline-flex;
justify-content: center;
2025-05-20 13:05:37 +08:00
width: 30px;
flex-direction: column;
2025-04-11 17:32:55 +08:00
}
.shop-name {
width: calc(100% - 62px);
font-size: 18px;
2025-05-20 13:05:37 +08:00
text-align: left;
2025-04-11 17:32:55 +08:00
}
}
.good-list {
width: 100%;
.good-item {
display: inline-flex;
justify-content: flex-start;
margin: 8px 0;
2025-05-20 13:05:37 +08:00
padding-left: 16px;
width: 100%;
gap: 16px;
2025-04-11 17:32:55 +08:00
}
.good-do,
.good-img,
.good-info,
.good-price-num {
display: inline-block;
vertical-align: middle;
}
.good-do {
display: inline-flex;
flex-direction: column;
justify-content: center;
.good-do-pos {
}
}
.good-img {
width: 120px;
height: 120px;
}
.good-info {
display: inline-flex;
justify-content: center;
2025-05-20 13:05:37 +08:00
width: 200px;
flex-direction: column;
2025-04-11 17:32:55 +08:00
.good-info-pos {
font-size: 18px;
color: $color-666;
2025-05-20 13:05:37 +08:00
.txt-ellipsis {
text-align: left;
}
2025-04-11 17:32:55 +08:00
}
}
.good-price-num {
display: inline-flex;
justify-content: center;
2025-05-20 13:05:37 +08:00
width: calc(100% - 340px);
2025-04-11 17:32:55 +08:00
flex-direction: column;
.good-price-num-pos {
display: inline-flex;
justify-content: space-around;
gap: 16px;
.price,
.total {
font-size: 20px;
}
.price {
font-weight: 400;
}
.total {
font-weight: 700;
2025-05-20 13:05:37 +08:00
color: $color-main;
2025-04-11 17:32:55 +08:00
}
.total::before {
content: '¥';
}
.good-del {
font-size: 16px;
2025-05-20 13:05:37 +08:00
color: $color-999;
2025-04-11 17:32:55 +08:00
}
}
}
}
}
}
}
}
}
</style>