import { fetchGoodsList } from '../../../services/good/fetchGoodsList'; import { getCategoryList } from '../../../services/good/fetchCategoryList'; import { fetchGoodsCaseView } from '../../../services/home/home'; import Toast from 'tdesign-miniprogram/toast/index'; const initFilters = { overall: 1, sorts: '', layout: 0, }; Page({ data: { goodsList: [], layout: 0, sorts: '', overall: 1, show: false, minVal: '', maxVal: '', keywords: '', page: 1, pageSize: 10, total: 0, tabValue: 0, filter: initFilters, hasLoaded: false, loadMoreStatus: 0, loading: true, tabList: [], caseView: wx.getStorageSync('goodsCaseView') === true, // 案例展示:标题改为软件案例,不显示购物车 }, handleFilterChange(e) { const { layout, overall, sorts } = e.detail; this.setData({ layout, sorts, page: 1, overall, loadMoreStatus: 0, }); this.loadGoodsList(true); }, tabChangeHandle(e) { const tabValue = e.detail.value; this.setData({ tabValue, }); this.loadGoodsList(true); }, //搜索 onChangeInput(e) { const { value } = e.detail; this.setData({ keywords: value }); if (!value) { this.loadGoodsList(true); } }, onChangeSearch(e) { const { value } = e.detail; if (value !== undefined) { this.setData({ keywords: value }); } console.log(e) console.log(value) this.loadGoodsList(true); }, navToSearchPage() { wx.navigateTo({ url: '/pages/goods/search/index' }); }, async init() { try { const result = await getCategoryList(); this.setData({ tabList: result, }); } catch (error) { console.error('err:', error); } }, // 产品列表 async loadGoodsList(reset = false) { if (!reset && (this._loading || this.data.loadMoreStatus !== 0)) { return; } const seq = (this._listSeq || 0) + 1; this._listSeq = seq; this._loading = true; const page = reset ? 1 : this.data.page; const { pageSize, keywords, tabValue } = this.data; this.setData({ loadMoreStatus: 1, loading: true, }); try { const result = await fetchGoodsList({ page, categoryId: tabValue, pageSize, keywords, }); if (seq !== this._listSeq) { return; } if (!result || result.code !== 1 || !result.data) { this.setData({ loading: false, hasLoaded: true, loadMoreStatus: 0, }); wx.showToast({ title: (result && result.msg) || '查询失败,请稍候重试', icon: 'none', }); return; } const { spuList = [], totalCount = 0 } = result.data; if (totalCount === 0 && reset) { this.setData({ emptyInfo: { tip: '抱歉,未找到相关商品', }, total: totalCount, hasLoaded: true, loadMoreStatus: 2, loading: false, goodsList: [], page: 1, }); return; } const start = reset ? 0 : this.data.goodsList.length; const loaded = start + spuList.length; const patch = { total: totalCount, page: page + 1, loadMoreStatus: loaded >= totalCount ? 2 : 0, hasLoaded: true, loading: false, }; if (reset) { patch.goodsList = spuList; } else { spuList.forEach((item, i) => { patch[`goodsList[${start + i}]`] = item; }); } this.setData(patch); } catch (error) { if (seq !== this._listSeq) { return; } console.error('loadGoodsList failed:', error); this.setData({ loading: false, hasLoaded: true, loadMoreStatus: 0, }); } finally { if (seq === this._listSeq) { this._loading = false; } } }, onLoad() { this.applyCaseView(); this.init(); this.loadGoodsList(true); }, onShow() { // 自定义 tabBar 每个 Tab 页各有一份实例,切过来后要按当前路由重设选中项 const tabBar = typeof this.getTabBar === 'function' && this.getTabBar(); if (tabBar) { tabBar.init(); } }, async applyCaseView() { if (this.data.caseView) { wx.setNavigationBarTitle({ title: '软件案例' }); } const caseView = await fetchGoodsCaseView(); this.setData({ caseView }); wx.setNavigationBarTitle({ title: caseView ? '软件案例' : '软件列表' }); }, onReachBottom() { if (this._loading || this.data.loadMoreStatus !== 0) { return; } const { goodsList, total } = this.data; if (goodsList.length >= total) { this.setData({ loadMoreStatus: 2, }); return; } this.loadGoodsList(false); }, tagClickHandle() { Toast({ context: this, selector: '#t-toast', message: '点击标签', }); }, gotoGoodsDetail(e) { console.log(e); const { index } = e.detail; const { spuId } = this.data.goodsList[index]; wx.navigateTo({ url: `/pages/goods/details/index?spuId=${spuId}`, }); }, showFilterPopup() { this.setData({ show: true, }); }, showFilterPopupClose() { this.setData({ show: false, }); }, onMinValAction(e) { const { value } = e.detail; this.setData({ minVal: value }); }, onMaxValAction(e) { const { value } = e.detail; this.setData({ maxVal: value }); }, reset() { this.setData({ minVal: '', maxVal: '' }); }, confirm() { let message = '' if (message) { Toast({ context: this, selector: '#t-toast', message, }); } this.setData( { show: false, page: 1, goodsList: [], loadMoreStatus: 0, }, () => { this.loadGoodsList(); }, ); }, });