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