/** * 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: upload supports index.js by holding or executing the upload 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 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') /** * 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-shop-form' 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: shopId supports index.js by holding or executing the shop 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. */ shopId: '', /** * Purpose: uploading supports index.js by holding or executing the uploading 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. */ uploading: false, /** * Purpose: uploadProgress supports index.js by holding or executing the upload progress 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. */ uploadProgress: 0, /** * Purpose: statusOptions supports index.js by holding or executing the status 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. */ statusOptions: ['启用', '停用'], /** * Purpose: statusIndex supports index.js by holding or executing the status index 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. */ statusIndex: 0, /** * 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: shopName supports index.js by holding or executing the shop name 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. */ shopName: '', /** * Purpose: description supports index.js by holding or executing the description 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. */ description: '', /** * Purpose: address supports index.js by holding or executing the address 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. */ address: '', /** * Purpose: businessHours supports index.js by holding or executing the business hours 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. */ businessHours: '', /** * Purpose: coverImageFileId supports index.js by holding or executing the cover image file 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. */ coverImageFileId: '', /** * Purpose: imageFileIds supports index.js by holding or executing the image file 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. */ imageFileIds: [] } }, /** * 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 currentShop = getTempRecord('currentShopRecord', null) const draft = getDraftValue(DRAFT_KEY, null) this.setData({ mode, /** * Purpose: shopId supports index.js by holding or executing the shop 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. */ shopId: options && options.id ? options.id : '' }) if (mode === 'edit' && currentShop) { 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: { /** * Purpose: shopName supports index.js by holding or executing the shop name 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. */ shopName: currentShop.shopName || '', /** * Purpose: description supports index.js by holding or executing the description 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. */ description: currentShop.description || '', /** * Purpose: address supports index.js by holding or executing the address 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. */ address: currentShop.address || '', /** * Purpose: businessHours supports index.js by holding or executing the business hours 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. */ businessHours: currentShop.businessHours || '', /** * Purpose: coverImageFileId supports index.js by holding or executing the cover image file 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. */ coverImageFileId: currentShop.coverImageFileId || '', /** * Purpose: imageFileIds supports index.js by holding or executing the image file 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. */ imageFileIds: currentShop.imageFileIds ? String(currentShop.imageFileIds).split(',').filter(Boolean) : [] }, /** * Purpose: statusIndex supports index.js by holding or executing the status index 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. */ statusIndex: Number(currentShop.status) === 0 ? 1 : 0 }) } 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: statusIndex supports index.js by holding or executing the status index 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. */ statusIndex: draft.statusIndex === 1 ? 1 : this.data.statusIndex }) } }, /** * 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('currentShopRecord') clearDraftTimer(this) }, /** * Purpose: scheduleShopDraft supports index.js by holding or executing the schedule shop 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. */ scheduleShopDraft() { 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: statusIndex supports index.js by holding or executing the status index 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. */ statusIndex: this.data.statusIndex, /** * 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, /** * Purpose: imageFileIds supports index.js by holding or executing the image file 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. */ imageFileIds: this.data.form.imageFileIds.slice() } })) }, /** * 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 this.setData({ [`form.${field}`]: event.detail.value }) this.scheduleShopDraft() }, /** * Purpose: handleStatusChange supports index.js by holding or executing the handle status 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. */ handleStatusChange(event) { this.setData({ /** * Purpose: statusIndex supports index.js by holding or executing the status index 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. */ 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({ /** * Purpose: count supports index.js by holding or executing the count 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. */ count: isBanner ? 1 : 9 }) this.setData({ /** * Purpose: uploading supports index.js by holding or executing the uploading 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. */ uploading: true, /** * Purpose: uploadProgress supports index.js by holding or executing the upload progress 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. */ uploadProgress: 0 }) const results = await upload.uploadImageFiles({ files, scene, /** * Purpose: onProgress supports index.js by holding or executing the on progress 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. */ onProgress: progress => { this.setData({ /** * Purpose: uploadProgress supports index.js by holding or executing the upload progress 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. */ 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({ /** * 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' }) } finally { this.setData({ /** * Purpose: uploading supports index.js by holding or executing the uploading 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. */ uploading: false }) } }, async handleSubmit() { if (!this.data.form.shopName) { 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: '请输入店铺名称', /** * 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: shopName supports index.js by holding or executing the shop name 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. */ shopName: this.data.form.shopName, /** * Purpose: description supports index.js by holding or executing the description 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. */ description: this.data.form.description || undefined, /** * Purpose: address supports index.js by holding or executing the address 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. */ address: this.data.form.address || undefined, /** * Purpose: businessHours supports index.js by holding or executing the business hours 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. */ businessHours: this.data.form.businessHours || undefined, /** * Purpose: coverImageFileId supports index.js by holding or executing the cover image file 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. */ coverImageFileId: this.data.form.coverImageFileId || undefined, /** * Purpose: imageFileIds supports index.js by holding or executing the image file 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. */ imageFileIds: this.data.form.imageFileIds.join(',') || undefined, /** * Purpose: status supports index.js by holding or executing the status 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. */ 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({ /** * 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/shops/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' }) } } })