const planningApi = require('../../api/planning') const { markPlanningRefresh } = require('../../utils/planning-refresh') const { chooseImageFilePaths, uploadObjectFile, resolveUploadedImagePreviewUrl } = 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 = resolveUploadedImagePreviewUrl(uploadResult, filePath) if (!fileId) { throw new Error('预约图片上传成功但未返回文件ID') } fileIds.push(String(fileId)) imageUrls.push(fileUrl) } return { fileIds, imageUrls } } Page({ data: { id: '', appointment: {}, avatarText: '', timeText: '', productImageUrls: [], productImageFileIds: '', imageUploading: false, canTransferAppointment: false, loadError: '' }, onLoad(options) { this.setData({ id: options.id || '', canTransferAppointment: canTransferAppointment() }) this.loadDetail() }, async loadDetail() { try { const appointment = await planningApi.appointment(this.data.id) this.applyAppointment(appointment) } catch (error) { this.setData({ 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: '' }) }, async updateStatus(status) { try { await planningApi.updateStatus(this.data.appointment.orderId, status) markPlanningRefresh() wx.showToast({ title: '已更新' }) this.loadDetail() } catch (error) { wx.showToast({ title: error.message || '操作失败', icon: 'none' }) } }, confirm() { this.updateStatus(1) }, complete() { this.updateStatus(2) }, cancel() { this.updateStatus(3) }, 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, previewImageUrls) { const appointment = await planningApi.updateAppointmentImages(this.data.id, { imageFileIds: fileIds.join(',') }) markPlanningRefresh() this.applyAppointment(appointment) if (Array.isArray(previewImageUrls) && previewImageUrls.length > 0) { this.setData({ productImageUrls: previewImageUrls }) } }, 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), this.data.productImageUrls.concat(uploaded.imageUrls) ) 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, uploaded.imageUrls) 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 currentImageUrls = Array.isArray(this.data.productImageUrls) ? this.data.productImageUrls : [] const nextFileIds = currentFileIds.filter((item, itemIndex) => itemIndex !== index) const nextImageUrls = currentImageUrls.filter((item, itemIndex) => itemIndex !== index) this.setData({ imageUploading: true }) try { await this.persistAppointmentImages(nextFileIds, nextImageUrls) 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() } })