2025-06-11 14:38:40 +08:00

172 lines
4.5 KiB
Vue

<template>
<div>
<common current-name="agricultural">
<template #main>
<banner :imglist="bannerList"></banner>
<filtertop :list="treeList" @select="selected" @search="search"></filtertop>
<div class="goods-list-warp">
<div class="goods-list">
<template v-for="(n, index) in list" :key="n.id">
<div class="goods-item">
<goodsItem :data="n"></goodsItem>
</div>
</template>
</div>
<el-pagination
:current-page="pagination.current"
:page-sizes="[10, 20, 50, 100]"
:page-size="pagination.size"
layout="total, sizes, prev, pager, next, jumper"
:total="pagination.total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
>
</el-pagination>
</div>
</template>
</common>
</div>
</template>
<script setup name="ecommerce">
import common from './components/common.vue';
import banner from './components/banner.vue';
import filtertop from './components/filtertop.vue';
import goodsItem from './components/goodsItem.vue';
import { ref, reactive, onMounted, onBeforeMount, onUnmounted, watch, computed } from 'vue';
import { transaction, agriculturalList } from '@/apis/agricultural';
let treeList = reactive([
// {
// id: '01',
// name: '种子/种苗',
// children: [
// { parentId: '01', id: '0101', name: '小麦种子' },
// { parentId: '01', id: '0102', name: '水稻种子' },
// { parentId: '01', id: '0103', name: '玉米种子' },
// { parentId: '01', id: '0104', name: '番茄种子' },
// { parentId: '01', id: '0105', name: '白菜种子' },
// ],
// },
// {
// id: '02',
// name: '农药',
// children: [
// { parentId: '03', id: '0101', name: '杀虫剂' },
// { parentId: '03', id: '0102', name: '杀菌剂' },
// { parentId: '03', id: '0103', name: '除草剂' },
// { parentId: '03', id: '0104', name: '杀螨剂' },
// ],
// },
// {
// id: '03',
// name: '化肥',
// children: [
// { parentId: '02', id: '0101', name: '有机肥' },
// { parentId: '02', id: '0102', name: '水溶肥' },
// { parentId: '02', id: '0103', name: '天然肥料' },
// ],
// },
]);
onBeforeMount(() => {
// 在 DOM 挂载前调用
getTree();
getList();
});
//获取农资商品分类
const getTree = () => {
transaction().then((res) => {
if (res.code === 200) {
let a = res.data.sort((a, b) => Number(b.id) - Number(a.id));
treeList.splice(0, treeList.length, ...a);
// console.log('treeList', treeList);
}
});
};
//获取农资商品列表
let list = reactive([]);
let params = reactive({
current: 1,
size: 10,
parentId: null,
childrenId: null,
});
let pagination = reactive({
current: 1,
size: 50,
total: 0,
});
const handleSizeChange = (val) => {
pagination.size = val;
getList();
};
const handleCurrentChange = (val) => {
pagination.current = val;
getList();
};
const getList = (data) => {
if (data) {
params.goodName = data.searchName;
}
params.current = pagination.current;
params.size = pagination.size;
agriculturalList(params).then((res) => {
if (res.code === 200) {
list.splice(0, list.length, ...res.data.records);
pagination.total = res.data.total;
}
});
};
let bannerList = reactive(['images/ecommerce/' + 'banner.png', 'images/ecommerce/' + 'banner1.png']);
const search = (data) => {
getList(data);
};
const selected = (data) => {
// 获取所有值并转为数组
// const val = Object.values(data);
params.categoryId = data.id;
// if (val.length === 1) {
// params.parentId = val[0].id;
// } else {
// if (val[0].id === '') {
// params.childrenId = null;
// params.parentId = null;
// } else {
// params.childrenId = val[val.length - 1].id;
// params.parentId = val[val.length - 1].parentId;
// }
// }
getList();
};
</script>
<style lang="scss" scoped>
.goods-list-warp {
margin-top: 16px;
width: 100%;
.goods-list {
display: inline-flex;
justify-content: space-around;
width: 100%;
flex-wrap: wrap;
gap: 10px;
.goods-item {
display: inline-block;
overflow: hidden;
margin-bottom: 16px;
width: calc((100% - 50px) / 5);
border-radius: 16px;
background: $color-fff;
}
}
/* 添加伪元素占位 */
.goods-list::after {
content: '';
flex: auto;
}
}
</style>