88 lines
1.9 KiB
JavaScript
88 lines
1.9 KiB
JavaScript
const couponApi = require('../../api/coupon')
|
|
const userApi = require('../../api/user')
|
|
const navigation = require('../../utils/navigation')
|
|
const { getTempRecord, clearTempRecord } = require('../../utils/storage')
|
|
|
|
Page({
|
|
data: {
|
|
coupon: null,
|
|
users: [],
|
|
quantity: 1
|
|
},
|
|
|
|
onLoad() {
|
|
const coupon = getTempRecord('currentCoupon', null)
|
|
this.setData({
|
|
coupon
|
|
})
|
|
this.loadUsers()
|
|
},
|
|
|
|
onUnload() {
|
|
clearTempRecord('currentCoupon')
|
|
},
|
|
|
|
async loadUsers() {
|
|
try {
|
|
const users = await userApi.listAllUsers()
|
|
this.setData({
|
|
users: (Array.isArray(users) ? users : []).map(item => ({
|
|
...item,
|
|
valueId: String(item.id),
|
|
checked: false
|
|
}))
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '可发放用户加载失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
handleUserCheck(event) {
|
|
const selected = event.detail.value || []
|
|
this.setData({
|
|
users: this.data.users.map(item => ({
|
|
...item,
|
|
checked: selected.includes(item.valueId)
|
|
}))
|
|
})
|
|
},
|
|
|
|
handleQuantityInput(event) {
|
|
this.setData({
|
|
quantity: event.detail.value
|
|
})
|
|
},
|
|
|
|
async handleSubmit() {
|
|
const selectedUserIds = this.data.users.filter(item => item.checked).map(item => Number(item.id))
|
|
if (!selectedUserIds.length) {
|
|
wx.showToast({
|
|
title: '请至少选择一个用户',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
try {
|
|
await couponApi.distributeCoupon({
|
|
couponId: this.data.coupon.id,
|
|
userIds: selectedUserIds,
|
|
quantity: Number(this.data.quantity || 1)
|
|
})
|
|
wx.showToast({
|
|
title: '优惠券已发放',
|
|
icon: 'none'
|
|
})
|
|
navigation.goBack('/pages/coupons/index')
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '发放失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|