yingsa/miniprogram/pages/points/order/index.js

131 lines
2.8 KiB
JavaScript

const app = getApp()
const util = require('../../../utils/util')
Page({
data: {
orders: [],
status: '',
loading: false,
page: 1,
pageSize: 20,
hasMore: true,
showOrderModal: false,
currentOrder: null
},
onLoad() {
this.fetchOrders()
},
onPullDownRefresh() {
this.setData({ page: 1, hasMore: true })
this.fetchOrders().then(() => {
wx.stopPullDownRefresh()
})
},
onReachBottom() {
if (this.data.hasMore && !this.data.loading) {
this.loadMore()
}
},
async fetchOrders() {
this.setData({ loading: true })
try {
const params = {
page: this.data.page,
pageSize: this.data.pageSize
}
if (this.data.status !== '') {
params.status = this.data.status
}
const res = await app.request('/api/points/orders', params)
const orders = (res.data.list || []).map(order => ({
...order,
createdAt: util.formatDate(order.createdAt)
}))
this.setData({
orders: this.data.page === 1 ? orders : [...this.data.orders, ...orders],
hasMore: orders.length >= this.data.pageSize
})
} catch (e) {
console.error('获取订单列表失败:', e)
} finally {
this.setData({ loading: false })
}
},
loadMore() {
this.setData({ page: this.data.page + 1 })
this.fetchOrders()
},
setStatus(e) {
const status = e.currentTarget.dataset.status
this.setData({ status, page: 1, hasMore: true })
this.fetchOrders()
},
async viewOrder(e) {
const order = e.currentTarget.dataset.order
wx.showLoading({ title: '加载中...' })
try {
const res = await app.request(`/api/points/orders/${order.id}`)
wx.hideLoading()
const orderData = {
...res.data,
createdAt: util.formatDate(res.data.createdAt),
qrcodeImage: ''
}
this.setData({
currentOrder: orderData,
showOrderModal: true
})
// 如果待核销,获取二维码图片
if (res.data.status === 0 && res.data.exchangeCode) {
this.generateQrcode(res.data.exchangeCode)
}
} catch (e) {
wx.hideLoading()
console.error('获取订单详情失败:', e)
wx.showToast({ title: '获取订单详情失败', icon: 'none' })
}
},
// 生成二维码
async generateQrcode(code) {
try {
const res = await app.request('/api/points/orders/qrcode', { code })
if (res.data && res.data.qrcode) {
this.setData({
'currentOrder.qrcodeImage': res.data.qrcode
})
}
} catch (e) {
console.error('生成二维码失败:', e)
}
},
closeOrderModal() {
this.setData({
showOrderModal: false,
currentOrder: null
})
},
goToMall() {
wx.switchTab({ url: '/pages/points/mall/index' })
}
})