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

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,183 @@
const shopApi = require('../../api/shop')
const navigation = require('../../utils/navigation')
const upload = require('../../utils/upload')
const { getTempRecord, clearTempRecord } = require('../../utils/storage')
const { getDraftValue, clearDraft, scheduleDraftSave, clearDraftTimer } = require('../../utils/form-draft')
const { markDirty } = require('../../utils/page-state')
const DRAFT_KEY = 'admin-shop-form'
Page({
data: {
mode: 'create',
shopId: '',
uploading: false,
uploadProgress: 0,
statusOptions: ['启用', '停用'],
statusIndex: 0,
form: {
shopName: '',
description: '',
address: '',
businessHours: '',
coverImageFileId: '',
imageFileIds: []
}
},
onLoad(options) {
const mode = options && options.mode ? options.mode : 'create'
const currentShop = getTempRecord('currentShopRecord', null)
const draft = getDraftValue(DRAFT_KEY, null)
this.setData({
mode,
shopId: options && options.id ? options.id : ''
})
if (mode === 'edit' && currentShop) {
this.setData({
form: {
shopName: currentShop.shopName || '',
description: currentShop.description || '',
address: currentShop.address || '',
businessHours: currentShop.businessHours || '',
coverImageFileId: currentShop.coverImageFileId || '',
imageFileIds: currentShop.imageFileIds ? String(currentShop.imageFileIds).split(',').filter(Boolean) : []
},
statusIndex: Number(currentShop.status) === 0 ? 1 : 0
})
}
if (draft && draft.mode === mode) {
this.setData({
form: {
...this.data.form,
...draft.form
},
statusIndex: draft.statusIndex === 1 ? 1 : this.data.statusIndex
})
}
},
onUnload() {
clearTempRecord('currentShopRecord')
clearDraftTimer(this)
},
scheduleShopDraft() {
scheduleDraftSave(this, DRAFT_KEY, () => ({
mode: this.data.mode,
statusIndex: this.data.statusIndex,
form: {
...this.data.form,
imageFileIds: this.data.form.imageFileIds.slice()
}
}))
},
handleInputChange(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`form.${field}`]: event.detail.value
})
this.scheduleShopDraft()
},
handleStatusChange(event) {
this.setData({
statusIndex: Number(event.detail.value)
})
this.scheduleShopDraft()
},
async handleUploadBanner() {
await this.handleUpload('admin-shop-banner', true)
},
async handleUploadImages() {
await this.handleUpload('admin-shop-image', false)
},
async handleUpload(scene, isBanner) {
try {
const files = await upload.chooseImageFiles({
count: isBanner ? 1 : 9
})
this.setData({
uploading: true,
uploadProgress: 0
})
const results = await upload.uploadImageFiles({
files,
scene,
onProgress: progress => {
this.setData({
uploadProgress: Math.round(progress)
})
}
})
if (isBanner) {
this.setData({
'form.coverImageFileId': results[0] ? String(results[0].id) : ''
})
} else {
this.setData({
'form.imageFileIds': this.data.form.imageFileIds.concat(results.map(item => String(item.id)))
})
}
this.scheduleShopDraft()
} catch (error) {
wx.showToast({
title: error.message || '图片上传失败',
icon: 'none'
})
} finally {
this.setData({
uploading: false
})
}
},
async handleSubmit() {
if (!this.data.form.shopName) {
wx.showToast({
title: '请输入店铺名称',
icon: 'none'
})
return
}
const payload = {
shopName: this.data.form.shopName,
description: this.data.form.description || undefined,
address: this.data.form.address || undefined,
businessHours: this.data.form.businessHours || undefined,
coverImageFileId: this.data.form.coverImageFileId || undefined,
imageFileIds: this.data.form.imageFileIds.join(',') || undefined,
status: this.data.statusIndex === 1 ? 0 : 1
}
try {
if (this.data.mode === 'edit') {
await shopApi.updateShop(this.data.shopId, payload)
} else {
await shopApi.createShop(payload)
}
clearDraft(DRAFT_KEY)
markDirty('admin-shops')
markDirty('admin-workbench')
wx.showToast({
title: this.data.mode === 'edit' ? '店铺已更新' : '店铺已创建',
icon: 'none'
})
navigation.goBack('/pages/shops/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,55 @@
<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.shopName}}" data-field="shopName" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">店铺地址</text>
<input class="field-input" value="{{form.address}}" data-field="address" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">营业时间</text>
<input class="field-input" value="{{form.businessHours}}" data-field="businessHours" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">店铺描述</text>
<textarea class="field-textarea" value="{{form.description}}" data-field="description" bindinput="handleInputChange"></textarea>
</view>
<view class="field-group">
<text class="field-label">状态</text>
<picker mode="selector" range="{{statusOptions}}" value="{{statusIndex}}" bindchange="handleStatusChange">
<view class="field-picker">{{statusOptions[statusIndex]}}</view>
</picker>
</view>
<view class="field-group">
<text class="field-label">Banner 图</text>
<view class="list-actions">
<view class="mini-btn primary" bindtap="handleUploadBanner">上传 Banner</view>
</view>
<view wx:if="{{form.coverImageFileId}}" class="image-grid">
<view class="image-chip">{{form.coverImageFileId}}</view>
</view>
</view>
<view class="field-group">
<text class="field-label">店铺图片</text>
<view class="list-actions">
<view class="mini-btn primary" bindtap="handleUploadImages">上传图片</view>
</view>
<view wx:if="{{form.imageFileIds.length}}" class="image-grid">
<block wx:for="{{form.imageFileIds}}" wx:key="*this">
<view class="image-chip">{{item}}</view>
</block>
</view>
</view>
<view wx:if="{{uploading}}" class="hint-card">图片上传中,当前进度 {{uploadProgress}}%</view>
<view class="action-row">
<button class="primary-btn" disabled="{{uploading}}" bindtap="handleSubmit">保存店铺</button>
</view>
</view>
</view>
</view>
@@ -0,0 +1,3 @@
.page-shell {
background: transparent;
}