const tagApi = require('../../api/tag') const productApi = require('../../api/product') const { buildProductCards } = require('../../utils/product') const navigation = require('../../utils/navigation') const { runWithLoading } = require('../../utils/loading') Page({ data: { initialized: false, loading: false, categories: ['全部'], selectedCategory: '全部', searchKeyword: '', products: [], activeBannerText: '' }, onLoad(options) { this.bootstrapPage(options || {}) }, onShow() { const app = getApp() const pendingFilter = app && app.globalData ? app.globalData.pendingProductFilter : null if (this.data.initialized && pendingFilter && (pendingFilter.keyword || pendingFilter.tag)) { if (app && app.globalData) { app.globalData.pendingProductFilter = null } this.applyFilter(pendingFilter) } }, async onPullDownRefresh() { await this.loadProducts() wx.stopPullDownRefresh() }, async bootstrapPage(options) { this.setData({ loading: true }) try { await this.loadTags() await this.applyInitialFilter(options) this.setData({ initialized: true }) } catch (error) { wx.showToast({ title: error.message || '作品列表加载失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, async loadTags() { const tags = await tagApi.getAllTags() const names = Array.isArray(tags) ? tags.map(item => item.tagName).filter(Boolean) : [] this.setData({ categories: ['全部'].concat(names) }) }, async applyInitialFilter(options) { const app = getApp() const pendingFilter = app && app.globalData ? app.globalData.pendingProductFilter : null if (pendingFilter && (pendingFilter.keyword || pendingFilter.tag)) { if (app && app.globalData) { app.globalData.pendingProductFilter = null } await this.applyFilter(pendingFilter) return } await this.applyFilter(options || {}) }, async applyFilter(filter) { const keyword = (filter.keyword || '').trim() const tag = (filter.tag || '').trim() this.setData({ searchKeyword: keyword, selectedCategory: tag && this.data.categories.includes(tag) ? tag : '全部' }) await this.loadProducts() }, async loadProducts() { this.setData({ loading: true }) try { let products = [] const keyword = this.data.searchKeyword.trim() const tag = this.data.selectedCategory if (keyword) { products = await productApi.searchProducts(keyword) } else if (tag !== '全部') { products = await productApi.getProductsByTag(tag) } else { products = await productApi.getAllProducts() } const cards = await buildProductCards(products || []) this.setData({ products: cards, activeBannerText: keyword ? `正在显示“${keyword}”的搜索结果` : (tag !== '全部' ? `当前按“${tag}”筛选` : '') }) } catch (error) { this.setData({ products: [], activeBannerText: '' }) wx.showToast({ title: error.message || '作品列表加载失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, handleSearchInput(event) { this.setData({ searchKeyword: event.detail.value }) }, async handleSearchConfirm() { this.setData({ selectedCategory: '全部' }) await runWithLoading(() => this.loadProducts()) }, async handleSelectCategory(event) { const category = event.currentTarget.dataset.category this.setData({ selectedCategory: category, searchKeyword: '' }) await runWithLoading(() => this.loadProducts()) }, async handleResetFilters() { this.setData({ selectedCategory: '全部', searchKeyword: '' }) await runWithLoading(() => this.loadProducts()) }, handleProductSelect(event) { const product = event.detail || {} if (!product.id) { return } navigation.navigate('/pages/product-detail/index', { id: product.id }) } })