96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
const couponApi = require('../../api/coupon')
|
|
const format = require('../../utils/format')
|
|
const navigation = require('../../utils/navigation')
|
|
const { setTempRecord, clearTempRecord } = require('../../utils/storage')
|
|
|
|
Page({
|
|
data: {
|
|
list: [],
|
|
canCreate: false
|
|
},
|
|
|
|
onShow() {
|
|
if (!navigation.ensureLogin('进入优惠券管理')) {
|
|
return
|
|
}
|
|
|
|
const permissionCodes = getApp().globalData.permissionCodes || []
|
|
this.setData({
|
|
canCreate: permissionCodes.includes('coupon:create')
|
|
})
|
|
this.loadCoupons()
|
|
},
|
|
|
|
async loadCoupons() {
|
|
try {
|
|
const result = await couponApi.listCoupons({
|
|
page: 1,
|
|
size: 20
|
|
})
|
|
this.setData({
|
|
list: (Array.isArray(result && result.list) ? result.list : []).map(item => ({
|
|
...item,
|
|
displayTypeText: format.getCouponTypeText(item.couponType),
|
|
displayValueText: format.getCouponValueText(item),
|
|
displayShopName: item.shopName || '当前店铺'
|
|
}))
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '优惠券加载失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
handleCreate() {
|
|
clearTempRecord('currentCoupon')
|
|
navigation.navigate('/pages/coupon-form/index', {
|
|
mode: 'create'
|
|
}).catch(() => undefined)
|
|
},
|
|
|
|
handleEdit(event) {
|
|
const coupon = event.currentTarget.dataset.coupon
|
|
setTempRecord('currentCoupon', coupon)
|
|
navigation.navigate('/pages/coupon-form/index', {
|
|
mode: 'edit',
|
|
id: coupon.id
|
|
}).catch(() => undefined)
|
|
},
|
|
|
|
handleDistribute(event) {
|
|
const coupon = event.currentTarget.dataset.coupon
|
|
setTempRecord('currentCoupon', coupon)
|
|
navigation.navigate('/pages/coupon-distribute/index', {
|
|
id: coupon.id
|
|
}).catch(() => undefined)
|
|
},
|
|
|
|
handleDelete(event) {
|
|
const coupon = event.currentTarget.dataset.coupon
|
|
wx.showModal({
|
|
title: '删除优惠券',
|
|
content: `确认删除优惠券“${coupon.couponName}”吗?`,
|
|
success: async result => {
|
|
if (!result.confirm) {
|
|
return
|
|
}
|
|
try {
|
|
await couponApi.deleteCoupon(coupon.id)
|
|
wx.showToast({
|
|
title: '优惠券已删除',
|
|
icon: 'none'
|
|
})
|
|
this.loadCoupons()
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '优惠券删除失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|