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 : '' }) } })