const { createSecondGoods } = require('../../api/secondGoodsApi') const { setField, uploadFile } = require('../../utils/request') Page({ data: { submitting: false, uploading: false, form: { title: '', priceCent: 0, description: '', category: '', contactPhone: '', tradeMethod: 'FACE_TO_FACE', imageUrls: [] } }, onFieldInput(event) { setField(this, event) }, onPriceInput(event) { this.setData({ 'form.priceCent': Math.round(Number(event.detail.value || 0) * 100) }) }, onTradeMethodChange(event) { this.setData({ 'form.tradeMethod': event.detail.value }) }, chooseImages() { if (this.data.uploading) { return } wx.chooseMedia({ count: Math.max(1, 6 - this.data.form.imageUrls.length), mediaType: ['image'], sourceType: ['album', 'camera'], success: (result) => { const files = result.tempFiles || [] this.uploadImages(files.map((item) => item.tempFilePath)) } }) }, uploadImages(filePaths) { if (!filePaths.length) { return } this.setData({ uploading: true }) Promise.all(filePaths.map((filePath) => uploadFile(filePath))) .then((results) => { const urls = results.map((item) => item.url).filter(Boolean) this.setData({ 'form.imageUrls': this.data.form.imageUrls.concat(urls).slice(0, 6) }) }) .catch((error) => wx.showToast({ title: error.message, icon: 'none' })) .finally(() => this.setData({ uploading: false })) }, removeImage(event) { const index = Number(event.currentTarget.dataset.index) const next = this.data.form.imageUrls.filter((item, itemIndex) => itemIndex !== index) this.setData({ 'form.imageUrls': next }) }, submit() { if (this.data.submitting) { return } this.setData({ submitting: true }) createSecondGoods(this.data.form) .then(() => { wx.showToast({ title: '待审核' }) wx.navigateBack() }) .catch((error) => wx.showToast({ title: error.message, icon: 'none' })) .finally(() => this.setData({ submitting: false })) } })