/** * Purpose: userApi supports index.js by holding or executing the user api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const userApi = require('../../api/user') /** * Purpose: shopApi supports index.js by holding or executing the shop api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const shopApi = require('../../api/shop') /** * Purpose: navigation supports index.js by holding or executing the navigation page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const navigation = require('../../utils/navigation') /** * Purpose: cache supports index.js by holding or executing the cache page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ 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') /** * Purpose: ACCOUNT_REGEX supports index.js by holding or executing the account regex page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const ACCOUNT_REGEX = /[^A-Za-z0-9]/g /** * Purpose: PHONE_REGEX supports index.js by holding or executing the phone regex page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const PHONE_REGEX = /[^\d]/g /** * Purpose: DRAFT_KEY supports index.js by holding or executing the draft key page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const DRAFT_KEY = 'admin-user-form' /** * Purpose: TYPE_OPTIONS supports index.js by holding or executing the type options page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const TYPE_OPTIONS = [ { label: '普通用户', value: 0 }, { label: '店长', value: 2 }, { label: '美甲师', value: 3 }, { label: '平台管理员', value: 4 } ] /** * Purpose: normalizeId supports index.js by holding or executing the normalize id page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function normalizeId(value) { return value === undefined || value === null ? '' : String(value) } Page({ /** * Purpose: data supports index.js by holding or executing the data page or API role. * Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow. */ data: { /** * Purpose: mode supports index.js by holding or executing the mode page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ mode: 'create', /** * Purpose: userId supports index.js by holding or executing the user id page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ userId: '', /** * Purpose: typeOptions supports index.js by holding or executing the type options page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ typeOptions: TYPE_OPTIONS, /** * Purpose: currentTypeLabel supports index.js by holding or executing the current type label page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentTypeLabel: TYPE_OPTIONS[0].label, /** * Purpose: shops supports index.js by holding or executing the shops page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ shops: [], /** * Purpose: selectedShopIds supports index.js by holding or executing the selected shop ids page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ selectedShopIds: [], /** * Purpose: form supports index.js by holding or executing the form page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ form: { /** * Purpose: nickname supports index.js by holding or executing the nickname page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ nickname: '', /** * Purpose: account supports index.js by holding or executing the account page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ account: '', /** * Purpose: phone supports index.js by holding or executing the phone page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ phone: '', /** * Purpose: password supports index.js by holding or executing the password page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ password: '', /** * Purpose: signature supports index.js by holding or executing the signature page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ signature: '', /** * Purpose: type supports index.js by holding or executing the type page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ type: 0 } }, /** * Purpose: onLoad supports index.js by holding or executing the on load page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ 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, /** * Purpose: userId supports index.js by holding or executing the user id page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ userId: options && options.id ? options.id : '', /** * Purpose: typeOptions supports index.js by holding or executing the type options page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ typeOptions: availableTypes, /** * Purpose: currentTypeLabel supports index.js by holding or executing the current type label page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentTypeLabel: availableTypes[0] ? availableTypes[0].label : '' } if (mode === 'edit' && currentUser) { baseState.form = { /** * Purpose: nickname supports index.js by holding or executing the nickname page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ nickname: currentUser.nickname || '', /** * Purpose: account supports index.js by holding or executing the account page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ account: currentUser.account || '', /** * Purpose: phone supports index.js by holding or executing the phone page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ phone: currentUser.phone || '', /** * Purpose: password supports index.js by holding or executing the password page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ password: '', /** * Purpose: signature supports index.js by holding or executing the signature page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ signature: currentUser.signature || '', /** * Purpose: type supports index.js by holding or executing the type page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ 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({ /** * Purpose: form supports index.js by holding or executing the form page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ form: { ...this.data.form, ...draft.form, /** * Purpose: password supports index.js by holding or executing the password page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ password: '' }, /** * Purpose: selectedShopIds supports index.js by holding or executing the selected shop ids page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ selectedShopIds: Array.isArray(draft.selectedShopIds) ? draft.selectedShopIds : this.data.selectedShopIds }) } this.syncTypeLabel(Number(this.data.form.type || 0)) this.loadShops() }, /** * Purpose: onUnload supports index.js by holding or executing the on unload page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ onUnload() { clearTempRecord('currentUser') clearDraftTimer(this) }, /** * Purpose: scheduleUserDraft supports index.js by holding or executing the schedule user draft page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ scheduleUserDraft() { scheduleDraftSave(this, DRAFT_KEY, () => ({ /** * Purpose: mode supports index.js by holding or executing the mode page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ mode: this.data.mode, /** * Purpose: form supports index.js by holding or executing the form page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ form: { /** * Purpose: nickname supports index.js by holding or executing the nickname page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ nickname: this.data.form.nickname, /** * Purpose: account supports index.js by holding or executing the account page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ account: this.data.form.account, /** * Purpose: phone supports index.js by holding or executing the phone page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ phone: this.data.form.phone, /** * Purpose: signature supports index.js by holding or executing the signature page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ signature: this.data.form.signature, /** * Purpose: type supports index.js by holding or executing the type page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ type: Number(this.data.form.type) }, /** * Purpose: selectedShopIds supports index.js by holding or executing the selected shop ids page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ 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({ /** * Purpose: shops supports index.js by holding or executing the shops page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ shops: (Array.isArray(shops) ? shops : []).filter(item => normalizeId(item.id) === normalizeId(currentShop.id)).map(item => ({ ...item, /** * Purpose: valueId supports index.js by holding or executing the value id page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ valueId: normalizeId(item.id), /** * Purpose: checked supports index.js by holding or executing the checked page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ checked: true })), /** * Purpose: selectedShopIds supports index.js by holding or executing the selected shop ids page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ selectedShopIds: [normalizeId(currentShop.id)] }) this.scheduleUserDraft() return } this.setData({ /** * Purpose: shops supports index.js by holding or executing the shops page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ shops: (Array.isArray(shops) ? shops : []).map(item => ({ ...item, /** * Purpose: valueId supports index.js by holding or executing the value id page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ valueId: normalizeId(item.id), /** * Purpose: checked supports index.js by holding or executing the checked page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ checked: this.data.selectedShopIds.includes(normalizeId(item.id)) })) }) } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '店铺列表加载失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } }, /** * Purpose: handleInputChange supports index.js by holding or executing the handle input change page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ 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() } }, /** * Purpose: handleTypeChange supports index.js by holding or executing the handle type change page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handleTypeChange(event) { const index = Number(event.detail.value) const item = this.data.typeOptions[index] this.setData({ 'form.type': item ? item.value : 0, /** * Purpose: currentTypeLabel supports index.js by holding or executing the current type label page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentTypeLabel: item ? item.label : '' }) this.scheduleUserDraft() }, /** * Purpose: handleShopCheck supports index.js by holding or executing the handle shop check page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handleShopCheck(event) { const selectedShopIds = event.detail.value || [] this.setData({ selectedShopIds, /** * Purpose: shops supports index.js by holding or executing the shops page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ shops: this.data.shops.map(item => ({ ...item, /** * Purpose: checked supports index.js by holding or executing the checked page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ checked: selectedShopIds.includes(String(item.id)) })) }) this.scheduleUserDraft() }, /** * Purpose: validateForm supports index.js by holding or executing the validate form page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ 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({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: errorMessage, /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) return } const payload = { /** * Purpose: nickname supports index.js by holding or executing the nickname page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ nickname: this.data.form.nickname, /** * Purpose: account supports index.js by holding or executing the account page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ account: this.data.form.account, /** * Purpose: phone supports index.js by holding or executing the phone page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ phone: this.data.form.phone, /** * Purpose: signature supports index.js by holding or executing the signature page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ signature: this.data.form.signature || undefined, /** * Purpose: type supports index.js by holding or executing the type page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ type: Number(this.data.form.type), /** * Purpose: shopIds supports index.js by holding or executing the shop ids page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ 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({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: this.data.mode === 'edit' ? '用户已更新' : '用户已创建', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) navigation.goBack('/pages/users/index') } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '保存失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } }, /** * Purpose: syncTypeLabel supports index.js by holding or executing the sync type label page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ syncTypeLabel(type) { const target = this.data.typeOptions.find(item => Number(item.value) === Number(type)) this.setData({ /** * Purpose: currentTypeLabel supports index.js by holding or executing the current type label page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentTypeLabel: target ? target.label : '' }) } })