purchase-let/pages/cart/cart.vue

507 lines
12 KiB
Vue
Raw Normal View History

2024-04-25 18:02:30 +08:00
<template>
2024-06-05 18:45:30 +08:00
<view class="wrap">
<up-navbar placeholder :autoBack="true">
<template #center>
<view
style="display: flex;align-items: center;width: 100%;justify-content: flex-start;margin-left: 100rpx;">
<view class="tabs" :class="{'tabs-active': tabsActive==index}" v-for="(item, index) in list"
:key="index" @click="changeTab(index)">
{{item}}
</view>
</view>
</template>
</up-navbar>
<swiper class="swiper-box" :current="swiperCurrent" @animationfinish="animationfinish">
<swiper-item class="swiper-item">
<scroll-view scroll-y @refresherrefresh="refresherrefresh(1)" :refresher-triggered="triggered"
refresher-enabled refresher-background="#F6F6F6" style="height: 100%;width: 100%;">
2024-06-14 10:54:40 +08:00
<view class="page-box1">
<view class="total" @click='isAdmin=!isAdmin'>
2024-06-05 18:45:30 +08:00
<view>共计<text style="color: #20B128;">{{cartList.length}}</text></view>
2024-06-14 10:54:40 +08:00
<view v-if="!isAdmin">管理</view>
<view v-else>完成</view>
2024-06-05 18:45:30 +08:00
</view>
<view v-if="cartList.length>0" class="list">
<view class="shop-item" v-for="(item, index) in cartList" :key="index"
@click="openGoodPopup(item)">
<view class="shop-check" @click.stop="checkItem(item, !item.check)">
<image v-if="!item.check" src="@/static/icon/n-check.png"></image>
<image v-else src="@/static/icon/check.png"></image>
</view>
<image class="shop-img" :src="item.imgs"></image>
<view class="shop-content">
<view class="title">
<view class="name u-line-2">{{item.goods_name}}</view>
<view class="tip u-line-1">{{item.unit_name}}</view>
2024-06-26 14:21:00 +08:00
<view v-if="priceKey.off_activity==1" class="tip u-line-1">原价<text
style="text-decoration: line-through;">{{item[priceKey.op_price]}}</text>
2024-06-26 14:06:40 +08:00
</view>
2024-06-05 18:45:30 +08:00
</view>
<view class="price-btn">
2024-06-26 14:21:00 +08:00
<view class="price">{{item[priceKey.price]}}</view>
2024-06-05 18:45:30 +08:00
<view class="btn">
<view class="num">{{item.cart_num}}</view>
<u--icon name="plus-circle-fill" size="20" color="#20b128"></u--icon>
</view>
</view>
</view>
</view>
</view>
<view v-else style="margin-top: 100rpx;">
<up-empty text="购物车空空如也"
icon="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/29955202404260944367594.png">
</up-empty>
</view>
<view style="width: 100%;height: 200rpx;"></view>
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiper-item">
<scroll-view scroll-y @refresherrefresh="refresherrefresh(2)" :refresher-triggered="triggered"
refresher-enabled refresher-background="#F6F6F6" style="height: 100%;width: 100%;">
<view class="page-box1">
<view v-if="frequentlyList.length>0" class="list">
<view class="shop-item" v-for="(item, index) in frequentlyList" :key="index"
@click="openGoodPopup(item)">
2024-06-12 10:04:00 +08:00
<image class="shop-img" :src="item.image"></image>
2024-06-05 18:45:30 +08:00
<view class="shop-content" style="width: 490rpx;">
<view class="title">
<view class="name u-line-2">{{item.name}}</view>
<!-- <view class="tip u-line-1">{{item.spec}}</view> -->
<view class="tip u-line-1">{{item.unit_name}}</view>
</view>
<view class="price-btn">
2024-06-12 10:04:00 +08:00
<view class="price">{{item.price}}</view>
2024-06-05 18:45:30 +08:00
<view class="btn">
<up-button size="small" plain color="#20b128"
shape="circle">加入购物车</up-button>
</view>
</view>
</view>
</view>
</view>
<view v-else style="margin-top: 100rpx;">
<up-empty text="没有常买的商品"
icon="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/29955202404260944367594.png">
</up-empty>
</view>
<view style="width: 100%;height: 200rpx;"></view>
</view>
</scroll-view>
</swiper-item>
</swiper>
</view>
<up-transition :show="tabsActive==0">
<view class="fiexd-btn-box cart-btn">
<view class="cart-check" @click="changeAll(checkAll===cartInfo.count)">
<image v-if="checkAll!=cartInfo.count" src="@/static/icon/n-check.png"></image>
<image v-else src="@/static/icon/check.png"></image>
<text style="font-size: 24rpx;">全选</text>
<text style="font-size: 24rpx;" v-if="checkAll">({{checkAll}})</text>
</view>
<view v-if="!isAdmin" class="btn-boxs">
<view class="all-price">
<view style="width: 80rpx;">合计:</view>
<view class="price">
<text style="font-size: 24rpx;">¥</text>
<text style="font-size: 34rpx;">{{c_price0}}</text>
<text style="font-size: 24rpx;">.{{c_price1}}</text>
</view>
</view>
<view style="width: 200rpx;">
2024-06-25 13:44:10 +08:00
<up-button color="#20b128" shape="circle" @click="settleAccounts">
<view class="">
2024-06-20 14:58:05 +08:00
去结算<text v-if="checkAll">({{checkAll}})</text>
</view>
2024-06-25 13:44:10 +08:00
2024-06-20 14:58:05 +08:00
</up-button>
2024-06-05 18:45:30 +08:00
</view>
</view>
<view v-else class="btn-boxs">
<!-- <view style="width: 100px;margin-right: 20rpx;"><up-button size="small" plain color="#989898" shape="circle">移入收藏夹</up-button></view> -->
<view style="width: 100px;"><up-button @click="deleteCartList()" size="small" plain color="#989898"
shape="circle">删除</up-button></view>
</view>
</view>
</up-transition>
<goodPopup ref="goodRef" :show="showGoodPopup" @close="showGoodPopup=false" @change="changeGood" />
2024-04-25 18:02:30 +08:00
</template>
<script setup>
2024-06-05 18:45:30 +08:00
import {
2024-06-26 14:06:40 +08:00
onShow,
onLoad
2024-06-05 18:45:30 +08:00
} from "@dcloudio/uni-app"
import {
computed,
ref,
watch
} from 'vue';
import {
cartListApi,
cartChangeApi,
cartCreateApi,
cartDeleteApi,
frequentlyPurchaseApi
} from "@/api/cart.js"
import goodPopup from "@/components/goodPopup.vue"
import useCartStore from "@/store/cart.js"
2024-06-11 15:12:49 +08:00
// 用户选择的门店信息
let STORE_INFO = uni.getStorageSync('STORE_INFO');
if (STORE_INFO)
STORE_INFO = JSON.parse(STORE_INFO)
2024-06-05 18:45:30 +08:00
const cartStore = useCartStore();
// 创建响应式数据
const list = ref(['购物车', '常买']);
const tabsActive = ref(0)
// 定义方法
const changeTab = (e) => {
tabsActive.value = e;
swiperCurrent.value = e;
}
const swiperCurrent = ref(0);
const animationfinish = ({
detail: {
current
}
}) => {
swiperCurrent.value = current;
tabsActive.value = current;
if (swiperCurrent.value == 1) getFrequentlyPurchase();
}
// 结算
const settleAccounts = () => {
let list = [];
cartList.value.forEach(item => {
if (item.check) list.push(item.id);
})
cartStore.setCartList(list);
uni.navigateTo({
url: '/pagesOrder/settle/settle'
})
}
// 删除
const deleteCartList = () => {
let list = [];
if (!cartList.value.length) return uni.showToast({
title: '请选择商品',
icon: 'none'
});
cartList.value.forEach(item => {
2024-06-08 15:27:58 +08:00
if (item.check) list.push(item.id);
2024-06-05 18:45:30 +08:00
})
cartDeleteApi({
2024-06-08 16:05:05 +08:00
id: list
2024-06-05 18:45:30 +08:00
}).then(() => {
getcartList(false);
})
}
// 购物车相关
const addCart = (id, cart_num) => { //加入购物车
2024-06-25 13:44:10 +08:00
if (uni.getStorageSync('STORE_INFO').length) {
let data = JSON.parse(uni.getStorageSync('STORE_INFO'))
let STORE_INFO = {};
for (let key in data) {
STORE_INFO[key] = data[key]
}
cartCreateApi({
cart_num: +cart_num,
product_id: id,
store_id: STORE_INFO.id
// store_id:
}).then(res => {
getcartList();
})
}
2024-06-05 18:45:30 +08:00
}
const isAdmin = ref(false); //是否管理
// 选择商品相关
const showGoodPopup = ref(false);
const goodRef = ref(null);
const goodData = ref({});
const openGoodPopup = (item) => { // 打开数量/重量弹窗
goodData.value = JSON.parse(JSON.stringify(item));
goodRef.value.setData(goodData.value);
showGoodPopup.value = true;
};
const changeGood = (data) => { // 确定选择商品重量
showGoodPopup.value = false;
2024-06-12 10:04:00 +08:00
addCart(data.product_id || data.id, data.cart_num);
2024-06-05 18:45:30 +08:00
}
const cartList = ref([]);
const cartInfo = ref({});
const getcartList = (check = true) => {
cartListApi().then(res => {
if (check) {
res.data.lists = res.data.lists.map(item => {
item.check = true;
return item;
})
checkAll.value = res.data?.count || 0
} else {
checkAll.value = 0
}
2024-06-25 13:44:10 +08:00
res.data.lists = res.data.lists.map(item => {
if (!item.price) item.price = item.sell; // 兼容旧数据新数据已经添加了price字段
return item;
})
2024-06-05 18:45:30 +08:00
cartList.value = res.data.lists;
cartInfo.value = {
total_price: res.data?.extend.total_price || '0.00',
2024-06-26 14:06:40 +08:00
pay_price: res.data?.extend.pay_price || '0.00',
2024-06-05 18:45:30 +08:00
count: res.data?.count || 0
}
2024-06-26 15:53:52 +08:00
console.log(cartInfo.value)
2024-06-05 18:45:30 +08:00
setTimeout(() => {
triggered.value = false;
}, 300)
})
}
const c_price0 = computed(() => {
2024-06-26 14:06:40 +08:00
let price = cartInfo.value.pay_price + '';
2024-06-05 18:45:30 +08:00
return price.split('.')[0] || '0';
})
const c_price1 = computed(() => {
2024-06-26 15:53:52 +08:00
let price = cartInfo.value.pay_price + '';
2024-06-05 18:45:30 +08:00
return price.split('.')[1] || '00';
})
// 选中
let checkAll = ref(0)
const checkItem = (item, type) => {
item.check = type;
2024-06-26 16:27:35 +08:00
let price = Number(cartInfo.value.pay_price);
2024-06-05 18:45:30 +08:00
if (type) {
2024-06-26 16:27:35 +08:00
price += (priceKey.value.off_activity == 1 ? item[priceKey.value.price] : item[priceKey.value.op_price]) *
item
.cart_num;
2024-06-05 18:45:30 +08:00
checkAll.value++;
} else {
2024-06-26 16:27:35 +08:00
price -= (priceKey.value.off_activity == 1 ? item[priceKey.value.price] : item[priceKey.value.op_price]) *
item.cart_num;
2024-06-05 18:45:30 +08:00
checkAll.value--;
}
2024-06-26 16:27:35 +08:00
cartInfo.value.pay_price = price.toFixed(2);
2024-06-05 18:45:30 +08:00
}
const changeAll = (flag = false) => {
let count = 0;
cartList.value.forEach(item => {
item.check = !flag;
if (!flag) count += item.sell * item.cart_num;
})
if (!flag) {
checkAll.value = cartInfo.value.count;
} else {
checkAll.value = 0;
}
cartInfo.value.total_price = count.toFixed(2);
}
// 常买记录
const frequentlyList = ref([]);
const where = ref({
page_no: 1,
page_size: 25
})
const getFrequentlyPurchase = () => {
frequentlyPurchaseApi({
...where.value
}).then(res => {
frequentlyList.value = res.data;
setTimeout(() => {
triggered.value = false;
}, 300)
})
}
const triggered = ref(false);
const refresherrefresh = (type = 1) => {
triggered.value = true;
if (type == 1) getcartList();
if (type == 2) getFrequentlyPurchase();
}
onShow(() => {
getcartList();
})
2024-06-26 14:06:40 +08:00
let priceKey = ref({})
2024-06-26 14:21:00 +08:00
onLoad(() => {
if (uni.getStorageSync('PRICE_KEY')) {
priceKey.value = JSON.parse(uni.getStorageSync('PRICE_KEY'));
} else {
priceKey.value = {};
2024-06-26 14:06:40 +08:00
}
2024-06-26 14:21:00 +08:00
2024-06-26 14:06:40 +08:00
})
2024-04-25 18:02:30 +08:00
</script>
<style lang="scss">
2024-06-05 18:45:30 +08:00
.tabs {
color: #444444;
font-size: 32rpx;
margin-right: 30rpx;
}
.tabs-active {
color: #20B128;
// font-size: 34rpx;
transition: 300ms;
}
.wrap {
display: flex;
flex-direction: column;
height: calc(100vh - var(--window-top));
width: 100%;
background-color: #f6f6f6;
}
.swiper-box {
flex: 1;
}
.swiper-item {
height: 100%;
}
.page-box1 {
position: relative;
.total {
2024-06-14 10:54:40 +08:00
// margin-top: 10rpx;
2024-06-11 15:12:49 +08:00
padding: 0 40rpx;
2024-06-05 18:45:30 +08:00
display: flex;
justify-content: space-between;
2024-06-14 10:54:40 +08:00
// position: sticky;
// top: var(--window-top);
// left: 0;
// right: 0;
z-index: 100000 !important;
2024-06-05 18:45:30 +08:00
height: 60rpx;
line-height: 60rpx;
font-size: 26rpx;
color: #444;
}
.list {
margin: 20rpx;
border-radius: 20rpx;
overflow: hidden;
.shop-item {
padding: 20rpx;
border-bottom: 1rpx solid #f6f6f6;
background-color: #fff;
display: flex;
.shop-check {
width: 60rpx;
height: 120rpx;
display: flex;
align-items: center;
image {
width: 40rpx;
height: 40rpx;
}
}
.shop-img {
2024-06-26 14:06:40 +08:00
height: 164rpx;
width: 164rpx;
2024-06-05 18:45:30 +08:00
margin-right: 20rpx;
border-radius: 14rpx;
}
.shop-content {
width: 430rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
.title {
.name {
font-size: 28rpx;
}
.tip {
color: #999;
font-size: 24rpx;
margin: 12rpx 0;
}
}
.price {
font-size: 30rpx;
font-weight: bold;
color: #F55726;
}
.price-btn {
display: flex;
justify-content: space-between;
.btn {
display: flex;
align-items: center;
.num {
margin: 0 20rpx;
text-align: center;
}
}
}
}
}
2024-06-15 17:29:39 +08:00
2024-06-05 18:45:30 +08:00
}
}
.cart-btn {
.cart-check {
display: flex;
align-items: center;
image {
width: 40rpx;
height: 40rpx;
margin: 0 10rpx;
}
}
.btn-boxs {
display: flex;
.all-price {
display: flex;
align-items: center;
margin-right: 20rpx;
.price {
color: #20B128;
display: table-cell;
vertical-align: bottom;
font-weight: bold;
}
}
}
}
2024-04-25 18:02:30 +08:00
</style>