purchase-let/pages/index/index.vue

746 lines
20 KiB
Vue
Raw Normal View History

2024-04-25 18:02:30 +08:00
<template>
<view class="content">
<up-navbar placeholder style="z-index: 10080;">
<template #left>
2024-04-29 18:14:58 +08:00
<view style="font-size: 30rpx;font-weight: bold;">惠农批发</view>
2024-04-25 18:02:30 +08:00
</template>
</up-navbar>
2024-04-29 10:44:50 +08:00
<view class="navbar">
<view style="width: 500rpx;">
<up-search placeholder="请输入商品" @search="searchKeyword" v-model="keyword" :showAction="false"></up-search>
</view>
<view class="nav-item" @click="navTo('/pages/cart/cart')">
<image src="@/static/tab/ba.png"></image>
<text>购物车</text>
</view>
<view class="nav-item" @click="navTo('/pages/my/my')">
<image src="@/static/tab/ca.png"></image>
<text>个人中心</text>
</view>
</view>
2024-04-25 18:02:30 +08:00
<view style="position: relative;overflow: hidden;">
<scroll-view class="head-view" scroll-x>
<view class="list">
2024-04-29 18:14:58 +08:00
<view class="item" :class="{'item-active': topActive===item.id}" v-for="(item, index) in goodClassList"
2024-04-28 18:08:57 +08:00
:key="index" @click="changeOne(item, index)">
<view class="c-img"><up-image height="100rpx" width="100rpx" :src="item.pic"></up-image></view>
<view class="c-text u-line-1">{{item.name}}</view>
2024-04-25 18:02:30 +08:00
</view>
<view class="item" style="width: 80rpx;height: 20rpx;">
</view>
</view>
</scroll-view>
<view class="r-btn" @click="show=1">
<view></view>
<view></view>
<up-icon name="list"></up-icon>
</view>
</view>
<viewPopup nav v-if="show===1" @close="show=0">
<view class="cateOne">
<view class="head-title">全部分类</view>
2024-04-27 18:02:43 +08:00
<scroll-view scroll-y style="height: 600rpx;">
2024-04-25 18:02:30 +08:00
<view class="list">
2024-04-29 18:14:58 +08:00
<view class="item" :class="{'item-active': topActive===item.id}" v-for="(item, index) in goodClassList"
2024-04-28 18:08:57 +08:00
:key="index" @click="changeOne(item, index)">
<view class="c-img"><up-image height="100rpx" width="100rpx" :src="item.pic"></up-image></view>
<view class="c-text u-line-1">{{item.name}}</view>
2024-04-25 18:02:30 +08:00
</view>
</view>
</scroll-view>
</view>
</viewPopup>
<view class="scroll-box">
<scroll-view class="left" scroll-y>
2024-04-29 18:14:58 +08:00
<view class="item u-line-1" :class="{'item-active': leftActive===item.id}" v-for="(item, index) in goodClassTow"
2024-04-28 18:08:57 +08:00
:key="index" @click="changeTwo(item, index)">{{item.name}}</view>
2024-04-29 10:44:50 +08:00
<view style="width: 100%;height: 300rpx;"></view>
2024-04-25 18:02:30 +08:00
</scroll-view>
<view class="right">
<view class="classify">
<scroll-view style="height: 90rpx;" scroll-x>
<view class="classify-list">
2024-04-29 18:14:58 +08:00
<view class="classify-list-item u-line-1" :class="{'item-active': rightActive===item.id}"
2024-04-28 18:08:57 +08:00
v-for="(item, index) in goodClassThree" :key="index" @click="changeThree(item, index)">
{{item.name}}
2024-04-25 18:02:30 +08:00
</view>
<view style="width: 70rpx;flex-shrink: 0;"></view>
</view>
</scroll-view>
<view class="done" @click="show=2">
<up-icon name="arrow-down"></up-icon>
</view>
<view class="order-by">
2024-04-27 18:02:43 +08:00
<view class="item" :class="{'order-active': where.order==''}" @click="changeOrder('')">综合</view>
2024-04-28 18:08:57 +08:00
<view class="item" :class="{'order-active': where.order=='desc'||where.order=='asc'}"
2024-04-27 18:02:43 +08:00
@click="changeOrder(where.order=='asc'?'desc':'asc')">价格</view>
<view class="item" :class="{'order-active': where.order=='sales'}" @click="changeOrder('sales')">销量</view>
2024-04-25 18:02:30 +08:00
</view>
</view>
<viewPopup v-if="show===2" @close="show=0">
<view class="cateOne">
<scroll-view scroll-y style="height: 230rpx;">
<view class="classify-list">
2024-04-29 18:14:58 +08:00
<view class="classify-list-item u-line-1" :class="{'item-active': rightActive===item.id}"
2024-04-28 18:08:57 +08:00
v-for="(item, index) in goodClassThree" :key="index" @click="changeThree(item, index)">
{{item.name}}
2024-04-25 18:02:30 +08:00
</view>
</view>
</scroll-view>
</view>
</viewPopup>
2024-04-28 18:08:57 +08:00
<scroll-view class="list" scroll-y @scrolltolower="loadMoreGood">
2024-04-29 18:14:58 +08:00
<view class="shop-item" v-for="(item, index) in goodList" :key="item.id" @click="openGoodPopup(item)">
2024-04-28 18:08:57 +08:00
<view class="shop-img">
<up-image width="160rpx" height="160rpx" :src="item.imgs"></up-image>
</view>
2024-04-25 18:02:30 +08:00
<view class="shop-content">
<view class="title">
2024-04-28 18:08:57 +08:00
<view class="name u-line-2">{{item.name}}</view>
<view class="tip u-line-1">
<text>{{item.brand_name}}|</text>
<text>{{item.class_name}}|</text>
<text>{{item.unit_name}}</text>
</view>
2024-04-25 18:02:30 +08:00
</view>
<view class="price-btn">
2024-04-28 18:08:57 +08:00
<view class="price">{{item.sell}}</view>
2024-04-25 18:02:30 +08:00
<view class="btn">
2024-04-29 18:14:58 +08:00
<u--icon name="plus-circle-fill" size="20" color="#20b128"></u--icon>
2024-04-25 18:02:30 +08:00
</view>
</view>
</view>
</view>
2024-04-29 10:44:50 +08:00
<view style="width: 100%;height: 300rpx;"></view>
2024-04-25 18:02:30 +08:00
</scroll-view>
</view>
</view>
2024-04-29 10:44:50 +08:00
<view class="fiexd-btn-box">
<view class="price-info">
<view class="row">
<view>合计</view>
2024-04-29 18:14:58 +08:00
<view class="price">¥<text style="font-size: 36rpx;">{{cartInfo.total_price}}</text></view>
2024-04-29 10:44:50 +08:00
</view>
2024-04-29 18:14:58 +08:00
<!-- <view class="row">
<view style="color: #777;">原价 ¥50.00</view>
2024-04-29 10:44:50 +08:00
<view class="price">优惠 ¥0.00</view>
2024-04-29 18:14:58 +08:00
</view> -->
2024-04-29 10:44:50 +08:00
</view>
<view class="btn">
2024-04-29 18:14:58 +08:00
<up-button color="#20b128" :disabled="cartInfo.count==0" @click="settleAccounts">结算</up-button>
2024-04-29 10:44:50 +08:00
</view>
2024-04-29 18:14:58 +08:00
<view class="cart" @click="navTo('/pages/cart/cart')">
2024-04-29 10:44:50 +08:00
<image src="@/static/icon/cart.png"></image>
2024-04-29 18:14:58 +08:00
<view class="badge">{{cartInfo.count}}</view>
2024-04-29 10:44:50 +08:00
</view>
</view>
2024-04-29 18:14:58 +08:00
<goodPopup ref="goodRef" :show="showGoodPopup" @close="showGoodPopup=false" @change="changeGood" />
2024-04-25 18:02:30 +08:00
</view>
</template>
<script setup>
2024-04-29 18:14:58 +08:00
import { onLoad, onShow } from "@dcloudio/uni-app"
2024-04-28 18:08:57 +08:00
import { reactive, ref } from "vue"
import { goodListApi, goodClassListApi } from "@/api/good.js"
2024-04-29 18:14:58 +08:00
import { cartCreateApi, cartChangeApi, cartListApi } from "@/api/cart.js"
2024-04-27 18:02:43 +08:00
import viewPopup from "@/components/viewPopup.vue"
2024-04-29 18:14:58 +08:00
import goodPopup from "@/components/goodPopup.vue"
import useCartStore from "@/store/cart.js"
2024-04-27 18:02:43 +08:00
2024-04-29 18:14:58 +08:00
const cartStore = useCartStore();
2024-04-27 18:02:43 +08:00
const show = ref(0);
2024-04-25 18:02:30 +08:00
const topActive = ref(0);
2024-04-28 18:08:57 +08:00
const changeOne = (item, index) => {
2024-04-27 18:02:43 +08:00
console.log('选择', item, index);
2024-04-28 18:08:57 +08:00
topActive.value = item.id;
2024-04-27 18:02:43 +08:00
show.value = 0;
2024-04-29 10:44:50 +08:00
goodClassTow.value = item?.children || [];
goodClassThree.value = goodClassTow.value[0]?.children || [];
2024-04-29 18:14:58 +08:00
leftActive.value = goodClassTow.value[0]?.id || '';
rightActive.value = goodClassThree.value[0]?.id || '';
getGoodList();
2024-04-27 18:02:43 +08:00
}
2024-04-28 18:08:57 +08:00
const leftActive = ref(0);
const changeTwo = (item, index) => {
2024-04-27 18:02:43 +08:00
console.log('选择', item, index);
2024-04-28 18:08:57 +08:00
leftActive.value = item.id;
2024-04-27 18:02:43 +08:00
show.value = 0;
2024-04-29 10:44:50 +08:00
goodClassThree.value = item?.children || [];
2024-04-29 18:14:58 +08:00
rightActive.value = goodClassThree.value[0]?.id || '';
getGoodList();
2024-04-27 18:02:43 +08:00
}
2024-04-28 18:08:57 +08:00
const rightActive = ref(0);
const changeThree = (item, index) => {
2024-04-27 18:02:43 +08:00
console.log('选择', item, index);
2024-04-28 18:08:57 +08:00
rightActive.value = item.id;
2024-04-27 18:02:43 +08:00
show.value = 0;
2024-04-29 18:14:58 +08:00
getGoodList();
2024-04-27 18:02:43 +08:00
}
2024-04-28 18:08:57 +08:00
const cartList = reactive(new Map());
2024-04-29 18:14:58 +08:00
const addCart = (id, cart_num) => { //加入购物车
2024-04-28 18:08:57 +08:00
cartCreateApi({
2024-04-29 18:14:58 +08:00
cart_num: cart_num,
2024-04-28 18:08:57 +08:00
is_new: 0, // 是否直接购买0否1是
goods_id: id
}).then(res => {
console.log(res);
let now = cartList.get(id) || 0;
cartList.set(id, now + 1);
2024-04-29 18:14:58 +08:00
getCartList();
2024-04-28 18:08:57 +08:00
})
}
const removeCart = (id) => {
let num = cartList.get(id) || 0;
if (num == 0) return;
cartChangeApi({
goods_id: id,
cart_num: num--
}).then(res => {
console.log(res);
cartList.set(id, num)
})
}
2024-04-27 18:02:43 +08:00
const keyword = ref('');
2024-04-28 18:08:57 +08:00
const searchKeyword = () => {
2024-04-29 18:14:58 +08:00
where.value.name = keyword.value;
getGoodList();
2024-04-27 18:02:43 +08:00
}
2024-04-28 18:08:57 +08:00
const changeOrder = (order) => {
2024-04-27 18:02:43 +08:00
console.log('排序', order);
where.value.order = order;
}
2024-04-28 18:08:57 +08:00
2024-04-27 18:02:43 +08:00
const where = ref({
2024-04-29 18:14:58 +08:00
page_no: 1,
page_size: 25,
name: '',
2024-04-27 18:02:43 +08:00
order: ''
})
const loading = ref(true);
2024-04-28 18:08:57 +08:00
const goodList = ref([]);
2024-04-29 18:14:58 +08:00
const getGoodList = (loadmore = false) => {
2024-04-27 18:02:43 +08:00
loading.value = true;
2024-04-29 18:14:58 +08:00
let class_id = rightActive.value || leftActive.value || topActive.value || '';
let class_all = '';
// 若分类没有选中时,则直接查询上一级分类
if(rightActive.value=='') class_all = leftActive.value;
if(leftActive.value=='') class_all = topActive.value;
if(topActive.value=='') class_all = '';
if(class_all) class_id = ""; //只能带其中一个
goodListApi({
...where.value,
class_all: class_all,
class: class_id
}).then(res => {
if (loadmore) goodList.value.push(...res.data.lists);
else goodList.value = res.data.lists;
2024-04-28 18:08:57 +08:00
})
2024-04-27 18:02:43 +08:00
}
2024-04-28 18:08:57 +08:00
// 商品列表触底
const loadMoreGood = () => {
console.log('触底了');
}
const goodClassList = ref([]); // 一级分类
const goodClassTow = ref([]); // 二级分类
const goodClassThree = ref([]); // 三级分类
const getgoodClassList = () => {
goodClassListApi().then(res => {
2024-04-29 18:14:58 +08:00
res.data?.lists?.unshift({
id: "",
name: "全部",
pic: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/35adb202404271727457954.png",
children: []
})
res.data.lists = res.data.lists.map(item=>{
if(!item.children) item.children = [];
item.children?.unshift({
id: "",
pid: item.id,
name: "全部",
})
item.children = item.children.map(t=>{
if(!t.children) t.children = [];
t.children?.unshift({
id: "",
pid: t.id,
name: "全部",
})
return t;
})
return item;
})
2024-04-29 10:44:50 +08:00
goodClassList.value = res.data?.lists || [];
goodClassTow.value = goodClassList.value[0]?.children || [];
goodClassThree.value = goodClassTow.value[0]?.children || [];
2024-04-28 18:08:57 +08:00
topActive.value = goodClassList.value[0]?.id;
leftActive.value = goodClassTow.value[0]?.id;
rightActive.value = goodClassThree.value[0]?.id;
})
}
2024-04-29 18:14:58 +08:00
const navTo = (url) => {
2024-04-29 10:44:50 +08:00
uni.navigateTo({
url: url
})
}
2024-04-28 18:08:57 +08:00
2024-04-29 18:14:58 +08:00
// 选择商品相关
const showGoodPopup = ref(false);
const goodRef = ref(null);
const goodData = ref({});
const openGoodPopup = (item) => { // 打开数量/重量弹窗
goodData.value = item;
goodRef.value.setData(item);
showGoodPopup.value = true;
};
const changeGood = (data)=>{ // 确定选择商品重量
showGoodPopup.value = false;
addCart(data.id, data.cart_num);
}
// 结算
const settleAccounts = ()=>{
uni.navigateTo({
url: '/pagesOrder/settle/settle'
})
}
// 购物车相关
const cartInfo = ref({
total_price: '0.00',
count: 0
})
const getCartList = (res)=>{
cartListApi({
page_no: 1,
page_size: 100,
}).then(res=>{
cartInfo.value = {
total_price: res.data?.extend?.total_price || '0.00',
count: res.data?.count || 0
}
cartStore.setCartList(res.data?.lists.map(item=>item.cart_id))
})
}
2024-04-28 18:08:57 +08:00
onLoad(() => {
getgoodClassList();
2024-04-27 18:02:43 +08:00
getGoodList();
})
2024-04-29 18:14:58 +08:00
onShow(()=>{
getCartList();
})
2024-04-25 18:02:30 +08:00
</script>
<style lang="scss">
.content {
background-color: #fff;
}
2024-04-29 10:44:50 +08:00
.navbar {
padding: 0 20rpx;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 22rpx;
color: #777;
.nav-item {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
image {
height: 40rpx;
width: 40rpx;
}
}
}
2024-04-25 18:02:30 +08:00
.head-view {
background-color: #fff;
height: 180rpx;
width: 750rpx;
.list {
height: 100%;
display: flex;
align-items: center;
padding: 0 20rpx;
font-size: 22rpx;
.item {
width: 120rpx;
flex-shrink: 0;
margin-right: 20rpx;
display: flex;
flex-direction: column;
align-items: center;
.c-img {
height: 100rpx;
width: 100rpx;
flex-shrink: 0;
border-radius: 50%;
border: 2px solid transparent;
2024-04-28 18:08:57 +08:00
overflow: hidden;
2024-04-25 18:02:30 +08:00
}
.c-text {
width: 100%;
text-align: center;
box-sizing: border-box;
padding: 2rpx 4rpx 3rpx 4rpx;
border-radius: 50rpx;
margin-top: 5rpx;
}
}
.item-active {
.c-img {
border: 2px solid #20b128;
}
.c-text {
background-color: #20b128;
color: #fff;
}
}
}
}
.r-btn {
position: absolute;
right: 0;
top: 0;
height: 100%;
width: 60rpx;
font-size: 24rpx;
background-color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
box-shadow: -10rpx 0 60rpx 1rpx rgba(0, 0, 0, 0.3);
}
.cateOne {
background-color: #fff;
.head-title {
color: #666666;
font-size: 30rpx;
height: 80rpx;
line-height: 80rpx;
padding-left: 30rpx;
}
.list {
2024-04-28 18:08:57 +08:00
padding: 20rpx 20rpx;
2024-04-25 18:02:30 +08:00
font-size: 22rpx;
display: grid;
2024-04-28 18:08:57 +08:00
grid-template-columns: repeat(5, 1fr);
2024-04-25 18:02:30 +08:00
justify-content: center;
grid-gap: 20rpx;
.item {
width: 120rpx;
2024-04-28 18:08:57 +08:00
margin: 0 auto;
2024-04-25 18:02:30 +08:00
display: flex;
flex-direction: column;
align-items: center;
.c-img {
height: 100rpx;
width: 100rpx;
flex-shrink: 0;
border-radius: 50%;
border: 2px solid transparent;
2024-04-28 18:08:57 +08:00
overflow: hidden;
2024-04-25 18:02:30 +08:00
}
.c-text {
width: 100%;
text-align: center;
box-sizing: border-box;
padding: 2rpx 4rpx 3rpx 4rpx;
border-radius: 50rpx;
margin-top: 5rpx;
}
}
.item-active {
.c-img {
border: 2px solid #20b128;
}
.c-text {
background-color: #20b128;
color: #fff;
}
}
}
.classify-list {
2024-04-28 18:08:57 +08:00
padding: 20rpx 30rpx;
2024-04-25 18:02:30 +08:00
font-size: 22rpx;
display: grid;
2024-04-28 18:08:57 +08:00
grid-template-columns: repeat(3, 1fr);
2024-04-25 18:02:30 +08:00
justify-content: center;
grid-gap: 20rpx;
.classify-list-item {
flex-shrink: 0;
text-align: center;
width: 150rpx;
height: 42rpx;
line-height: 42rpx;
background: #F6F6F6;
border-radius: 22rpx 22rpx 22rpx 22rpx;
border: 1rpx solid transparent;
}
.item-active {
border: 1rpx solid #20b128;
color: #20b128;
background-color: rgba(#20b128, 0.1);
}
}
}
.scroll-box {
// background-color: red;
display: flex;
.left {
2024-04-29 10:44:50 +08:00
height: calc(100vh - var(--window-top) - var(--window-bottom) - 500rpx);
2024-04-25 18:02:30 +08:00
/* #ifdef H5 */
2024-04-29 10:44:50 +08:00
height: calc(100vh - 44px - 300rpx);
2024-04-25 18:02:30 +08:00
/* #endif */
width: 170rpx;
box-sizing: border-box;
2024-04-28 18:08:57 +08:00
background-color: #f6f6f6;
2024-04-25 18:02:30 +08:00
font-size: 24rpx;
.item {
height: 96rpx;
line-height: 96rpx;
text-align: center;
}
.item-active {
background-color: #fff;
position: relative;
color: #20b128;
&::before {
content: '';
background-color: #20b128;
width: 6rpx;
height: 48rpx;
border-radius: 10rpx;
position: absolute;
left: 0;
top: 50%;
transform: translate(0, -50%);
}
}
}
.right {
2024-04-29 10:44:50 +08:00
height: calc(100vh - var(--window-top) - 500rpx);
2024-04-25 18:02:30 +08:00
/* #ifdef H5 */
height: calc(100vh - 96px - 180rpx);
/* #endif */
width: 580rpx;
box-sizing: border-box;
position: relative;
overflow: hidden;
.classify {
height: 150rpx;
background-color: #fff;
2024-04-28 18:08:57 +08:00
border-bottom: 1rpx solid #f6f6f6;
2024-04-25 18:02:30 +08:00
position: relative;
font-size: 24rpx;
.classify-list {
display: flex;
padding: 20rpx;
.classify-list-item {
flex-shrink: 0;
text-align: center;
width: 108rpx;
height: 42rpx;
line-height: 42rpx;
background: #F6F6F6;
border-radius: 22rpx 22rpx 22rpx 22rpx;
margin-right: 20rpx;
border: 1rpx solid transparent;
}
.item-active {
border: 1rpx solid #20b128;
color: #20b128;
background-color: rgba(#20b128, 0.1);
}
}
.done {
height: 90rpx;
width: 60rpx;
position: absolute;
top: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
}
.order-by {
display: flex;
justify-content: flex-end;
align-items: center;
.item {
padding-right: 20rpx;
}
2024-04-28 18:08:57 +08:00
.order-active {
2024-04-27 18:02:43 +08:00
color: #20b128;
}
2024-04-25 18:02:30 +08:00
}
}
.list {
2024-04-29 10:44:50 +08:00
height: calc(100vh - var(--window-top) - 600rpx);
2024-04-25 18:02:30 +08:00
/* #ifdef H5 */
2024-04-29 10:44:50 +08:00
height: calc(100vh - 44px - 400rpx);
2024-04-28 18:08:57 +08:00
2024-04-25 18:02:30 +08:00
/* #endif */
.shop-item {
padding: 20rpx;
2024-04-28 18:08:57 +08:00
border-bottom: 1rpx solid #f6f6f6;
2024-04-25 18:02:30 +08:00
background-color: #fff;
display: flex;
.shop-img {
height: 160rpx;
width: 160rpx;
margin-right: 20rpx;
border-radius: 14rpx;
2024-04-28 18:08:57 +08:00
overflow: hidden;
2024-04-25 18:02:30 +08:00
}
.shop-content {
width: 380rpx;
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 {
width: 60rpx;
text-align: center;
}
}
}
}
}
}
}
}
2024-04-29 10:44:50 +08:00
.fiexd-btn-box {
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
height: 140rpx;
height: calc(constant(safe-area-inset-bottom) + 140rpx);
/* 适用于iOS设备 */
height: calc(env(safe-area-inset-bottom) + 140rpx);
/* 适用于Android设备 */
padding-top: 40rpx;
.price-info {
.row {
display: flex;
font-size: 26rpx;
align-items: center;
&>view {
margin-right: 15rpx;
}
.price {
color: #F55726;
}
}
}
.btn {
width: 200rpx;
}
.cart {
position: absolute;
top: -40rpx;
left: 40rpx;
image {
width: 80rpx;
height: 80rpx;
}
2024-04-29 18:14:58 +08:00
.badge {
position: absolute;
top: 0;
right: 0;
background-color: #F55726;
color: #fff;
border-radius: 50%;
width: 26rpx;
height: 26rpx;
text-align: center;
line-height: 26rpx;
font-size: 18rpx;
}
2024-04-29 10:44:50 +08:00
}
}
2024-04-25 18:02:30 +08:00
</style>