292 lines
9.1 KiB
JavaScript
292 lines
9.1 KiB
JavaScript
const couponApi = require('../../api/coupon')
|
|
const tagApi = require('../../api/tag')
|
|
const shopApi = require('../../api/shop')
|
|
const navigation = require('../../utils/navigation')
|
|
const format = require('../../utils/format')
|
|
const { getTempRecord, clearTempRecord } = require('../../utils/storage')
|
|
|
|
function normalizeId(value) {
|
|
return value === undefined || value === null ? '' : String(value)
|
|
}
|
|
|
|
function buildCheckableList(list, selectedIds, labelKey) {
|
|
return (Array.isArray(list) ? list : []).map(item => ({
|
|
...item,
|
|
valueId: normalizeId(item.id),
|
|
labelText: item[labelKey],
|
|
checked: selectedIds.includes(normalizeId(item.id))
|
|
}))
|
|
}
|
|
|
|
function syncSelectedIds(selectedIds, list) {
|
|
const availableIds = new Set((Array.isArray(list) ? list : []).map(item => normalizeId(item.id)))
|
|
return (Array.isArray(selectedIds) ? selectedIds : []).filter(id => availableIds.has(normalizeId(id)))
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
mode: 'create',
|
|
couponId: '',
|
|
shops: [],
|
|
shopIndex: 0,
|
|
shopLabel: '',
|
|
couponTypeOptions: ['折扣券', '满减券'],
|
|
couponTypeIndex: 0,
|
|
couponTypeLabel: '折扣券',
|
|
vipLevelOptions: ['普通', '青铜', '白银', '黄金'],
|
|
vipLevelIndex: 0,
|
|
vipLevelLabel: '普通',
|
|
startDate: '',
|
|
startTime: '',
|
|
endDate: '',
|
|
endTime: '',
|
|
availableTags: [],
|
|
unavailableTags: [],
|
|
form: {
|
|
couponName: '',
|
|
couponCode: '',
|
|
discountValue: '',
|
|
minAmount: '',
|
|
reduceAmount: '',
|
|
totalCount: '',
|
|
couponScene: '',
|
|
autoMatch: false
|
|
}
|
|
},
|
|
|
|
onLoad(options) {
|
|
const mode = options && options.mode ? options.mode : 'create'
|
|
const currentCoupon = getTempRecord('currentCoupon', null)
|
|
this.setData({
|
|
mode,
|
|
couponId: options && options.id ? options.id : ''
|
|
})
|
|
|
|
this._selectedAvailableTagIds = []
|
|
this._selectedUnavailableTagIds = []
|
|
|
|
if (mode === 'edit' && currentCoupon) {
|
|
const start = format.parseDateTimeToPicker(currentCoupon.startTime)
|
|
const end = format.parseDateTimeToPicker(currentCoupon.endTime)
|
|
this._selectedAvailableTagIds = Array.isArray(currentCoupon.availableTags) ? currentCoupon.availableTags.map(item => String(item.id)) : []
|
|
this._selectedUnavailableTagIds = Array.isArray(currentCoupon.unavailableTags) ? currentCoupon.unavailableTags.map(item => String(item.id)) : []
|
|
this._initialShopId = currentCoupon.shopId
|
|
this.setData({
|
|
form: {
|
|
couponName: currentCoupon.couponName || '',
|
|
couponCode: currentCoupon.couponCode || '',
|
|
discountValue: currentCoupon.discountValue || '',
|
|
minAmount: currentCoupon.minAmount || '',
|
|
reduceAmount: currentCoupon.reduceAmount || '',
|
|
totalCount: currentCoupon.totalCount || '',
|
|
couponScene: currentCoupon.couponScene || '',
|
|
autoMatch: Number(currentCoupon.autoMatch) === 1
|
|
},
|
|
couponTypeIndex: Number(currentCoupon.couponType) === 2 ? 1 : 0,
|
|
couponTypeLabel: Number(currentCoupon.couponType) === 2 ? '满减券' : '折扣券',
|
|
vipLevelIndex: this.data.vipLevelOptions.findIndex(item => item === (currentCoupon.requiredVipLevel || '普通')),
|
|
vipLevelLabel: currentCoupon.requiredVipLevel || '普通',
|
|
startDate: start.date,
|
|
startTime: start.time,
|
|
endDate: end.date,
|
|
endTime: end.time
|
|
})
|
|
}
|
|
|
|
this.loadDependencies()
|
|
},
|
|
|
|
onUnload() {
|
|
clearTempRecord('currentCoupon')
|
|
},
|
|
|
|
async loadDependencies() {
|
|
const app = getApp()
|
|
const currentShop = app.globalData.currentShop
|
|
const operator = app.globalData.userInfo || {}
|
|
|
|
try {
|
|
const shopList = await shopApi.listAllShops()
|
|
|
|
let shops = Array.isArray(shopList) ? shopList : []
|
|
if (Number(operator.type) !== 4 && currentShop) {
|
|
shops = shops.filter(item => normalizeId(item.id) === normalizeId(currentShop.id))
|
|
}
|
|
|
|
let shopIndex = 0
|
|
if (this._initialShopId) {
|
|
const index = shops.findIndex(item => normalizeId(item.id) === normalizeId(this._initialShopId))
|
|
if (index >= 0) {
|
|
shopIndex = index
|
|
}
|
|
}
|
|
const selectedShop = shops[shopIndex]
|
|
|
|
this.setData({
|
|
shops,
|
|
shopIndex,
|
|
shopLabel: selectedShop ? selectedShop.shopName : ''
|
|
})
|
|
|
|
await this.loadTagOptions(selectedShop ? selectedShop.id : '')
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '初始化优惠券表单失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
async loadTagOptions(shopId) {
|
|
if (!normalizeId(shopId)) {
|
|
this._selectedAvailableTagIds = []
|
|
this._selectedUnavailableTagIds = []
|
|
this.setData({
|
|
availableTags: [],
|
|
unavailableTags: []
|
|
})
|
|
return
|
|
}
|
|
|
|
try {
|
|
const tagList = await tagApi.listTagsByShopId(shopId)
|
|
this._selectedAvailableTagIds = syncSelectedIds(this._selectedAvailableTagIds, tagList)
|
|
this._selectedUnavailableTagIds = syncSelectedIds(this._selectedUnavailableTagIds, tagList)
|
|
this.setData({
|
|
availableTags: buildCheckableList(tagList, this._selectedAvailableTagIds, 'tagName'),
|
|
unavailableTags: buildCheckableList(tagList, this._selectedUnavailableTagIds, 'tagName')
|
|
})
|
|
} catch (error) {
|
|
this.setData({
|
|
availableTags: [],
|
|
unavailableTags: []
|
|
})
|
|
wx.showToast({
|
|
title: error.message || '可用标签加载失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
handleInputChange(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
const value = event.detail.value
|
|
this.setData({
|
|
[`form.${field}`]: value
|
|
})
|
|
},
|
|
|
|
handleShopChange(event) {
|
|
const shopIndex = Number(event.detail.value)
|
|
const selectedShop = this.data.shops[shopIndex]
|
|
this.setData({
|
|
shopIndex,
|
|
shopLabel: selectedShop ? selectedShop.shopName : ''
|
|
})
|
|
this.loadTagOptions(selectedShop ? selectedShop.id : '')
|
|
},
|
|
|
|
handleCouponTypeChange(event) {
|
|
const couponTypeIndex = Number(event.detail.value)
|
|
this.setData({
|
|
couponTypeIndex,
|
|
couponTypeLabel: this.data.couponTypeOptions[couponTypeIndex]
|
|
})
|
|
},
|
|
|
|
handleVipLevelChange(event) {
|
|
const vipLevelIndex = Number(event.detail.value)
|
|
this.setData({
|
|
vipLevelIndex,
|
|
vipLevelLabel: this.data.vipLevelOptions[vipLevelIndex]
|
|
})
|
|
},
|
|
|
|
handleDateTimeChange(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
this.setData({
|
|
[field]: event.detail.value
|
|
})
|
|
},
|
|
|
|
handleAutoMatchChange(event) {
|
|
this.setData({
|
|
'form.autoMatch': event.detail.value
|
|
})
|
|
},
|
|
|
|
handleAvailableTagChange(event) {
|
|
this._selectedAvailableTagIds = event.detail.value || []
|
|
this.setData({
|
|
availableTags: this.data.availableTags.map(item => ({
|
|
...item,
|
|
checked: this._selectedAvailableTagIds.includes(item.valueId)
|
|
}))
|
|
})
|
|
},
|
|
|
|
handleUnavailableTagChange(event) {
|
|
this._selectedUnavailableTagIds = event.detail.value || []
|
|
this.setData({
|
|
unavailableTags: this.data.unavailableTags.map(item => ({
|
|
...item,
|
|
checked: this._selectedUnavailableTagIds.includes(item.valueId)
|
|
}))
|
|
})
|
|
},
|
|
|
|
async handleSubmit() {
|
|
const selectedShop = this.data.shops[this.data.shopIndex]
|
|
if (!this.data.form.couponName) {
|
|
wx.showToast({
|
|
title: '请输入优惠券名称',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
if (!selectedShop) {
|
|
wx.showToast({
|
|
title: '请选择所属店铺',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
const payload = {
|
|
couponCode: this.data.form.couponCode || undefined,
|
|
couponName: this.data.form.couponName,
|
|
couponType: this.data.couponTypeIndex === 1 ? 2 : 1,
|
|
discountValue: this.data.couponTypeIndex === 0 ? Number(this.data.form.discountValue || 0) : undefined,
|
|
minAmount: this.data.couponTypeIndex === 1 ? Number(this.data.form.minAmount || 0) : undefined,
|
|
reduceAmount: this.data.couponTypeIndex === 1 ? Number(this.data.form.reduceAmount || 0) : undefined,
|
|
totalCount: this.data.form.totalCount ? Number(this.data.form.totalCount) : undefined,
|
|
startTime: format.buildDateTime(this.data.startDate, this.data.startTime),
|
|
endTime: format.buildDateTime(this.data.endDate, this.data.endTime),
|
|
shopId: selectedShop.id,
|
|
couponScene: this.data.form.couponScene || undefined,
|
|
requiredVipLevel: this.data.vipLevelLabel || undefined,
|
|
autoMatch: this.data.form.autoMatch ? 1 : 0,
|
|
availableTagIds: this._selectedAvailableTagIds.map(item => Number(item)),
|
|
unavailableTagIds: this._selectedUnavailableTagIds.map(item => Number(item))
|
|
}
|
|
|
|
try {
|
|
if (this.data.mode === 'edit') {
|
|
await couponApi.updateCoupon(this.data.couponId, payload)
|
|
} else {
|
|
await couponApi.createCoupon(payload)
|
|
}
|
|
wx.showToast({
|
|
title: this.data.mode === 'edit' ? '优惠券已更新' : '优惠券已创建',
|
|
icon: 'none'
|
|
})
|
|
navigation.goBack('/pages/coupons/index')
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '保存失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|