功能:收口小程序前后台安全与防重复提交

This commit is contained in:
xiaowang
2026-04-20 16:02:57 +08:00
parent eb1c209d79
commit a3df262fae
237 changed files with 14068 additions and 728 deletions
@@ -0,0 +1,291 @@
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'
})
}
}
})
@@ -0,0 +1,5 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar"
}
}
@@ -0,0 +1,111 @@
<navigation-bar title="{{mode === 'edit' ? '编辑优惠券' : '新增优惠券'}}" back="{{true}}" color="#f6efe7" background="#081518"></navigation-bar>
<view class="page-shell">
<view class="page-stack">
<view class="surface-card">
<text class="section-title">优惠券信息</text>
<view class="field-group">
<text class="field-label">优惠券名称</text>
<input class="field-input" value="{{form.couponName}}" data-field="couponName" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">所属店铺</text>
<picker mode="selector" range="{{shops}}" range-key="shopName" value="{{shopIndex}}" bindchange="handleShopChange">
<view class="field-picker">{{shopLabel || '请选择店铺'}}</view>
</picker>
</view>
<view class="field-group">
<text class="field-label">券类型</text>
<picker mode="selector" range="{{couponTypeOptions}}" value="{{couponTypeIndex}}" bindchange="handleCouponTypeChange">
<view class="field-picker">{{couponTypeLabel}}</view>
</picker>
</view>
<view class="field-group">
<text class="field-label">优惠编码</text>
<input class="field-input" value="{{form.couponCode}}" data-field="couponCode" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{couponTypeIndex === 0}}" class="field-group">
<text class="field-label">折扣值</text>
<input class="field-input" type="digit" value="{{form.discountValue}}" data-field="discountValue" bindinput="handleInputChange"></input>
</view>
<block wx:if="{{couponTypeIndex === 1}}">
<view class="field-group">
<text class="field-label">满多少可用</text>
<input class="field-input" type="digit" value="{{form.minAmount}}" data-field="minAmount" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">减免金额</text>
<input class="field-input" type="digit" value="{{form.reduceAmount}}" data-field="reduceAmount" bindinput="handleInputChange"></input>
</view>
</block>
<view class="field-group">
<text class="field-label">总数量</text>
<input class="field-input" type="number" value="{{form.totalCount}}" data-field="totalCount" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">优惠场景</text>
<input class="field-input" value="{{form.couponScene}}" data-field="couponScene" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">VIP 等级限制</text>
<picker mode="selector" range="{{vipLevelOptions}}" value="{{vipLevelIndex}}" bindchange="handleVipLevelChange">
<view class="field-picker">{{vipLevelLabel}}</view>
</picker>
</view>
<view class="field-group">
<text class="field-label">开始日期 / 时间</text>
<view class="action-row">
<picker mode="date" value="{{startDate}}" data-field="startDate" bindchange="handleDateTimeChange">
<view class="field-picker">{{startDate || '开始日期'}}</view>
</picker>
<picker mode="time" value="{{startTime}}" data-field="startTime" bindchange="handleDateTimeChange">
<view class="field-picker">{{startTime || '开始时间'}}</view>
</picker>
</view>
</view>
<view class="field-group">
<text class="field-label">结束日期 / 时间</text>
<view class="action-row">
<picker mode="date" value="{{endDate}}" data-field="endDate" bindchange="handleDateTimeChange">
<view class="field-picker">{{endDate || '结束日期'}}</view>
</picker>
<picker mode="time" value="{{endTime}}" data-field="endTime" bindchange="handleDateTimeChange">
<view class="field-picker">{{endTime || '结束时间'}}</view>
</picker>
</view>
</view>
<view class="field-group">
<text class="field-label">自动匹配</text>
<switch checked="{{form.autoMatch}}" bindchange="handleAutoMatchChange"></switch>
</view>
<view class="field-group">
<text class="field-label">可用标签</text>
<checkbox-group bindchange="handleAvailableTagChange">
<block wx:for="{{availableTags}}" wx:key="id">
<label class="checkbox-chip {{item.checked ? 'active' : ''}}">
<checkbox value="{{item.valueId}}" checked="{{item.checked}}" />
{{item.labelText}}
</label>
</block>
</checkbox-group>
</view>
<view class="field-group">
<text class="field-label">不可用标签</text>
<checkbox-group bindchange="handleUnavailableTagChange">
<block wx:for="{{unavailableTags}}" wx:key="id">
<label class="checkbox-chip {{item.checked ? 'active' : ''}}">
<checkbox value="{{item.valueId}}" checked="{{item.checked}}" />
{{item.labelText}}
</label>
</block>
</checkbox-group>
</view>
<view class="action-row">
<button class="primary-btn" bindtap="handleSubmit">保存优惠券</button>
</view>
</view>
</view>
</view>
@@ -0,0 +1,4 @@
checkbox {
transform: scale(0.75);
margin-right: 8rpx;
}