Initial commit
This commit is contained in:
142
miniprogram/pages/category/index.js
Normal file
142
miniprogram/pages/category/index.js
Normal file
@@ -0,0 +1,142 @@
|
||||
import { getCategoryList } from '../../services/good/fetchCategoryList';
|
||||
import { fetchGoodsList } from '../../services/good/fetchGoodsList';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
list: [],
|
||||
goodsList: [],
|
||||
hasLoaded: false,
|
||||
loadMoreStatus: 0,
|
||||
categoryId: '',
|
||||
categoryName: '',
|
||||
},
|
||||
pageNum: 1,
|
||||
pageSize: 30,
|
||||
total: 0,
|
||||
async init() {
|
||||
try {
|
||||
const result = await getCategoryList();
|
||||
this.setData({
|
||||
list: result,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('err:', error);
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.getTabBar().init();
|
||||
},
|
||||
onChange(e) {
|
||||
const { item, source } = e.detail;
|
||||
let categoryId = item.groupId || item.categoryId || '';
|
||||
const categoryName = item.name || '';
|
||||
|
||||
// 兼容“全部”按钮的 groupId: `${id}_all`
|
||||
if (categoryId && categoryId.toString().endsWith('_all')) {
|
||||
categoryId = categoryId.toString().replace('_all', '');
|
||||
}
|
||||
|
||||
// 二级分类:跳转到商品列表页;一级分类:当前页加载数据
|
||||
if (source === 'child') {
|
||||
const url = `/pages/goods/list/index?categoryId=${encodeURIComponent(categoryId)}&categoryName=${encodeURIComponent(categoryName)}`;
|
||||
wx.navigateTo({ url });
|
||||
return;
|
||||
}
|
||||
|
||||
// 一级分类只在当前页加载商品
|
||||
this.pageNum = 1;
|
||||
this.setData({ categoryId, categoryName, loadMoreStatus: 0 });
|
||||
this.loadCategoryGoods(true);
|
||||
},
|
||||
|
||||
// 组装查询参数
|
||||
buildQueryParams(reset = true) {
|
||||
const params = {
|
||||
pageNum: reset ? 1 : this.pageNum + 1,
|
||||
pageSize: this.pageSize,
|
||||
category_id: this.data.categoryId || undefined,
|
||||
};
|
||||
return params;
|
||||
},
|
||||
|
||||
async loadCategoryGoods(reset = true) {
|
||||
const { loadMoreStatus, goodsList = [] } = this.data;
|
||||
if (loadMoreStatus !== 0) return;
|
||||
|
||||
const params = this.buildQueryParams(reset);
|
||||
this.setData({ loadMoreStatus: 1 });
|
||||
try {
|
||||
const result = await fetchGoodsList(params);
|
||||
|
||||
if (!result || !result.spuList) {
|
||||
this.setData({ hasLoaded: true, loadMoreStatus: 0 });
|
||||
return;
|
||||
}
|
||||
|
||||
const { spuList, totalCount = 0 } = result;
|
||||
const merged = reset ? spuList : goodsList.concat(spuList);
|
||||
const status = merged.length === totalCount ? 2 : 0;
|
||||
|
||||
this.pageNum = params.pageNum || 1;
|
||||
this.total = totalCount;
|
||||
|
||||
this.setData({
|
||||
goodsList: merged,
|
||||
loadMoreStatus: status,
|
||||
hasLoaded: true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取分类商品失败:', error);
|
||||
this.setData({ loadMoreStatus: 0, hasLoaded: true });
|
||||
}
|
||||
},
|
||||
|
||||
onReachBottom() {
|
||||
const { goodsList } = this.data;
|
||||
const total = this.total || 0;
|
||||
if (goodsList.length >= total && total > 0) {
|
||||
this.setData({ loadMoreStatus: 2 });
|
||||
return;
|
||||
}
|
||||
this.loadCategoryGoods(false);
|
||||
},
|
||||
|
||||
handleAddCart() {
|
||||
wx.showToast({ title: '点击加购', icon: 'none' });
|
||||
},
|
||||
|
||||
|
||||
onLoad() {
|
||||
this.init(true);
|
||||
},
|
||||
|
||||
// 分享功能
|
||||
onShareAppMessage() {
|
||||
return {
|
||||
title: '商品分类 - 浏览更多精选商品',
|
||||
path: '/pages/category/index'
|
||||
};
|
||||
},
|
||||
|
||||
// 分享到朋友圈
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '商品分类 - 浏览更多精选商品'
|
||||
};
|
||||
},
|
||||
|
||||
handleClickGoods(e) {
|
||||
const { index } = e.detail || {};
|
||||
const { goodsList = [] } = this.data;
|
||||
const item = goodsList[index];
|
||||
const spuId = item && (item.spuId || item.id || item.skuId);
|
||||
if (!spuId) {
|
||||
wx.showToast({ title: '无法识别商品ID', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
wx.navigateTo({
|
||||
url: `/pages/goods/details/index?spuId=${encodeURIComponent(spuId)}`
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user