611 lines
18 KiB
Vue
611 lines
18 KiB
Vue
<template>
|
||
<div>
|
||
<common current-name="agricultural">
|
||
<template #main>
|
||
<div class="my-shoping-car-warp">
|
||
<userHeader :title="'我的购物车'"></userHeader>
|
||
<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>
|
||
</div>
|
||
<div class="batch-del" @click="doBatchDel">
|
||
<el-icon><Delete /></el-icon>
|
||
<span class="del-txt">批量删除</span>
|
||
</div>
|
||
</div>
|
||
<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">
|
||
<div class="content-item-warp">
|
||
<div v-for="(n, index) in data" :key="n.id" class="content-item">
|
||
<!-- <div class="shop-img">
|
||
<costomImg
|
||
:url="'images/ecommerce/' + 'pic.png'"
|
||
:preview-list="[getAssetsFile('images/ecommerce/' + 'pic.png')?.href ?? '']"
|
||
:is-view="false"
|
||
></costomImg>
|
||
</div> -->
|
||
<!-- <div class="shop-info" @click="toCheckShop(index)">
|
||
<div class="shop-do">
|
||
<ischeck :value="n.ischeck"></ischeck>
|
||
</div>
|
||
<span class="shop-name txt-ellipsis clamp2">{{ n.cartName }}</span>
|
||
</div> -->
|
||
|
||
<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">
|
||
<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)">
|
||
<!-- <costomImg :url="g.productImgUrl" :is-view="false"></costomImg> -->
|
||
<img class="good-img" style="border-radius: 5px" :src="g.productImgUrl.split('?')[0]" alt="" />
|
||
</div>
|
||
<div class="good-info" @click="toCheckGood(index, indexg)">
|
||
<div class="good-info-pos">
|
||
<div class="txt-ellipsis clamp2">{{ g.productName || '--' }}</div>
|
||
</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>
|
||
<div class="total" @click="toCheckGood(index, indexg)">{{ (g.price * g.quantity).toFixed(2) }}</div>
|
||
<div class="num">
|
||
<div class="right-item">
|
||
<el-input-number v-model="g.quantity" :min="1" @change="numberChange(g.quantity, index, indexg)">
|
||
<template #suffix>
|
||
<span>{{ g.unit }}</span>
|
||
</template>
|
||
</el-input-number>
|
||
</div>
|
||
</div>
|
||
<div class="good-del" @click="doSingleDel(index, indexg)">
|
||
<span>删除</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="fix-bottom">
|
||
<div class="bottom-total">
|
||
<span class="tips">合计</span>
|
||
<span class="total">{{ totalAmout.toFixed(2) }}</span>
|
||
</div>
|
||
<div class="bottom-do">
|
||
<el-button :disabled="data.length == 0" :type="data.length != 0 ? 'primary' : 'info'" @click="toSettlement">结算</el-button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
</common>
|
||
</div>
|
||
</template>
|
||
<script setup>
|
||
import common from './components/common.vue';
|
||
import { ref, reactive, computed, onMounted } from 'vue';
|
||
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';
|
||
import { shoppingCart, deleteChooseGoods, addToCart, delCartGood, setGoodsCount, commitOrder } from '../../apis/user';
|
||
|
||
const app = useApp();
|
||
const route = useRoute();
|
||
const router = useRouter();
|
||
|
||
let number = ref(1);
|
||
let total = ref(99);
|
||
let isAll = ref(false);
|
||
let data = ref([]);
|
||
let page = reactive({
|
||
current: 1,
|
||
size: 100,
|
||
});
|
||
let ids = ref([]);
|
||
|
||
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 },
|
||
],
|
||
},
|
||
]);
|
||
|
||
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 });
|
||
};
|
||
|
||
let totalAmout = computed(() => {
|
||
let num = 0;
|
||
let list = [];
|
||
if (data && data.value.length > 0) {
|
||
list = data.value
|
||
.map((m) => {
|
||
return m.cartDetails;
|
||
})
|
||
.flat();
|
||
if (list && list.length > 0) {
|
||
num = list.reduce((acc, current) => {
|
||
return acc + (current.ischeck ? current.price * current.quantity : 0);
|
||
}, 0);
|
||
}
|
||
}
|
||
|
||
return num;
|
||
});
|
||
|
||
const toSettlement = () => {
|
||
commitOrder({ chooseChartDetails: ids.value }).then((res) => {
|
||
// router.push('/sub-operation-service/sureOrder');
|
||
router.push({
|
||
path: '/sub-operation-service/sureOrder',
|
||
query: { id: res.data },
|
||
});
|
||
});
|
||
};
|
||
|
||
const toCheckAll = () => {
|
||
isAll.value = !isAll.value;
|
||
console.info('操作全选', isAll.value);
|
||
if (data && data.value.length > 0) {
|
||
data.value.forEach((m) => {
|
||
m.ischeck = isAll.value;
|
||
if (m.cartDetails && m.cartDetails.length > 0) {
|
||
m.cartDetails = setCheck(m.cartDetails, isAll.value);
|
||
if (isAll.value) {
|
||
m.cartDetails.forEach((res, index) => {
|
||
ids.value.push(res.id);
|
||
});
|
||
} else {
|
||
ids.value = [];
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
console.log(ids.value);
|
||
};
|
||
|
||
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) {
|
||
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);
|
||
}
|
||
setIsAll();
|
||
}
|
||
};
|
||
|
||
const toCheckGood = (indexP, index) => {
|
||
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);
|
||
}
|
||
console.log(ids.value);
|
||
if (indexP > -1 && index > -1) {
|
||
let list = data.value[indexP].cartDetails;
|
||
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) {
|
||
data.value[indexP].ischeck = false;
|
||
} else if (checkNum > 0 && checkNum == len) {
|
||
data.value[indexP].ischeck = true;
|
||
} else {
|
||
data.value[indexP].ischeck = false;
|
||
}
|
||
setIsAll();
|
||
}
|
||
};
|
||
|
||
const setIsAll = () => {
|
||
let len = data.value.length || 0;
|
||
let checkNum = data.value.reduce((acc, current) => {
|
||
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) => {
|
||
let list = [];
|
||
list.push(data.value[indexP].cartDetails[index].id);
|
||
if (indexP > -1 && index > -1) {
|
||
app
|
||
.$confirm(`删除后信息将不可查看,确认要删除吗?`, '确定删除', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning',
|
||
})
|
||
.then(() => {
|
||
deleteChooseGoods({ goodsIds: list.join(',') });
|
||
data.value[indexP].cartDetails.splice(index, 1);
|
||
})
|
||
.catch((err) => {
|
||
console.log(err);
|
||
});
|
||
}
|
||
};
|
||
//批量删除
|
||
const doBatchDel = () => {
|
||
const allUnchecked = isAllUnchecked(data.value);
|
||
console.log(allUnchecked); // true 或 false
|
||
if (allUnchecked) {
|
||
app.$message({
|
||
message: '请先选择要删除的商品',
|
||
type: 'warning',
|
||
});
|
||
} else {
|
||
app
|
||
.$confirm(`删除后信息将不可查看,确认要删除吗?`, '确定删除', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning',
|
||
})
|
||
.then(() => {
|
||
// 删除逻辑
|
||
// const filteredData = removeCheckedItems(data);
|
||
// console.log(filteredData);
|
||
// data.value = filteredData;
|
||
deleteChooseGoods({ goodsIds: ids.value.join(',') });
|
||
setTimeout(() => {
|
||
shoppingCart(page).then((res) => {
|
||
console.log(res.data.records);
|
||
data.value = res.data.records;
|
||
addIsCheckProperty(data);
|
||
});
|
||
}, 500);
|
||
})
|
||
.catch(() => {});
|
||
}
|
||
};
|
||
// 判断是否全部未选中
|
||
function isAllUnchecked(data) {
|
||
return data.every((item) => {
|
||
// 检查当前项的 ischeck 是否为 false
|
||
const currentUnchecked = item.ischeck === false;
|
||
// 如果有子项 goodlist,递归检查子项
|
||
const childrenUnchecked = item.cartDetails ? item.cartDetails.every((child) => child.ischeck === false) : true;
|
||
return currentUnchecked && childrenUnchecked;
|
||
});
|
||
}
|
||
//批量删除逻辑
|
||
function removeCheckedItems(data) {
|
||
return data.filter((item) => {
|
||
// 如果父项的 ischeck 为 true,直接过滤掉(包括所有子项)
|
||
if (item.ischeck === true) {
|
||
return false;
|
||
}
|
||
|
||
// 如果父项的 ischeck 为 false,但子项可能存在 ischeck 为 true 的情况
|
||
if (item.cartDetails) {
|
||
item.cartDetails = item.cartDetails.filter((child) => child.ischeck !== true);
|
||
}
|
||
|
||
return true;
|
||
});
|
||
}
|
||
</script>
|
||
<style lang="scss" scoped>
|
||
.empty {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 100%;
|
||
height: 500px;
|
||
}
|
||
.my-shoping-car-warp {
|
||
width: 100%;
|
||
.page-content-warp {
|
||
position: relative;
|
||
overflow: hidden;
|
||
margin: 16px 0 0;
|
||
width: 100%;
|
||
height: calc(100vh - 135px);
|
||
border-radius: 16px;
|
||
background: $color-fff;
|
||
.fix-top,
|
||
.fix-bottom {
|
||
position: absolute;
|
||
left: 0;
|
||
z-index: 1;
|
||
padding: 16px;
|
||
width: 100%;
|
||
background: $color-fff;
|
||
}
|
||
.fix-top {
|
||
top: 0;
|
||
display: inline-flex;
|
||
justify-content: space-between;
|
||
.do-all,
|
||
.batch-del {
|
||
display: inline-block;
|
||
vertical-align: middle;
|
||
cursor: pointer;
|
||
}
|
||
.do-all {
|
||
display: inline-flex;
|
||
flex-direction: column;
|
||
justify-content: center;
|
||
.all-txt {
|
||
display: inline-block;
|
||
padding-left: 16px;
|
||
font-size: 16px;
|
||
vertical-align: middle;
|
||
}
|
||
}
|
||
.batch-del {
|
||
padding: 8px 16px;
|
||
font-size: 16px;
|
||
border: 1px solid $color-333;
|
||
border-radius: 16px;
|
||
.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 {
|
||
padding-left: 16px;
|
||
font-size: 42px;
|
||
color: $color-main;
|
||
}
|
||
.total::before {
|
||
content: '¥';
|
||
}
|
||
}
|
||
.bottom-do {
|
||
display: inline-flex;
|
||
justify-content: center;
|
||
flex-direction: column;
|
||
::v-deep() {
|
||
.el-button {
|
||
display: inline-block !important;
|
||
padding: 0 40px !important;
|
||
height: 42px !important;
|
||
font-size: 18px !important;
|
||
line-height: 42px !important;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.conetnt-warp {
|
||
overflow-y: auto;
|
||
padding: 80px 16px 96px;
|
||
width: 100%;
|
||
height: 100%;
|
||
.content-item-warp {
|
||
width: 100%;
|
||
.content-item {
|
||
margin-bottom: 16px;
|
||
width: 100%;
|
||
cursor: pointer;
|
||
.shop-info {
|
||
display: inline-flex;
|
||
justify-content: flex-start;
|
||
width: 100%;
|
||
gap: 16px;
|
||
.shop-do,
|
||
.shop-img,
|
||
.shop-name {
|
||
display: inline-block;
|
||
vertical-align: middle;
|
||
}
|
||
.shop-img {
|
||
display: inline-flex;
|
||
justify-content: center;
|
||
width: 32px;
|
||
height: 32px;
|
||
border-radius: 4px;
|
||
flex-direction: column;
|
||
}
|
||
.shop-do {
|
||
display: inline-flex;
|
||
justify-content: center;
|
||
width: 30px;
|
||
flex-direction: column;
|
||
}
|
||
.shop-name {
|
||
width: calc(100% - 62px);
|
||
font-size: 18px;
|
||
text-align: left;
|
||
}
|
||
}
|
||
.good-list {
|
||
width: 100%;
|
||
.good-item {
|
||
display: inline-flex;
|
||
justify-content: flex-start;
|
||
margin: 8px 0;
|
||
padding-left: 16px;
|
||
width: 100%;
|
||
gap: 16px;
|
||
}
|
||
.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;
|
||
width: 200px;
|
||
flex-direction: column;
|
||
.good-info-pos {
|
||
font-size: 18px;
|
||
color: $color-666;
|
||
.txt-ellipsis {
|
||
text-align: left;
|
||
}
|
||
}
|
||
}
|
||
.good-price-num {
|
||
display: inline-flex;
|
||
justify-content: center;
|
||
width: calc(100% - 340px);
|
||
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;
|
||
color: $color-main;
|
||
}
|
||
.total::before {
|
||
content: '¥';
|
||
}
|
||
.good-del {
|
||
font-size: 16px;
|
||
color: $color-999;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</style>
|