优化店务预约作品快照与图片维护
This commit is contained in:
@@ -1,5 +1,44 @@
|
||||
const planningApi = require('../../api/planning')
|
||||
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
||||
const { chooseImageFilePaths, uploadObjectFile } = require('../../utils/upload')
|
||||
|
||||
function canTransferAppointment() {
|
||||
const app = getApp()
|
||||
const userInfo = app.globalData.userInfo || {}
|
||||
return Number(userInfo.type) === 2
|
||||
}
|
||||
|
||||
function splitImageValues(value) {
|
||||
return String(value || '')
|
||||
.split(',')
|
||||
.map(item => item.trim())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
function resolveProductImageUrls(appointment) {
|
||||
if (appointment && Array.isArray(appointment.productImageUrls)) {
|
||||
return appointment.productImageUrls.filter(Boolean)
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
async function uploadAppointmentImageFiles(limit) {
|
||||
const filePaths = await chooseImageFilePaths(limit)
|
||||
const fileIds = []
|
||||
const imageUrls = []
|
||||
for (let index = 0; index < filePaths.length; index += 1) {
|
||||
const filePath = filePaths[index]
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const fileId = uploadResult.id || uploadResult.fileId
|
||||
const fileUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath
|
||||
if (!fileId) {
|
||||
throw new Error('预约图片上传成功但未返回文件ID')
|
||||
}
|
||||
fileIds.push(String(fileId))
|
||||
imageUrls.push(fileUrl)
|
||||
}
|
||||
return { fileIds, imageUrls }
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -7,11 +46,18 @@ Page({
|
||||
appointment: {},
|
||||
avatarText: '',
|
||||
timeText: '',
|
||||
productImageUrls: [],
|
||||
productImageFileIds: '',
|
||||
imageUploading: false,
|
||||
canTransferAppointment: false,
|
||||
loadError: ''
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
this.setData({ id: options.id || '' })
|
||||
this.setData({
|
||||
id: options.id || '',
|
||||
canTransferAppointment: canTransferAppointment()
|
||||
})
|
||||
this.loadDetail()
|
||||
},
|
||||
|
||||
@@ -24,16 +70,22 @@ Page({
|
||||
appointment: {},
|
||||
avatarText: '',
|
||||
timeText: '',
|
||||
productImageUrls: [],
|
||||
productImageFileIds: '',
|
||||
loadError: error.message || '预约详情加载失败'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
applyAppointment(appointment) {
|
||||
const imageUrls = resolveProductImageUrls(appointment)
|
||||
this.setData({
|
||||
appointment,
|
||||
avatarText: String(appointment.customerName || '客').slice(0, 1),
|
||||
timeText: `${(appointment.startTime || '').slice(0, 16)} - ${(appointment.endTime || '').slice(11, 16)}`,
|
||||
productImageUrls: imageUrls,
|
||||
productImageFileIds: String(appointment.productImageFileIds || ''),
|
||||
canTransferAppointment: canTransferAppointment(),
|
||||
loadError: ''
|
||||
})
|
||||
},
|
||||
@@ -62,9 +114,97 @@ Page({
|
||||
},
|
||||
|
||||
goTransfer() {
|
||||
if (!this.data.canTransferAppointment) {
|
||||
wx.showToast({ title: '只有店长可以改派预约', icon: 'none' })
|
||||
return
|
||||
}
|
||||
wx.navigateTo({ url: `/pages/artist-transfer/index?id=${this.data.id}` })
|
||||
},
|
||||
|
||||
async persistAppointmentImages(fileIds) {
|
||||
const appointment = await planningApi.updateAppointmentImages(this.data.id, {
|
||||
imageFileIds: fileIds.join(',')
|
||||
})
|
||||
markPlanningRefresh()
|
||||
this.applyAppointment(appointment)
|
||||
},
|
||||
|
||||
async chooseAppointmentImages() {
|
||||
if (this.data.imageUploading) {
|
||||
return
|
||||
}
|
||||
const currentFileIds = splitImageValues(this.data.productImageFileIds)
|
||||
const remainingCount = 9 - currentFileIds.length
|
||||
if (remainingCount <= 0) {
|
||||
wx.showToast({ title: '最多上传 9 张作品图片', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ imageUploading: true })
|
||||
try {
|
||||
const uploaded = await uploadAppointmentImageFiles(remainingCount)
|
||||
if (uploaded.fileIds.length === 0) {
|
||||
return
|
||||
}
|
||||
await this.persistAppointmentImages(currentFileIds.concat(uploaded.fileIds))
|
||||
wx.showToast({ title: '图片已保存' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片上传失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ imageUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
async replaceAppointmentImages() {
|
||||
if (this.data.imageUploading) {
|
||||
return
|
||||
}
|
||||
this.setData({ imageUploading: true })
|
||||
try {
|
||||
const uploaded = await uploadAppointmentImageFiles(9)
|
||||
if (uploaded.fileIds.length === 0) {
|
||||
return
|
||||
}
|
||||
await this.persistAppointmentImages(uploaded.fileIds)
|
||||
wx.showToast({ title: '图片已保存' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片上传失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ imageUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
async removeAppointmentImage(event) {
|
||||
if (this.data.imageUploading) {
|
||||
return
|
||||
}
|
||||
const index = Number(event.currentTarget.dataset.index)
|
||||
const currentFileIds = splitImageValues(this.data.productImageFileIds)
|
||||
if (!Number.isInteger(index) || index < 0 || index >= currentFileIds.length) {
|
||||
return
|
||||
}
|
||||
const nextFileIds = currentFileIds.filter((item, itemIndex) => itemIndex !== index)
|
||||
this.setData({ imageUploading: true })
|
||||
try {
|
||||
await this.persistAppointmentImages(nextFileIds)
|
||||
wx.showToast({ title: '图片已保存' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片保存失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ imageUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
previewProductImage(event) {
|
||||
const current = this.data.productImageUrls[Number(event.currentTarget.dataset.index)]
|
||||
if (!current) {
|
||||
return
|
||||
}
|
||||
wx.previewImage({
|
||||
current,
|
||||
urls: this.data.productImageUrls
|
||||
})
|
||||
},
|
||||
|
||||
back() {
|
||||
wx.navigateBack()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user