Files

58 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2026-07-07 16:38:13 +08:00
const { listCategories, listProducts } = require('../../api/productApi')
Page({
data: {
loading: false,
categories: [],
activeCategoryId: null,
2026-07-14 16:04:51 +08:00
keyword: '',
2026-07-07 16:38:13 +08:00
products: []
},
onLoad() {
this.load()
},
load() {
this.setData({ loading: true })
2026-07-14 16:04:51 +08:00
Promise.all([listCategories(), listProducts(null, this.data.keyword)])
2026-07-07 16:38:13 +08:00
.then(([categories, products]) => {
this.setData({
categories: categories || [],
products: products || []
})
})
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
.finally(() => this.setData({ loading: false }))
},
chooseCategory(event) {
const categoryId = event.currentTarget.dataset.id
this.setData({ activeCategoryId: categoryId })
2026-07-14 16:04:51 +08:00
listProducts(categoryId, this.data.keyword)
2026-07-07 16:38:13 +08:00
.then((products) => this.setData({ products: products || [] }))
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
},
clearCategory() {
this.setData({ activeCategoryId: null })
2026-07-14 16:04:51 +08:00
listProducts(null, this.data.keyword)
.then((products) => this.setData({ products: products || [] }))
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
},
onKeywordInput(event) {
this.setData({ keyword: event.detail.value })
},
search() {
listProducts(this.data.activeCategoryId, this.data.keyword)
2026-07-07 16:38:13 +08:00
.then((products) => this.setData({ products: products || [] }))
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
},
goDetail(event) {
wx.navigateTo({ url: '/pages/products/detail?id=' + event.currentTarget.dataset.id })
}
})