Files
kangyuan-mobile/pages/index/category.vue
T

184 lines
5.1 KiB
Vue

<template>
<s-pages title="分类" :showLeftButton="false" color="#ffffff">
<card_search class="search" style="padding-bottom: 32rpx" @search="search"></card_search>
<view class="page_bottom flex-row">
<view class="classify shrink">
<view
class="c_name size26"
:class="{ activeName: i == dataObj.index }"
v-for="(v, i) in dataObj.categoryList"
:key="v.id"
@click="classifyChange(i)"
>
{{ v.name }}
</view>
</view>
<pages_commodity
class="commodity flex1"
ref="pageCommodity"
:dataList="dataObj.dataList"
@getData="getData"
:subclassification="subclassification"
:height="height"
></pages_commodity>
</view>
</s-pages>
</template>
<script setup>
import { ref, reactive } from 'vue';
import sheep from '@/sheep';
import SpuApi from '@/sheep/api/product/spu';
import CategoryApi from '@/sheep/api/product/category';
import { onLoad } from '@dcloudio/uni-app';
import { handleTree } from '@/sheep/helper/utils';
import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll.js';
import goodsItem2 from '@/components/goodsItem/goodsItem2.vue';
import pages_commodity from '@/components/pages_commodity/pages_commodity.vue';
let sysInfo = uni.getSystemInfoSync();
const windowHeight = sysInfo.windowHeight;
let height = ref('100%');
height.value =
windowHeight -
sysInfo.statusBarHeight -
uni.upx2px(88) -
uni.upx2px(80) -
uni.upx2px(96) -
uni.upx2px(16) +
'px';
const dataObj = reactive({
categoryList: [], // 商品分类树
index: 0, // 选中的一级菜单,在 categoryList 的下标
c_id: null, // 选中的一级菜单 id
dataList: [], // 商品列表
});
let pageCommodity = ref(null);
// 获取分类
const getcategoryList = async () => {
const { code, data } = await CategoryApi.getCategoryList();
if (code !== 0) {
return;
}
// dataObj.categoryList = data;
dataObj.categoryList = handleTree(data);
if (dataObj.categoryList.length) {
dataObj.c_id = dataObj.categoryList[0].id;
}
classifyChange(0);
};
const subclassification = ref([]);
const classifyChange = (i) => {
dataObj.index = i;
dataObj.c_id = dataObj.categoryList[i]?.id;
subclassification.value = dataObj.categoryList[i]?.children || [];
// upCallback({
// num: 1,
// size: 10,
// });
// pageCommodity.value.changeData()
if (pageCommodity.value) {
pageCommodity.value.clearAll();
}
};
const changeArr = (id, arr, names = []) => {
if (!arr || !arr.length) return '';
for (const item of arr) {
const pathNames = [...names, item.name];
if (item.id == id) {
return pathNames.join(' | ');
}
if (item.children && item.children.length > 0) {
const found = changeArr(id, item.children, pathNames);
if (found) return found;
}
}
return '';
};
onLoad(async () => {
let res = await getcategoryList();
pageCommodity.value.changeData();
// upCallback();
});
const getData = async (mescroll) => {
let res = await SpuApi.getSpuPage({
categoryId: dataObj.c_id,
pageNo: mescroll?.num || 1,
pageSize: mescroll?.size || 10,
keyword: searchText.value,
...mescroll.params,
});
if (res.code !== 0) {
pageCommodity.value.requestResult(false);
return;
}
const curPageData = res.data.list || []; // 当前页数据
if (curPageData.length) {
curPageData.forEach((item) => {
item.categoryText = changeArr(item.categoryId, dataObj.categoryList);
});
}
if (mescroll?.num == 1) dataObj.dataList = []; // 第一页需手动制空列表
dataObj.dataList = dataObj.dataList.concat(curPageData); //追加新数据
// mescroll.endSuccess(curPageData.length); // 请求成功, 结束加载
pageCommodity.value.requestResult(true, curPageData.length);
};
let searchText = ref(null);
const search = (text) => {
searchText.value = text;
pageCommodity.value.changeData();
};
</script>
<style lang="scss" scoped>
.page_bottom {
width: 100%;
height: calc(100vh - 88rpx - 72rpx - 32rpx - 100rpx - 8rpx);
.classify {
width: 160rpx;
overflow-y: auto;
background-color: #f2f4f7;
.c_name {
text-align: center;
line-height: 120rpx;
color: #333333;
}
.activeName {
background: rgba($color: #ffffff, $alpha: 1);
position: relative;
color: var(--color32);
font-weight: 600;
&::before {
content: '';
display: inline-block;
width: 6rpx;
height: 76rpx;
background-color: var(--color32);
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
border-radius: 0 4rpx 4rpx 0;
}
}
}
}
.commodity {
width: calc(100% - 160rpx);
}
.search {
padding: 0 30rpx;
box-sizing: border-box;
background-color: #fff;
}
</style>