160 lines
3.7 KiB
JavaScript
160 lines
3.7 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
userInfo: null,
|
|
currentStore: null,
|
|
products: [],
|
|
loading: false,
|
|
page: 1,
|
|
pageSize: 20,
|
|
hasMore: true,
|
|
showProductModal: false,
|
|
currentProduct: null
|
|
},
|
|
|
|
onLoad() {
|
|
this.initData()
|
|
},
|
|
|
|
onShow() {
|
|
this.setData({
|
|
userInfo: app.globalData.userInfo,
|
|
currentStore: app.globalData.currentStore
|
|
})
|
|
|
|
// 门店切换后刷新商品
|
|
if (app.globalData.storeChanged) {
|
|
app.globalData.storeChanged = false
|
|
this.setData({ page: 1, hasMore: true, products: [] })
|
|
this.fetchProducts()
|
|
}
|
|
},
|
|
|
|
onPullDownRefresh() {
|
|
this.setData({ page: 1, hasMore: true })
|
|
this.fetchProducts().then(() => {
|
|
wx.stopPullDownRefresh()
|
|
})
|
|
},
|
|
|
|
onReachBottom() {
|
|
if (this.data.hasMore && !this.data.loading) {
|
|
this.loadMore()
|
|
}
|
|
},
|
|
|
|
async initData() {
|
|
if (!app.globalData.token) {
|
|
try {
|
|
await app.login()
|
|
} catch (e) {
|
|
console.error('登录失败:', e)
|
|
}
|
|
}
|
|
|
|
this.setData({
|
|
userInfo: app.globalData.userInfo,
|
|
currentStore: app.globalData.currentStore
|
|
})
|
|
this.fetchProducts()
|
|
},
|
|
|
|
async fetchProducts() {
|
|
this.setData({ loading: true })
|
|
|
|
try {
|
|
const params = {
|
|
page: this.data.page,
|
|
pageSize: this.data.pageSize
|
|
}
|
|
|
|
// 根据当前门店筛选商品
|
|
if (this.data.currentStore?.storeId) {
|
|
params.store_id = this.data.currentStore.storeId
|
|
}
|
|
|
|
const res = await app.request('/api/points/products', params)
|
|
|
|
const products = res.data.list || []
|
|
this.setData({
|
|
products: this.data.page === 1 ? products : [...this.data.products, ...products],
|
|
hasMore: products.length >= this.data.pageSize
|
|
})
|
|
} catch (e) {
|
|
console.error('获取商品列表失败:', e)
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
loadMore() {
|
|
this.setData({ page: this.data.page + 1 })
|
|
this.fetchProducts()
|
|
},
|
|
|
|
viewProduct(e) {
|
|
const product = e.currentTarget.dataset.product
|
|
this.setData({
|
|
currentProduct: product,
|
|
showProductModal: true
|
|
})
|
|
},
|
|
|
|
closeProductModal() {
|
|
this.setData({ showProductModal: false })
|
|
},
|
|
|
|
async exchangeProduct() {
|
|
const product = this.data.currentProduct
|
|
|
|
wx.showModal({
|
|
title: '确认兑换',
|
|
content: `确定使用 ${product.pointsRequired} 积分兑换「${product.name}」吗?\n请到 ${product.storeName} 领取`,
|
|
success: async (res) => {
|
|
if (!res.confirm) return
|
|
|
|
wx.showLoading({ title: '兑换中...' })
|
|
|
|
try {
|
|
const exchangeRes = await app.request('/api/points/exchange', {
|
|
product_id: product.id
|
|
}, 'POST')
|
|
|
|
wx.hideLoading()
|
|
|
|
// 更新用户积分
|
|
const newPoints = this.data.userInfo.totalPoints - product.pointsRequired
|
|
app.globalData.userInfo.totalPoints = newPoints
|
|
this.setData({
|
|
'userInfo.totalPoints': newPoints,
|
|
showProductModal: false
|
|
})
|
|
|
|
wx.showModal({
|
|
title: '兑换成功',
|
|
content: `请到 ${product.storeName} 出示兑换码领取\n兑换码: ${exchangeRes.data.exchangeCode}`,
|
|
showCancel: false,
|
|
success: () => {
|
|
wx.navigateTo({ url: '/pages/points/order/index' })
|
|
}
|
|
})
|
|
|
|
this.fetchProducts()
|
|
} catch (e) {
|
|
wx.hideLoading()
|
|
console.error('兑换失败:', e)
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
goToRecords() {
|
|
wx.navigateTo({ url: '/pages/points/records/index' })
|
|
},
|
|
|
|
goToOrders() {
|
|
wx.navigateTo({ url: '/pages/points/order/index' })
|
|
}
|
|
})
|