Files
LinHelp/miniapp/miniprogram/pages/second-hand/create.js

82 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2026-07-07 16:38:13 +08:00
const { createSecondGoods } = require('../../api/secondGoodsApi')
2026-07-14 16:04:51 +08:00
const { setField, uploadFile } = require('../../utils/request')
2026-07-07 16:38:13 +08:00
Page({
data: {
submitting: false,
2026-07-14 16:04:51 +08:00
uploading: false,
2026-07-07 16:38:13 +08:00
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 })
},
2026-07-14 16:04:51 +08:00
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))
}
2026-07-07 16:38:13 +08:00
})
},
2026-07-14 16:04:51 +08:00
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 })
},
2026-07-07 16:38:13 +08:00
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 }))
}
})