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

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,256 @@
const userApi = require('../../api/user')
const shopApi = require('../../api/shop')
const navigation = require('../../utils/navigation')
const cache = require('../../utils/cache')
const { getTempRecord, clearTempRecord } = require('../../utils/storage')
const { getDraftValue, clearDraft, scheduleDraftSave, clearDraftTimer } = require('../../utils/form-draft')
const { markDirty } = require('../../utils/page-state')
const { CACHE_TTL } = require('../../utils/config')
const ACCOUNT_REGEX = /[^A-Za-z0-9]/g
const PHONE_REGEX = /[^\d]/g
const DRAFT_KEY = 'admin-user-form'
const TYPE_OPTIONS = [
{ label: '普通用户', value: 0 },
{ label: '店长', value: 2 },
{ label: '美甲师', value: 3 },
{ label: '平台管理员', value: 4 }
]
function normalizeId(value) {
return value === undefined || value === null ? '' : String(value)
}
Page({
data: {
mode: 'create',
userId: '',
typeOptions: TYPE_OPTIONS,
currentTypeLabel: TYPE_OPTIONS[0].label,
shops: [],
selectedShopIds: [],
form: {
nickname: '',
account: '',
phone: '',
password: '',
signature: '',
type: 0
}
},
onLoad(options) {
const mode = options && options.mode ? options.mode : 'create'
const currentUser = getTempRecord('currentUser', null)
const draft = getDraftValue(DRAFT_KEY, null)
const app = getApp()
const operator = app.globalData.userInfo || {}
let availableTypes = TYPE_OPTIONS
if (Number(operator.type) === 2) {
availableTypes = TYPE_OPTIONS.filter(item => item.value === 0 || item.value === 3)
}
const baseState = {
mode,
userId: options && options.id ? options.id : '',
typeOptions: availableTypes,
currentTypeLabel: availableTypes[0] ? availableTypes[0].label : ''
}
if (mode === 'edit' && currentUser) {
baseState.form = {
nickname: currentUser.nickname || '',
account: currentUser.account || '',
phone: currentUser.phone || '',
password: '',
signature: currentUser.signature || '',
type: Number(currentUser.type || 0)
}
baseState.selectedShopIds = Array.isArray(currentUser.shopIds) ? currentUser.shopIds.map(item => String(item)) : []
}
this.setData(baseState)
if (draft && draft.mode === mode) {
this.setData({
form: {
...this.data.form,
...draft.form,
password: ''
},
selectedShopIds: Array.isArray(draft.selectedShopIds) ? draft.selectedShopIds : this.data.selectedShopIds
})
}
this.syncTypeLabel(Number(this.data.form.type || 0))
this.loadShops()
},
onUnload() {
clearTempRecord('currentUser')
clearDraftTimer(this)
},
scheduleUserDraft() {
scheduleDraftSave(this, DRAFT_KEY, () => ({
mode: this.data.mode,
form: {
nickname: this.data.form.nickname,
account: this.data.form.account,
phone: this.data.form.phone,
signature: this.data.form.signature,
type: Number(this.data.form.type)
},
selectedShopIds: this.data.selectedShopIds.slice()
}))
},
async loadShops() {
try {
const shops = await cache.remember('admin:shops:all', CACHE_TTL.dict, () => shopApi.listAllShops())
const app = getApp()
const operator = app.globalData.userInfo || {}
const currentShop = app.globalData.currentShop
if (Number(operator.type) !== 4 && currentShop) {
this.setData({
shops: (Array.isArray(shops) ? shops : []).filter(item => normalizeId(item.id) === normalizeId(currentShop.id)).map(item => ({
...item,
valueId: normalizeId(item.id),
checked: true
})),
selectedShopIds: [normalizeId(currentShop.id)]
})
this.scheduleUserDraft()
return
}
this.setData({
shops: (Array.isArray(shops) ? shops : []).map(item => ({
...item,
valueId: normalizeId(item.id),
checked: this.data.selectedShopIds.includes(normalizeId(item.id))
}))
})
} catch (error) {
wx.showToast({
title: error.message || '店铺列表加载失败',
icon: 'none'
})
}
},
handleInputChange(event) {
const field = event.currentTarget.dataset.field
let value = event.detail.value
if (field === 'account') {
value = String(value || '').replace(ACCOUNT_REGEX, '')
}
if (field === 'phone') {
value = String(value || '').replace(PHONE_REGEX, '').slice(0, 11)
}
this.setData({
[`form.${field}`]: value
})
if (field !== 'password') {
this.scheduleUserDraft()
}
},
handleTypeChange(event) {
const index = Number(event.detail.value)
const item = this.data.typeOptions[index]
this.setData({
'form.type': item ? item.value : 0,
currentTypeLabel: item ? item.label : ''
})
this.scheduleUserDraft()
},
handleShopCheck(event) {
const selectedShopIds = event.detail.value || []
this.setData({
selectedShopIds,
shops: this.data.shops.map(item => ({
...item,
checked: selectedShopIds.includes(String(item.id))
}))
})
this.scheduleUserDraft()
},
validateForm() {
const form = this.data.form
if (!form.nickname) {
return '请输入用户昵称'
}
if (!form.account) {
return '请输入账号'
}
if (!form.phone || form.phone.length !== 11) {
return '请输入 11 位手机号'
}
if (this.data.mode === 'create' && !form.password) {
return '新增用户时必须设置密码'
}
if (!this.data.selectedShopIds.length) {
return '请至少绑定一个店铺'
}
return ''
},
async handleSubmit() {
const errorMessage = this.validateForm()
if (errorMessage) {
wx.showToast({
title: errorMessage,
icon: 'none'
})
return
}
const payload = {
nickname: this.data.form.nickname,
account: this.data.form.account,
phone: this.data.form.phone,
signature: this.data.form.signature || undefined,
type: Number(this.data.form.type),
shopIds: this.data.selectedShopIds.map(item => Number(item))
}
if (this.data.form.password) {
payload.password = this.data.form.password
}
try {
if (this.data.mode === 'edit') {
await userApi.updateUser(this.data.userId, payload)
} else {
await userApi.createUser(payload)
}
clearDraft(DRAFT_KEY)
markDirty('admin-users')
wx.showToast({
title: this.data.mode === 'edit' ? '用户已更新' : '用户已创建',
icon: 'none'
})
navigation.goBack('/pages/users/index')
} catch (error) {
wx.showToast({
title: error.message || '保存失败',
icon: 'none'
})
}
},
syncTypeLabel(type) {
const target = this.data.typeOptions.find(item => Number(item.value) === Number(type))
this.setData({
currentTypeLabel: target ? target.label : ''
})
}
})
@@ -0,0 +1,5 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar"
}
}
@@ -0,0 +1,51 @@
<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">{{mode === 'edit' ? '更新用户资料' : '创建后台账号'}}</text>
<text class="section-desc">账号仅允许字母和数字,手机号保持全局唯一。店长创建美甲师时,这里会直接绑定当前店铺。</text>
<view class="field-group">
<text class="field-label">昵称</text>
<input class="field-input" value="{{form.nickname}}" data-field="nickname" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">账号</text>
<input class="field-input" value="{{form.account}}" data-field="account" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">手机号</text>
<input class="field-input" type="number" value="{{form.phone}}" data-field="phone" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">{{mode === 'edit' ? '密码(留空则不修改)' : '登录密码'}}</text>
<input class="field-input" password value="{{form.password}}" data-field="password" bindinput="handleInputChange"></input>
</view>
<view class="field-group">
<text class="field-label">签名</text>
<textarea class="field-textarea" value="{{form.signature}}" data-field="signature" bindinput="handleInputChange"></textarea>
</view>
<view class="field-group">
<text class="field-label">角色</text>
<picker mode="selector" range="{{typeOptions}}" range-key="label" value="0" bindchange="handleTypeChange">
<view class="field-picker">{{currentTypeLabel || '请选择角色'}}</view>
</picker>
</view>
<view class="field-group">
<text class="field-label">绑定店铺</text>
<checkbox-group bindchange="handleShopCheck">
<block wx:for="{{shops}}" wx:key="id">
<label class="checkbox-chip {{item.checked ? 'active' : ''}}">
<checkbox value="{{item.valueId}}" checked="{{item.checked}}" />
{{item.shopName}}
</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;
}