实现预约草稿和资料头像编辑
This commit is contained in:
@@ -3,6 +3,9 @@ const productsApi = require('../../api/products')
|
||||
const customersApi = require('../../api/customers')
|
||||
const dateUtil = require('../../utils/date')
|
||||
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
||||
const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload')
|
||||
|
||||
const DRAFT_STORAGE_PREFIX = 'managerAppointmentDraft'
|
||||
|
||||
function buildClientRequestId(prefix) {
|
||||
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
||||
@@ -22,6 +25,49 @@ function canCreateAppointment() {
|
||||
return Number(userInfo.type) !== 3
|
||||
}
|
||||
|
||||
function buildDraftKey() {
|
||||
const app = getApp()
|
||||
const userInfo = app.globalData.userInfo || {}
|
||||
return `${DRAFT_STORAGE_PREFIX}:${userInfo.id || 'guest'}:${userInfo.shopId || 'default'}`
|
||||
}
|
||||
|
||||
function hasPresetOptions(options) {
|
||||
return Boolean(options.customerName || options.phone || options.date || options.artistId)
|
||||
}
|
||||
|
||||
function readDraftForm() {
|
||||
try {
|
||||
const draft = wx.getStorageSync(buildDraftKey())
|
||||
return draft && draft.form ? draft.form : null
|
||||
} catch (error) {
|
||||
throw new Error(`读取预约草稿失败:${error.message || error}`)
|
||||
}
|
||||
}
|
||||
|
||||
function writeDraftForm(form) {
|
||||
try {
|
||||
wx.setStorageSync(buildDraftKey(), {
|
||||
form,
|
||||
savedAt: Date.now()
|
||||
})
|
||||
} catch (error) {
|
||||
throw new Error(`保存预约草稿失败:${error.message || error}`)
|
||||
}
|
||||
}
|
||||
|
||||
function removeDraftForm() {
|
||||
try {
|
||||
wx.removeStorageSync(buildDraftKey())
|
||||
} catch (error) {
|
||||
throw new Error(`清空预约草稿失败:${error.message || error}`)
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeDuration(value) {
|
||||
const duration = Number(value)
|
||||
return Number.isFinite(duration) && duration > 0 ? Math.floor(duration) : 0
|
||||
}
|
||||
|
||||
function parseProductTags(product) {
|
||||
if (Array.isArray(product.tagList) && product.tagList.length > 0) {
|
||||
return product.tagList
|
||||
@@ -90,6 +136,12 @@ Page({
|
||||
productId: '',
|
||||
manualProductTitle: '',
|
||||
manualProductPrice: '',
|
||||
manualProductDescription: '',
|
||||
manualProductTags: '',
|
||||
manualProductTargetAudience: '',
|
||||
manualProductPrecautions: '',
|
||||
manualProductImageFileIds: '',
|
||||
manualProductImageUrl: '',
|
||||
durationMinutes: '',
|
||||
artistId: '',
|
||||
remark: ''
|
||||
@@ -109,17 +161,26 @@ Page({
|
||||
conflictTip: '',
|
||||
conflictLevel: 'free',
|
||||
canCreateAppointment: true,
|
||||
draftSavedAtText: '',
|
||||
manualImageUploading: false,
|
||||
loading: false
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
const creatable = canCreateAppointment()
|
||||
const draftForm = creatable && !hasPresetOptions(options) ? readDraftForm() : null
|
||||
const baseForm = draftForm || {}
|
||||
this.setData({
|
||||
canCreateAppointment: creatable,
|
||||
'form.customerName': options.customerName ? decodeURIComponent(options.customerName) : '',
|
||||
'form.phone': options.phone ? decodeURIComponent(options.phone) : '',
|
||||
'form.appointmentDate': options.date || dateUtil.formatDate(new Date()),
|
||||
'form.artistId': options.artistId || ''
|
||||
form: {
|
||||
...this.data.form,
|
||||
...baseForm,
|
||||
customerName: options.customerName ? decodeURIComponent(options.customerName) : (baseForm.customerName || ''),
|
||||
phone: options.phone ? decodeURIComponent(options.phone) : (baseForm.phone || ''),
|
||||
appointmentDate: options.date || baseForm.appointmentDate || dateUtil.formatDate(new Date()),
|
||||
artistId: options.artistId || baseForm.artistId || ''
|
||||
},
|
||||
draftSavedAtText: draftForm ? '已恢复本机草稿' : ''
|
||||
})
|
||||
if (!creatable) {
|
||||
wx.showToast({ title: '美甲师账号只能查看预约', icon: 'none' })
|
||||
@@ -290,7 +351,13 @@ Page({
|
||||
'form.productId': selectedProduct.id,
|
||||
'form.manualProductTitle': '',
|
||||
'form.manualProductPrice': '',
|
||||
'form.durationMinutes': String(parseInt(selectedProduct.approximateDuration, 10) || this.data.form.durationMinutes || 60)
|
||||
'form.manualProductDescription': '',
|
||||
'form.manualProductTags': '',
|
||||
'form.manualProductTargetAudience': '',
|
||||
'form.manualProductPrecautions': '',
|
||||
'form.manualProductImageFileIds': '',
|
||||
'form.manualProductImageUrl': '',
|
||||
'form.durationMinutes': String(parseInt(selectedProduct.approximateDuration, 10) || this.data.form.durationMinutes || '')
|
||||
})
|
||||
this.closeProductModal()
|
||||
this.checkAvailability()
|
||||
@@ -313,6 +380,40 @@ Page({
|
||||
this.checkAvailability()
|
||||
},
|
||||
|
||||
async chooseManualProductImage() {
|
||||
if (this.data.manualImageUploading) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
const filePath = await chooseImageFilePath()
|
||||
if (!filePath) {
|
||||
return
|
||||
}
|
||||
this.setData({ manualImageUploading: true })
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const fileId = uploadResult.id || uploadResult.fileId
|
||||
const fileUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath
|
||||
if (!fileId) {
|
||||
throw new Error('作品图片上传成功但未返回文件ID')
|
||||
}
|
||||
this.setData({
|
||||
'form.manualProductImageFileIds': String(fileId),
|
||||
'form.manualProductImageUrl': fileUrl
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '图片上传失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ manualImageUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
removeManualProductImage() {
|
||||
this.setData({
|
||||
'form.manualProductImageFileIds': '',
|
||||
'form.manualProductImageUrl': ''
|
||||
})
|
||||
},
|
||||
|
||||
onArtistChange(event) {
|
||||
const selectedArtist = this.data.artists[Number(event.detail.value)] || {}
|
||||
this.setData({
|
||||
@@ -328,7 +429,11 @@ Page({
|
||||
this.setData({ conflictTip: '', conflictLevel: 'free' })
|
||||
return { ok: true }
|
||||
}
|
||||
const durationMinutes = Number(form.durationMinutes || 0) || 60
|
||||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||||
if (!durationMinutes) {
|
||||
this.setData({ conflictTip: '', conflictLevel: 'free' })
|
||||
return { ok: true }
|
||||
}
|
||||
const startMinute = dateUtil.minutesOfDay(form.appointmentTime)
|
||||
const endMinute = startMinute + durationMinutes
|
||||
if (startMinute < dateUtil.minutesOfDay('10:00') || endMinute > dateUtil.minutesOfDay('22:00')) {
|
||||
@@ -380,10 +485,15 @@ Page({
|
||||
}
|
||||
const form = this.data.form
|
||||
const hasManualProduct = String(form.manualProductTitle || '').trim() && String(form.manualProductPrice || '').trim()
|
||||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||||
if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.artistId) {
|
||||
wx.showToast({ title: '请补全预约信息', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!durationMinutes) {
|
||||
wx.showToast({ title: '请填写预约时长', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const availability = await this.checkAvailability()
|
||||
if (!availability.ok) {
|
||||
wx.showToast({ title: availability.tip || '该时段已被预约', icon: 'none' })
|
||||
@@ -395,7 +505,7 @@ Page({
|
||||
...form,
|
||||
customerUserId: form.customerUserId ? String(form.customerUserId) : undefined,
|
||||
artistId: String(form.artistId),
|
||||
durationMinutes: Number(form.durationMinutes || 0) || undefined,
|
||||
durationMinutes,
|
||||
clientRequestId: buildClientRequestId('manager-appointment')
|
||||
}
|
||||
if (form.productId) {
|
||||
@@ -406,17 +516,43 @@ Page({
|
||||
payload.productId = undefined
|
||||
payload.manualProductTitle = String(form.manualProductTitle || '').trim()
|
||||
payload.manualProductPrice = Number(form.manualProductPrice)
|
||||
payload.manualProductDescription = String(form.manualProductDescription || '').trim() || undefined
|
||||
payload.manualProductTags = String(form.manualProductTags || '').trim() || undefined
|
||||
payload.manualProductTargetAudience = String(form.manualProductTargetAudience || '').trim() || undefined
|
||||
payload.manualProductPrecautions = String(form.manualProductPrecautions || '').trim() || undefined
|
||||
payload.manualProductImageFileIds = String(form.manualProductImageFileIds || '').trim() || undefined
|
||||
}
|
||||
const result = await planningApi.createAppointment(payload)
|
||||
removeDraftForm()
|
||||
markPlanningRefresh()
|
||||
wx.redirectTo({ url: `/pages/appointment-detail/index?id=${result.orderItemId}` })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
|
||||
wx.showToast({ title: error.message || '提交失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
saveDraft() {
|
||||
try {
|
||||
writeDraftForm(this.data.form)
|
||||
this.setData({ draftSavedAtText: '草稿已保存到本机' })
|
||||
wx.showToast({ title: '草稿已保存', icon: 'success' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '草稿保存失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
clearDraft() {
|
||||
try {
|
||||
removeDraftForm()
|
||||
this.setData({ draftSavedAtText: '' })
|
||||
wx.showToast({ title: '草稿已清空', icon: 'success' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '草稿清空失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
back() {
|
||||
wx.navigateBack()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user