1736 lines
54 KiB
Vue
1736 lines
54 KiB
Vue
<template>
|
||
<MobileShell :title="pageTitle">
|
||
<div v-if="!isEditMode && draftSavedAtText" class="card draft-tip">
|
||
<span>{{ draftSavedAtText }}</span>
|
||
<button class="text-btn" type="button" @click="clearDraft">清空</button>
|
||
</div>
|
||
|
||
<section class="card form-card">
|
||
<div class="field customer-combobox">
|
||
<label class="required">客户</label>
|
||
<div class="customer-control">
|
||
<button v-if="selectedCustomerReady" class="customer-selected-row" type="button" @click="openCustomerDropdown">
|
||
<span class="customer-selected-name">
|
||
<span class="customer-avatar">
|
||
<img v-if="selectedCustomerDisplay.avatar" :src="selectedCustomerDisplay.avatar" alt="" />
|
||
<i v-else>{{ selectedCustomerDisplay.initial }}</i>
|
||
</span>
|
||
<b>{{ selectedCustomerDisplay.name }}</b>
|
||
</span>
|
||
<strong>{{ selectedCustomerDisplay.phone || '未填写手机号' }}</strong>
|
||
</button>
|
||
<input
|
||
v-else
|
||
v-model.trim="customerSearchText"
|
||
placeholder="搜索客户姓名或手机号"
|
||
@focus="openCustomerDropdown"
|
||
@input="onCustomerSearchInput"
|
||
@keydown.escape="customerDropdownOpen = false"
|
||
/>
|
||
<button v-if="customerSearchText || selectedCustomerReady" class="customer-clear" type="button" @click="clearCustomerSelection">×</button>
|
||
</div>
|
||
<div v-if="customerDropdownOpen" class="customer-dropdown">
|
||
<div v-if="customerSearching" class="customer-muted">正在搜索客户...</div>
|
||
<template v-else>
|
||
<div v-if="customerOptions.length > 0" class="customer-options-scroll">
|
||
<button v-for="item in customerOptionItems" :key="item.display.id" class="customer-option" type="button" @click="selectCustomer(item.customer)">
|
||
<span class="customer-option-main">
|
||
<span class="customer-avatar customer-avatar--small">
|
||
<img v-if="item.display.avatar" :src="item.display.avatar" alt="" />
|
||
<i v-else>{{ item.display.initial }}</i>
|
||
</span>
|
||
<strong>{{ item.display.name }}</strong>
|
||
</span>
|
||
<span class="customer-option-phone">{{ item.display.phone || '未填写手机号' }}</span>
|
||
</button>
|
||
</div>
|
||
<div v-else class="customer-muted">暂无匹配客户</div>
|
||
</template>
|
||
<div class="customer-new-entry">
|
||
<span>没有找到客户?</span>
|
||
<button class="secondary-btn" type="button" @click="openCreateCustomerModal">新增客户</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="field"><label class="required">预约日期</label><DatePickerField v-model="form.appointmentDate" placeholder="请选择日期" title="日期选择" @confirm="onDateChange" /></div>
|
||
<div class="field"><label class="required">预约作品</label><button class="field-value picker-value" type="button" @click="updateProductModalVisible(true)">{{ selectedProductTitle }}</button></div>
|
||
<div class="field">
|
||
<label class="required">预计时长</label>
|
||
<div class="duration-control">
|
||
<input v-model.trim="form.durationMinutes" inputmode="numeric" placeholder="请输入时长" @input="refreshAvailableTimes" />
|
||
<span>分钟</span>
|
||
</div>
|
||
</div>
|
||
<div class="field"><label class="required">指定员工</label><select v-model="form.artistId" @change="refreshAvailableTimes"><option value="">请选择员工</option><option v-for="artist in artists" :key="String(resolveArtistUserId(artist))" :value="String(resolveArtistUserId(artist))">{{ artist.name || artist.nickname }}</option></select></div>
|
||
<div class="field"><label class="required">预约时间</label><button class="field-value picker-value" type="button" @click="openTimeSheet">{{ form.appointmentTime || timeSelectTip }}</button></div>
|
||
<div class="field field--stack"><label>备注</label><textarea v-model.trim="form.remark" placeholder="请输入预约备注"></textarea></div>
|
||
</section>
|
||
|
||
<div v-if="conflictTip" class="conflict-tip" :class="`conflict-tip--${conflictLevel}`">{{ conflictTip }}</div>
|
||
|
||
<div class="fixed-bottom">
|
||
<div class="fixed-bottom__inner button-row">
|
||
<button v-if="!isEditMode" class="secondary-btn" type="button" @click="saveDraft">保存草稿</button>
|
||
<button class="primary-btn" type="button" :disabled="loading" @click="submit">{{ submitButtonText }}</button>
|
||
</div>
|
||
</div>
|
||
|
||
<BaseModal
|
||
:model-value="productModalVisible"
|
||
title="选择预约作品"
|
||
@update:model-value="updateProductModalVisible"
|
||
>
|
||
<div class="product-modal">
|
||
<div class="product-modal__actions">
|
||
<input v-model.trim="productKeyword" class="search-input" placeholder="搜索作品、标签、美甲师" @input="filterProducts" />
|
||
<button class="secondary-btn product-add-btn" type="button" @click="openCreateProductModal">新增</button>
|
||
</div>
|
||
<div class="list">
|
||
<button v-for="product in filteredProducts" :key="String(product.id)" class="product-card" type="button" @click="selectProduct(product)">
|
||
<div class="product-card__cover">
|
||
<img v-if="product.coverImage" :src="product.coverImage" alt="" />
|
||
<span v-else>作品</span>
|
||
</div>
|
||
<div class="product-card__body">
|
||
<strong>{{ product.title }}</strong>
|
||
<p v-if="product.descriptionText">{{ product.descriptionText }}</p>
|
||
<div v-if="product.tagList.length > 0" class="tag-row">
|
||
<i v-for="tag in product.tagList" :key="tag">{{ tag }}</i>
|
||
</div>
|
||
<div class="product-card__meta">
|
||
<span>{{ product.priceText }}</span>
|
||
<span>{{ product.durationText || '时长待定' }}</span>
|
||
</div>
|
||
<small>{{ product.artistText }}</small>
|
||
</div>
|
||
</button>
|
||
<EmptyState v-if="filteredProducts.length === 0" title="暂无匹配作品" description="可以调整关键词或新增作品" />
|
||
</div>
|
||
</div>
|
||
</BaseModal>
|
||
|
||
<BaseModal
|
||
:model-value="productCreateModalVisible"
|
||
title="新增作品"
|
||
:close-disabled="creatingProduct"
|
||
:has-unsaved-changes="productCreateChanged"
|
||
@update:model-value="updateProductCreateModalVisible"
|
||
>
|
||
<div class="product-create-form">
|
||
<div class="field product-create-row">
|
||
<label class="required">作品名称</label>
|
||
<div class="product-create-control">
|
||
<input v-model.trim="productCreateForm.title" class="search-input" placeholder="请输入作品名称" />
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row">
|
||
<label class="required">价格</label>
|
||
<div class="product-create-control">
|
||
<input v-model.trim="productCreateForm.price" class="search-input" inputmode="decimal" placeholder="请输入价格" />
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row">
|
||
<label class="required">时长</label>
|
||
<div class="product-create-control">
|
||
<input v-model.trim="productCreateForm.durationMinutes" class="search-input" inputmode="numeric" placeholder="请输入分钟数" />
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row product-create-row--top">
|
||
<label>作品说明</label>
|
||
<div class="product-create-control">
|
||
<div class="rich-editor">
|
||
<div class="rich-editor__toolbar" aria-label="作品说明格式">
|
||
<button class="rich-tool rich-tool--bold" type="button" aria-label="加粗" @mousedown.prevent @click="runDescriptionCommand('bold')">B</button>
|
||
<button class="rich-tool rich-tool--italic" type="button" aria-label="斜体" @mousedown.prevent @click="runDescriptionCommand('italic')">I</button>
|
||
<button class="rich-tool rich-tool--underline" type="button" aria-label="下划线" @mousedown.prevent @click="runDescriptionCommand('underline')">U</button>
|
||
<button class="rich-tool" type="button" aria-label="列表" @mousedown.prevent @click="runDescriptionCommand('insertUnorderedList')">•</button>
|
||
</div>
|
||
<div
|
||
ref="descriptionEditor"
|
||
class="rich-editor__content"
|
||
contenteditable="true"
|
||
role="textbox"
|
||
aria-label="作品说明"
|
||
data-placeholder="请输入作品说明"
|
||
@input="syncDescriptionFromEditor"
|
||
@blur="syncDescriptionFromEditor"
|
||
></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row product-create-row--top">
|
||
<label>适合人群</label>
|
||
<div class="product-create-control">
|
||
<textarea v-model.trim="productCreateForm.targetAudience" class="search-input textarea" placeholder="请输入适合人群"></textarea>
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row product-create-row--top">
|
||
<label>注意事项</label>
|
||
<div class="product-create-control">
|
||
<textarea v-model.trim="productCreateForm.precautions" class="search-input textarea" placeholder="请输入注意事项"></textarea>
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row product-create-row--top">
|
||
<label>作品首图</label>
|
||
<div class="product-create-control">
|
||
<div class="manual-image-row">
|
||
<img v-if="productCreateForm.imageUrl" :src="productCreateForm.imageUrl" alt="" />
|
||
<div v-else class="manual-image-placeholder">图</div>
|
||
<div class="manual-image-actions">
|
||
<label class="secondary-btn image-upload">
|
||
{{ productImageUploading ? '上传中...' : '上传图片' }}
|
||
<input type="file" accept="image/*" :disabled="productImageUploading" @change="uploadProductCreateImage" />
|
||
</label>
|
||
<button v-if="productCreateForm.imageUrl" class="text-btn" type="button" @click="removeProductCreateImage">移除</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row product-create-row--top">
|
||
<label>标签</label>
|
||
<div class="product-create-control">
|
||
<div v-if="tagOptions.length > 0" class="choice-grid">
|
||
<label v-for="tag in tagOptions" :key="String(tag.id)" class="choice-chip">
|
||
<input v-model="productCreateForm.tagIds" type="checkbox" :value="String(tag.id)" />
|
||
<span>{{ tag.displayName || tag.tagName }}</span>
|
||
</label>
|
||
</div>
|
||
<div v-else class="choice-empty">暂无同步标签</div>
|
||
</div>
|
||
</div>
|
||
<div class="field product-create-row product-create-row--top">
|
||
<label>可预约员工</label>
|
||
<div class="product-create-control">
|
||
<div v-if="artists.length > 0" class="choice-grid">
|
||
<label v-for="artist in artists" :key="String(resolveArtistUserId(artist))" class="choice-chip">
|
||
<input v-model="productCreateForm.bookableArtistIds" type="checkbox" :value="String(resolveArtistUserId(artist))" />
|
||
<span>{{ artist.name || artist.nickname }}</span>
|
||
</label>
|
||
</div>
|
||
<div v-else class="choice-empty">暂无可预约员工</div>
|
||
</div>
|
||
</div>
|
||
<div class="product-create-actions">
|
||
<button class="primary-btn" type="button" :disabled="creatingProduct || productImageUploading" @click="createProduct">
|
||
{{ creatingProduct ? '保存中...' : '保存并选择' }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</BaseModal>
|
||
|
||
<BaseModal
|
||
:model-value="timeSheetVisible"
|
||
title="选择可约时间"
|
||
@update:model-value="updateTimeSheetVisible"
|
||
>
|
||
<div class="time-sheet">
|
||
<LoadingState v-if="timeLoading" text="正在计算可约时间" />
|
||
<EmptyState v-else-if="availableTimeGroups.length === 0" title="暂无可约时间" :description="timeSelectTip" />
|
||
<section v-for="group in availableTimeGroups" v-else :key="group.key">
|
||
<h3>{{ group.title }}</h3>
|
||
<div class="time-grid">
|
||
<button v-for="time in group.times" :key="time.value" type="button" @click="selectAvailableTime(time.value)">
|
||
<strong>{{ time.value }}</strong>
|
||
<span>{{ time.rangeText }}</span>
|
||
</button>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</BaseModal>
|
||
|
||
<BaseModal
|
||
:model-value="createCustomerModalVisible"
|
||
title="新增客户"
|
||
:close-disabled="creatingCustomer"
|
||
:has-unsaved-changes="createCustomerChanged"
|
||
@update:model-value="updateCreateCustomerModalVisible"
|
||
>
|
||
<div class="customer-create-form">
|
||
<div class="field">
|
||
<label class="required">客户姓名</label>
|
||
<input v-model.trim="createCustomerForm.nickname" placeholder="请输入客户姓名" />
|
||
</div>
|
||
<div class="field">
|
||
<label class="required">手机号</label>
|
||
<input v-model.trim="createCustomerForm.phone" inputmode="numeric" placeholder="请输入手机号" />
|
||
</div>
|
||
<button class="primary-btn" type="button" :disabled="creatingCustomer" @click="createCustomer">
|
||
{{ creatingCustomer ? '新增中...' : '保存并选择' }}
|
||
</button>
|
||
</div>
|
||
</BaseModal>
|
||
</MobileShell>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, nextTick, onMounted, reactive, ref, watch } from 'vue'
|
||
import { useRoute, useRouter } from 'vue-router'
|
||
import BaseModal from '@/components/BaseModal.vue'
|
||
import DatePickerField from '@/components/DatePickerField.vue'
|
||
import EmptyState from '@/components/EmptyState.vue'
|
||
import LoadingState from '@/components/LoadingState.vue'
|
||
import MobileShell from '@/components/MobileShell.vue'
|
||
import { managerCustomersApi } from '@/api/managerCustomers'
|
||
import { managerEmployeesApi } from '@/api/managerEmployees'
|
||
import { managerPlanningApi } from '@/api/managerPlanning'
|
||
import { managerProductsApi, type ProductCreateRequest } from '@/api/managerProducts'
|
||
import { managerTagsApi, type ManagerTag } from '@/api/managerTags'
|
||
import { fileApi } from '@/api/file'
|
||
import type { Appointment, AppointmentForm, Artist, PlanningCalendar, Product } from '@/types/manager'
|
||
import type { UserInfo } from '@/types/common'
|
||
import { useSessionStore } from '@/stores/session'
|
||
import { useToastStore } from '@/stores/toast'
|
||
import { formatDate } from '@/utils/date'
|
||
import { buildAvailableTimeGroups, resolveAppointmentCalendarEndDate, type AvailableTimeGroup } from '@/utils/available-times'
|
||
import {
|
||
buildCustomerCreateDraft,
|
||
buildCustomerSearchText,
|
||
resolveCustomerDisplay,
|
||
validateAppointmentCustomerSelection,
|
||
type CustomerDisplay
|
||
} from '@/utils/customer-selection'
|
||
import { buildClientRequestId, normalizeDuration } from '@/utils/planning'
|
||
import { findArtistByUserId, normalizeArtistSelectionId, resolveArtistUserId } from '@/utils/artist'
|
||
import { readDraftForm, removeDraftForm, writeDraftForm } from '@/utils/appointment-draft'
|
||
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||
import { markPlanningRefresh } from '@/utils/storage'
|
||
|
||
interface ProductCard extends Product {
|
||
coverImage: string
|
||
descriptionText: string
|
||
priceText: string
|
||
durationText: string
|
||
artistText: string
|
||
tagList: string[]
|
||
}
|
||
|
||
interface AppointmentModalState {
|
||
productModalVisible: boolean
|
||
timeSheetVisible: boolean
|
||
}
|
||
|
||
interface CustomerCreateForm {
|
||
nickname: string
|
||
phone: string
|
||
}
|
||
|
||
interface ProductCreateForm {
|
||
title: string
|
||
price: string
|
||
durationMinutes: string
|
||
description: string
|
||
targetAudience: string
|
||
precautions: string
|
||
imageFileIds: string
|
||
imageUrl: string
|
||
tagIds: string[]
|
||
bookableArtistIds: string[]
|
||
}
|
||
|
||
const APPOINTMENT_MODAL_STATE_DRAFT_NAME = 'appointment-modal-state'
|
||
const CUSTOMER_CREATE_DRAFT_NAME = 'appointment-customer-create'
|
||
const PRODUCT_CREATE_DRAFT_NAME = 'appointment-product-create'
|
||
const CUSTOMER_PHONE_PATTERN = /^1[3-9]\d{9}$/
|
||
|
||
const normalizeStringArray = (value: string[] | undefined): string[] => {
|
||
if (!Array.isArray(value)) {
|
||
return []
|
||
}
|
||
return value.map(item => String(item || '').trim()).filter(Boolean)
|
||
}
|
||
|
||
const isSameStringArray = (left: string[], right: string[]): boolean => {
|
||
return left.length === right.length && left.every((item, index) => item === right[index])
|
||
}
|
||
|
||
const buildEmptyProductCreateForm = (): ProductCreateForm => ({
|
||
title: '',
|
||
price: '',
|
||
durationMinutes: '',
|
||
description: '',
|
||
targetAudience: '',
|
||
precautions: '',
|
||
imageFileIds: '',
|
||
imageUrl: '',
|
||
tagIds: [],
|
||
bookableArtistIds: []
|
||
})
|
||
|
||
const normalizeProductCreateForm = (value: ProductCreateForm | undefined): ProductCreateForm => {
|
||
if (!value) {
|
||
return buildEmptyProductCreateForm()
|
||
}
|
||
return {
|
||
title: value.title || '',
|
||
price: value.price || '',
|
||
durationMinutes: value.durationMinutes || '',
|
||
description: value.description || '',
|
||
targetAudience: value.targetAudience || '',
|
||
precautions: value.precautions || '',
|
||
imageFileIds: value.imageFileIds || '',
|
||
imageUrl: value.imageUrl || '',
|
||
tagIds: normalizeStringArray(value.tagIds),
|
||
bookableArtistIds: normalizeStringArray(value.bookableArtistIds)
|
||
}
|
||
}
|
||
|
||
const route = useRoute()
|
||
const router = useRouter()
|
||
const session = useSessionStore()
|
||
const toast = useToastStore()
|
||
const initialEditMode = Boolean(route.params.id)
|
||
const baseForm = initialEditMode ? null : readDraftForm(session.userInfo)
|
||
const modalStateDraft = initialEditMode ? null : readFormModalDraft<AppointmentModalState>(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
const createCustomerDraft = initialEditMode ? null : readFormModalDraft<CustomerCreateForm>(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||
const createProductDraft = initialEditMode ? null : readFormModalDraft<ProductCreateForm>(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||
const form = reactive<AppointmentForm>({
|
||
customerUserId: String(route.query.customerUserId || baseForm?.customerUserId || ''),
|
||
customerName: String(route.query.customerName || baseForm?.customerName || ''),
|
||
phone: String(route.query.phone || baseForm?.phone || ''),
|
||
appointmentDate: String(route.query.date || baseForm?.appointmentDate || formatDate(new Date())),
|
||
appointmentTime: baseForm?.appointmentTime || '',
|
||
productId: baseForm?.productId || '',
|
||
manualProductTitle: baseForm?.manualProductTitle || '',
|
||
manualProductPrice: baseForm?.manualProductPrice || '',
|
||
manualProductDescription: baseForm?.manualProductDescription || '',
|
||
manualProductTags: baseForm?.manualProductTags || '',
|
||
manualProductTargetAudience: baseForm?.manualProductTargetAudience || '',
|
||
manualProductPrecautions: baseForm?.manualProductPrecautions || '',
|
||
manualProductImageFileIds: baseForm?.manualProductImageFileIds || '',
|
||
manualProductImageUrl: baseForm?.manualProductImageUrl || '',
|
||
durationMinutes: baseForm?.durationMinutes || '',
|
||
artistId: String(route.query.artistId || baseForm?.artistId || ''),
|
||
remark: baseForm?.remark || ''
|
||
})
|
||
const draftSavedAtText = ref(baseForm ? '已恢复本机草稿' : '')
|
||
const customerOptions = ref<UserInfo[]>([])
|
||
const customerTimer = ref<number | null>(null)
|
||
const customerSearchText = ref([form.customerName, form.phone].filter(Boolean).join(' '))
|
||
const customerDropdownOpen = ref(false)
|
||
const customerSearching = ref(false)
|
||
const selectedCustomer = ref<UserInfo | null>(null)
|
||
const createCustomerModalVisible = ref(Boolean(createCustomerDraft?.open))
|
||
const creatingCustomer = ref(false)
|
||
const createCustomerForm = reactive<CustomerCreateForm>({
|
||
nickname: createCustomerDraft?.form.nickname || '',
|
||
phone: createCustomerDraft?.form.phone || ''
|
||
})
|
||
const productCreateForm = reactive<ProductCreateForm>(normalizeProductCreateForm(createProductDraft?.form))
|
||
const productCreateBaseline = ref<ProductCreateForm>(buildEmptyProductCreateForm())
|
||
const products = ref<ProductCard[]>([])
|
||
const filteredProducts = ref<ProductCard[]>([])
|
||
const productKeyword = ref('')
|
||
const productModalVisible = ref(Boolean(modalStateDraft?.open && modalStateDraft.form.productModalVisible))
|
||
const productCreateModalVisible = ref(Boolean(createProductDraft?.open))
|
||
const artists = ref<Artist[]>([])
|
||
const tagOptions = ref<ManagerTag[]>([])
|
||
const selectedProduct = ref<ProductCard | null>(null)
|
||
const descriptionEditor = ref<HTMLElement | null>(null)
|
||
const timeSheetVisible = ref(Boolean(modalStateDraft?.open && modalStateDraft.form.timeSheetVisible))
|
||
const availableTimeGroups = ref<AvailableTimeGroup[]>([])
|
||
const timeLoading = ref(false)
|
||
const timeSelectTip = ref('请先选择美甲师并填写时长')
|
||
const conflictTip = ref('')
|
||
const conflictLevel = ref<'free' | 'danger'>('free')
|
||
const loading = ref(false)
|
||
const creatingProduct = ref(false)
|
||
const productImageUploading = ref(false)
|
||
const isEditMode = computed(() => Boolean(route.params.id))
|
||
const editAppointmentId = computed(() => String(route.params.id || ''))
|
||
const pageTitle = computed(() => (isEditMode.value ? '修改预约' : '新建预约'))
|
||
const submitButtonText = computed(() => {
|
||
if (loading.value) {
|
||
return isEditMode.value ? '保存中...' : '提交中...'
|
||
}
|
||
return isEditMode.value ? '保存修改' : '提交预约'
|
||
})
|
||
const selectedCustomerReady = computed(() => {
|
||
return Boolean(form.customerUserId)
|
||
})
|
||
const customerOptionItems = computed(() => {
|
||
return customerOptions.value.map(customer => ({ customer, display: resolveCustomerDisplay(customer) }))
|
||
})
|
||
const selectedCustomerDisplay = computed<CustomerDisplay>(() => {
|
||
if (selectedCustomer.value) {
|
||
return resolveCustomerDisplay(selectedCustomer.value)
|
||
}
|
||
const name = form.customerName || '未填写姓名'
|
||
const phone = form.phone || ''
|
||
return {
|
||
id: String(form.customerUserId || ''),
|
||
name,
|
||
phone,
|
||
avatar: '',
|
||
initial: name.slice(0, 1) || '客',
|
||
searchText: buildCustomerSearchText(name, phone)
|
||
}
|
||
})
|
||
const createCustomerChanged = computed(() => {
|
||
return Boolean(createCustomerForm.nickname || createCustomerForm.phone)
|
||
})
|
||
const productCreateChanged = computed(() => {
|
||
const baseline = productCreateBaseline.value
|
||
return productCreateForm.title !== baseline.title
|
||
|| productCreateForm.price !== baseline.price
|
||
|| productCreateForm.durationMinutes !== baseline.durationMinutes
|
||
|| productCreateForm.description !== baseline.description
|
||
|| productCreateForm.targetAudience !== baseline.targetAudience
|
||
|| productCreateForm.precautions !== baseline.precautions
|
||
|| productCreateForm.imageFileIds !== baseline.imageFileIds
|
||
|| productCreateForm.imageUrl !== baseline.imageUrl
|
||
|| !isSameStringArray(productCreateForm.tagIds, baseline.tagIds)
|
||
|| !isSameStringArray(productCreateForm.bookableArtistIds, baseline.bookableArtistIds)
|
||
})
|
||
const appointmentFormChanged = computed(() => {
|
||
return Boolean(
|
||
form.customerUserId
|
||
|| form.customerName
|
||
|| form.phone
|
||
|| form.appointmentTime
|
||
|| form.productId
|
||
|| form.manualProductTitle
|
||
|| form.manualProductPrice
|
||
|| form.manualProductDescription
|
||
|| form.manualProductTags
|
||
|| form.manualProductTargetAudience
|
||
|| form.manualProductPrecautions
|
||
|| form.manualProductImageFileIds
|
||
|| form.manualProductImageUrl
|
||
|| form.durationMinutes
|
||
|| form.artistId
|
||
|| form.remark
|
||
)
|
||
})
|
||
|
||
const syncAppointmentDraft = (): void => {
|
||
if (isEditMode.value) {
|
||
return
|
||
}
|
||
if (!appointmentFormChanged.value) {
|
||
return
|
||
}
|
||
writeDraftForm(session.userInfo, { ...form })
|
||
if (!draftSavedAtText.value) {
|
||
draftSavedAtText.value = '草稿已自动保存到本机'
|
||
}
|
||
}
|
||
|
||
const syncAppointmentModalState = (): void => {
|
||
if (isEditMode.value) {
|
||
return
|
||
}
|
||
if (!productModalVisible.value && !timeSheetVisible.value) {
|
||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
return
|
||
}
|
||
writeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME, {
|
||
productModalVisible: productModalVisible.value,
|
||
timeSheetVisible: timeSheetVisible.value
|
||
}, true)
|
||
}
|
||
|
||
const updateProductModalVisible = (value: boolean): void => {
|
||
productModalVisible.value = value
|
||
syncAppointmentModalState()
|
||
}
|
||
|
||
const updateTimeSheetVisible = (value: boolean): void => {
|
||
timeSheetVisible.value = value
|
||
syncAppointmentModalState()
|
||
}
|
||
|
||
const snapshotCreateCustomerForm = (): CustomerCreateForm => ({
|
||
nickname: createCustomerForm.nickname,
|
||
phone: createCustomerForm.phone
|
||
})
|
||
|
||
const applyCreateCustomerForm = (value: CustomerCreateForm): void => {
|
||
createCustomerForm.nickname = value.nickname || ''
|
||
createCustomerForm.phone = value.phone || ''
|
||
}
|
||
|
||
const resetCreateCustomerForm = (): void => {
|
||
applyCreateCustomerForm({ nickname: '', phone: '' })
|
||
}
|
||
|
||
const syncCreateCustomerDraft = (): void => {
|
||
if (isEditMode.value) {
|
||
return
|
||
}
|
||
if (!createCustomerModalVisible.value) {
|
||
removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||
return
|
||
}
|
||
if (!createCustomerChanged.value) {
|
||
return
|
||
}
|
||
writeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME, snapshotCreateCustomerForm(), true)
|
||
}
|
||
|
||
const updateCreateCustomerModalVisible = (value: boolean): void => {
|
||
createCustomerModalVisible.value = value
|
||
if (!value) {
|
||
if (!isEditMode.value) {
|
||
removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
|
||
}
|
||
resetCreateCustomerForm()
|
||
}
|
||
}
|
||
|
||
const snapshotProductCreateForm = (): ProductCreateForm => ({
|
||
title: productCreateForm.title,
|
||
price: productCreateForm.price,
|
||
durationMinutes: productCreateForm.durationMinutes,
|
||
description: productCreateForm.description,
|
||
targetAudience: productCreateForm.targetAudience,
|
||
precautions: productCreateForm.precautions,
|
||
imageFileIds: productCreateForm.imageFileIds,
|
||
imageUrl: productCreateForm.imageUrl,
|
||
tagIds: [...productCreateForm.tagIds],
|
||
bookableArtistIds: [...productCreateForm.bookableArtistIds]
|
||
})
|
||
|
||
const applyProductCreateForm = (value: ProductCreateForm): void => {
|
||
productCreateForm.title = value.title
|
||
productCreateForm.price = value.price
|
||
productCreateForm.durationMinutes = value.durationMinutes
|
||
productCreateForm.description = value.description
|
||
productCreateForm.targetAudience = value.targetAudience
|
||
productCreateForm.precautions = value.precautions
|
||
productCreateForm.imageFileIds = value.imageFileIds
|
||
productCreateForm.imageUrl = value.imageUrl
|
||
productCreateForm.tagIds = [...value.tagIds]
|
||
productCreateForm.bookableArtistIds = [...value.bookableArtistIds]
|
||
}
|
||
|
||
const normalizeRichText = (value: string): string => {
|
||
const content = value.trim()
|
||
const plainText = content
|
||
.replace(/<[^>]*>/g, ' ')
|
||
.replace(/ /g, ' ')
|
||
.replace(/\s+/g, ' ')
|
||
.trim()
|
||
return plainText ? content : ''
|
||
}
|
||
|
||
const toPlainText = (value: string | undefined): string => {
|
||
return normalizeRichText(value || '')
|
||
.replace(/<[^>]*>/g, ' ')
|
||
.replace(/ /g, ' ')
|
||
.replace(/\s+/g, ' ')
|
||
.trim()
|
||
}
|
||
|
||
const syncDescriptionEditor = (): void => {
|
||
const editor = descriptionEditor.value
|
||
if (!editor) {
|
||
return
|
||
}
|
||
editor.innerHTML = productCreateForm.description
|
||
}
|
||
|
||
const syncDescriptionFromEditor = (): void => {
|
||
productCreateForm.description = normalizeRichText(descriptionEditor.value?.innerHTML || '')
|
||
}
|
||
|
||
const runDescriptionCommand = (command: string): void => {
|
||
const editor = descriptionEditor.value
|
||
if (!editor) {
|
||
return
|
||
}
|
||
editor.focus()
|
||
document.execCommand(command)
|
||
syncDescriptionFromEditor()
|
||
}
|
||
|
||
const resetProductCreateForm = (): void => {
|
||
applyProductCreateForm(buildEmptyProductCreateForm())
|
||
productCreateBaseline.value = buildEmptyProductCreateForm()
|
||
}
|
||
|
||
const syncProductCreateDraft = (): void => {
|
||
if (isEditMode.value) {
|
||
return
|
||
}
|
||
if (!productCreateModalVisible.value) {
|
||
removeFormModalDraft(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||
return
|
||
}
|
||
if (!productCreateChanged.value) {
|
||
return
|
||
}
|
||
writeFormModalDraft(session.userInfo, PRODUCT_CREATE_DRAFT_NAME, snapshotProductCreateForm(), true)
|
||
}
|
||
|
||
const updateProductCreateModalVisible = (value: boolean): void => {
|
||
productCreateModalVisible.value = value
|
||
if (!value) {
|
||
if (!isEditMode.value) {
|
||
removeFormModalDraft(session.userInfo, PRODUCT_CREATE_DRAFT_NAME)
|
||
}
|
||
resetProductCreateForm()
|
||
}
|
||
}
|
||
|
||
const selectedProductTitle = computed(() => {
|
||
if (selectedProduct.value) {
|
||
return selectedProduct.value.title
|
||
}
|
||
if (form.manualProductTitle) {
|
||
return form.manualProductTitle
|
||
}
|
||
return '请选择作品'
|
||
})
|
||
|
||
const parseProductTags = (product: Product): string[] => {
|
||
if (Array.isArray(product.tagList) && product.tagList.length > 0) {
|
||
return product.tagList.map(tag => String(tag || '').trim()).filter(Boolean)
|
||
}
|
||
if (Array.isArray(product.tagInfos) && product.tagInfos.length > 0) {
|
||
return product.tagInfos.map(tag => String(tag.tagName || '').trim()).filter(Boolean)
|
||
}
|
||
return String(product.tags || '')
|
||
.split(/[;,,、]/)
|
||
.map(tag => tag.trim())
|
||
.filter(Boolean)
|
||
}
|
||
|
||
const resolveProductArtistText = (product: Product, artistOptions: Artist[]): string => {
|
||
const artistIds = String(product.bookableArtists || '')
|
||
.split(',')
|
||
.map(id => id.trim())
|
||
.filter(Boolean)
|
||
if (artistIds.length === 0) {
|
||
return '全部美甲师'
|
||
}
|
||
const artistNames = artistIds
|
||
.map(id => findArtistByUserId(artistOptions, id))
|
||
.filter((artist): artist is Artist => Boolean(artist))
|
||
.map(artist => artist.name || artist.nickname || '')
|
||
.filter(Boolean)
|
||
return artistNames.length > 0 ? artistNames.join('、') : '指定美甲师'
|
||
}
|
||
|
||
const normalizeProduct = (product: Product, artistOptions: Artist[]): ProductCard => {
|
||
const imageUrls = Array.isArray(product.imageUrls) ? product.imageUrls : []
|
||
const durationText = product.approximateDuration || (product.serviceDurationMinutes ? `${product.serviceDurationMinutes}分钟` : '')
|
||
return {
|
||
...product,
|
||
coverImage: imageUrls[0] || '',
|
||
descriptionText: toPlainText(product.description),
|
||
tagList: parseProductTags(product).slice(0, 4),
|
||
priceText: product.price || product.price === 0 ? `¥${product.price}` : '价格待定',
|
||
durationText,
|
||
artistText: resolveProductArtistText(product, artistOptions)
|
||
}
|
||
}
|
||
|
||
const filterProducts = (): void => {
|
||
const keyword = productKeyword.value.toLowerCase()
|
||
filteredProducts.value = products.value.filter(product => {
|
||
if (!keyword) {
|
||
return true
|
||
}
|
||
return [product.title, product.priceText, product.durationText, product.artistText, ...product.tagList]
|
||
.some(value => String(value || '').toLowerCase().includes(keyword))
|
||
})
|
||
}
|
||
|
||
const loadOptions = async (): Promise<void> => {
|
||
const [productData, employeeData, tagData] = await Promise.all([
|
||
managerProductsApi.list({ page: 1, size: 100 }),
|
||
managerEmployeesApi.list(),
|
||
managerTagsApi.listAll()
|
||
])
|
||
artists.value = employeeData
|
||
tagOptions.value = tagData
|
||
form.artistId = normalizeArtistSelectionId(form.artistId, artists.value)
|
||
const productList = Array.isArray(productData) ? productData : productData.list
|
||
products.value = productList.map(product => normalizeProduct(product, artists.value))
|
||
filteredProducts.value = products.value
|
||
if (form.productId) {
|
||
selectedProduct.value = products.value.find(item => String(item.id) === String(form.productId)) || null
|
||
}
|
||
}
|
||
|
||
const resolveAppointmentDateValue = (appointment: Appointment): string => {
|
||
return appointment.date || String(appointment.startTime || '').slice(0, 10) || formatDate(new Date())
|
||
}
|
||
|
||
const resolveAppointmentTimeValue = (appointment: Appointment): string => {
|
||
return String(appointment.startTime || '').slice(11, 16)
|
||
}
|
||
|
||
const applyAppointmentToForm = (appointment: Appointment): void => {
|
||
const customerId = appointment.customerId ?? appointment.customerUserId ?? ''
|
||
form.customerUserId = String(customerId || '')
|
||
form.customerName = appointment.customerName || ''
|
||
form.phone = appointment.phone || appointment.customerPhone || ''
|
||
form.appointmentDate = resolveAppointmentDateValue(appointment)
|
||
form.appointmentTime = resolveAppointmentTimeValue(appointment)
|
||
form.productId = String(appointment.productId || '')
|
||
form.manualProductTitle = appointment.productTitle || ''
|
||
form.manualProductPrice = ''
|
||
form.manualProductDescription = ''
|
||
form.manualProductTags = ''
|
||
form.manualProductTargetAudience = ''
|
||
form.manualProductPrecautions = ''
|
||
form.manualProductImageFileIds = ''
|
||
form.manualProductImageUrl = ''
|
||
form.durationMinutes = String(appointment.durationMinutes || '')
|
||
form.artistId = normalizeArtistSelectionId(String(appointment.artistId || ''), artists.value)
|
||
form.remark = appointment.remark || ''
|
||
selectedProduct.value = products.value.find(item => String(item.id) === String(form.productId)) || null
|
||
customerSearchText.value = [form.customerName, form.phone].filter(Boolean).join(' ')
|
||
conflictTip.value = ''
|
||
}
|
||
|
||
const loadAppointmentForEdit = async (): Promise<void> => {
|
||
const appointment = await managerPlanningApi.appointment(editAppointmentId.value)
|
||
if (appointment.canEdit === false || appointment.status === 2 || appointment.status === 3) {
|
||
toast.error('已完成或已取消的订单只可查看')
|
||
await router.replace(`/appointments/${editAppointmentId.value}`)
|
||
return
|
||
}
|
||
applyAppointmentToForm(appointment)
|
||
}
|
||
|
||
const withoutCurrentAppointment = (calendar: PlanningCalendar): PlanningCalendar => {
|
||
if (!isEditMode.value) {
|
||
return calendar
|
||
}
|
||
return {
|
||
...calendar,
|
||
appointments: (calendar.appointments || []).filter(item => String(item.orderItemId) !== editAppointmentId.value)
|
||
}
|
||
}
|
||
|
||
const scheduleCustomerSearch = (): void => {
|
||
if (customerTimer.value !== null) {
|
||
window.clearTimeout(customerTimer.value)
|
||
}
|
||
customerTimer.value = window.setTimeout(() => {
|
||
void searchCustomers()
|
||
}, 250)
|
||
}
|
||
|
||
const searchCustomers = async (): Promise<void> => {
|
||
const keyword = customerSearchText.value.trim()
|
||
customerSearching.value = true
|
||
try {
|
||
const data = await managerCustomersApi.list({ page: 1, size: 20, keyword })
|
||
customerOptions.value = data.list || []
|
||
} finally {
|
||
customerSearching.value = false
|
||
}
|
||
}
|
||
|
||
const openCustomerDropdown = (): void => {
|
||
customerDropdownOpen.value = true
|
||
if (customerOptions.value.length === 0) {
|
||
scheduleCustomerSearch()
|
||
}
|
||
}
|
||
|
||
const onCustomerSearchInput = (): void => {
|
||
form.customerUserId = ''
|
||
form.customerName = ''
|
||
form.phone = ''
|
||
selectedCustomer.value = null
|
||
customerDropdownOpen.value = true
|
||
scheduleCustomerSearch()
|
||
}
|
||
|
||
const clearCustomerSelection = (): void => {
|
||
form.customerUserId = ''
|
||
form.customerName = ''
|
||
form.phone = ''
|
||
customerSearchText.value = ''
|
||
selectedCustomer.value = null
|
||
customerOptions.value = []
|
||
customerDropdownOpen.value = true
|
||
}
|
||
|
||
const selectCustomer = (customer: UserInfo): void => {
|
||
const display = resolveCustomerDisplay(customer)
|
||
selectedCustomer.value = customer
|
||
form.customerUserId = display.id
|
||
form.customerName = display.name
|
||
form.phone = display.phone
|
||
customerSearchText.value = display.searchText
|
||
customerOptions.value = []
|
||
customerDropdownOpen.value = false
|
||
}
|
||
|
||
const openCreateCustomerModal = (): void => {
|
||
applyCreateCustomerForm(buildCustomerCreateDraft(customerSearchText.value))
|
||
customerDropdownOpen.value = false
|
||
createCustomerModalVisible.value = true
|
||
syncCreateCustomerDraft()
|
||
}
|
||
|
||
const createCustomer = async (): Promise<void> => {
|
||
const nickname = createCustomerForm.nickname.trim()
|
||
const phone = createCustomerForm.phone.trim()
|
||
if (!nickname || !phone) {
|
||
toast.error('请填写客户姓名和手机号')
|
||
return
|
||
}
|
||
if (!CUSTOMER_PHONE_PATTERN.test(phone)) {
|
||
toast.error('手机号格式不正确')
|
||
return
|
||
}
|
||
creatingCustomer.value = true
|
||
try {
|
||
const customerId = await managerCustomersApi.create({ nickname, phone })
|
||
const customer = await managerCustomersApi.detail(customerId)
|
||
toast.success('客户已新增')
|
||
updateCreateCustomerModalVisible(false)
|
||
selectCustomer(customer)
|
||
} finally {
|
||
creatingCustomer.value = false
|
||
}
|
||
}
|
||
|
||
const onDateChange = async (): Promise<void> => {
|
||
form.appointmentTime = ''
|
||
await refreshAvailableTimes()
|
||
}
|
||
|
||
const selectProduct = (product: ProductCard): void => {
|
||
selectedProduct.value = product
|
||
form.productId = String(product.id)
|
||
form.manualProductTitle = ''
|
||
form.manualProductPrice = ''
|
||
form.manualProductDescription = ''
|
||
form.manualProductTags = ''
|
||
form.manualProductTargetAudience = ''
|
||
form.manualProductPrecautions = ''
|
||
form.manualProductImageFileIds = ''
|
||
form.manualProductImageUrl = ''
|
||
form.durationMinutes = String(Number.parseInt(product.approximateDuration || '', 10) || product.serviceDurationMinutes || form.durationMinutes)
|
||
updateProductModalVisible(false)
|
||
void refreshAvailableTimes()
|
||
}
|
||
|
||
const openCreateProductModal = (): void => {
|
||
updateProductModalVisible(false)
|
||
if (!productCreateChanged.value && form.artistId) {
|
||
productCreateForm.bookableArtistIds = [form.artistId]
|
||
}
|
||
productCreateBaseline.value = snapshotProductCreateForm()
|
||
productCreateModalVisible.value = true
|
||
void nextTick(syncDescriptionEditor)
|
||
syncProductCreateDraft()
|
||
}
|
||
|
||
const uploadProductCreateImage = async (event: Event): Promise<void> => {
|
||
const input = event.target as HTMLInputElement
|
||
const file = input.files?.[0]
|
||
if (!file) {
|
||
return
|
||
}
|
||
productImageUploading.value = true
|
||
try {
|
||
const uploaded = await fileApi.uploadObjectFile(file)
|
||
productCreateForm.imageFileIds = String(uploaded.id)
|
||
productCreateForm.imageUrl = uploaded.primaryUrl || uploaded.fileUrl || ''
|
||
} finally {
|
||
input.value = ''
|
||
productImageUploading.value = false
|
||
}
|
||
}
|
||
|
||
const removeProductCreateImage = (): void => {
|
||
productCreateForm.imageFileIds = ''
|
||
productCreateForm.imageUrl = ''
|
||
}
|
||
|
||
const buildProductCreatePayload = (): ProductCreateRequest | null => {
|
||
const title = productCreateForm.title.trim()
|
||
const price = Number(productCreateForm.price)
|
||
const durationMinutes = normalizeDuration(productCreateForm.durationMinutes)
|
||
if (!title || Number.isNaN(price) || price < 0 || !durationMinutes) {
|
||
toast.error('请填写作品名称、价格和时长')
|
||
return null
|
||
}
|
||
return {
|
||
title,
|
||
price,
|
||
description: normalizeRichText(productCreateForm.description) || undefined,
|
||
targetAudience: productCreateForm.targetAudience.trim() || undefined,
|
||
precautions: productCreateForm.precautions.trim() || undefined,
|
||
imageFileIds: productCreateForm.imageFileIds || undefined,
|
||
tags: productCreateForm.tagIds.join(';') || undefined,
|
||
approximateDuration: `${durationMinutes}分钟`,
|
||
bookableArtists: productCreateForm.bookableArtistIds.join(',') || undefined,
|
||
frontVisible: 1
|
||
}
|
||
}
|
||
|
||
const createProduct = async (): Promise<void> => {
|
||
if (productImageUploading.value) {
|
||
toast.error('图片上传完成后再保存作品')
|
||
return
|
||
}
|
||
const payload = buildProductCreatePayload()
|
||
if (!payload) {
|
||
return
|
||
}
|
||
creatingProduct.value = true
|
||
try {
|
||
const productId = await managerProductsApi.create(payload)
|
||
await loadOptions()
|
||
const createdProduct = products.value.find(product => String(product.id) === String(productId))
|
||
if (!createdProduct) {
|
||
throw new Error(`作品已创建但未在列表中返回,作品ID:${productId}`)
|
||
}
|
||
updateProductCreateModalVisible(false)
|
||
selectProduct(createdProduct)
|
||
toast.success('作品已新增')
|
||
} catch (error) {
|
||
const message = error instanceof Error ? error.message : '新增作品失败'
|
||
toast.error(message)
|
||
} finally {
|
||
creatingProduct.value = false
|
||
}
|
||
}
|
||
|
||
const refreshAvailableTimes = async (): Promise<void> => {
|
||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||
if (!form.appointmentDate || !form.artistId || !durationMinutes) {
|
||
availableTimeGroups.value = []
|
||
timeSelectTip.value = '请先选择美甲师并填写时长'
|
||
return
|
||
}
|
||
timeLoading.value = true
|
||
try {
|
||
const calendar = withoutCurrentAppointment(await managerPlanningApi.calendar({
|
||
startDate: form.appointmentDate,
|
||
endDate: resolveAppointmentCalendarEndDate(form),
|
||
artistId: form.artistId
|
||
}))
|
||
availableTimeGroups.value = buildAvailableTimeGroups(calendar, form)
|
||
timeSelectTip.value = availableTimeGroups.value.length > 0 ? '请选择预约时间' : '当前美甲师当天暂无可约时间'
|
||
} finally {
|
||
timeLoading.value = false
|
||
}
|
||
}
|
||
|
||
const openTimeSheet = async (): Promise<void> => {
|
||
if (!form.appointmentDate || !form.artistId || !normalizeDuration(form.durationMinutes)) {
|
||
toast.error('请先选择美甲师并填写时长')
|
||
return
|
||
}
|
||
updateTimeSheetVisible(true)
|
||
await refreshAvailableTimes()
|
||
}
|
||
|
||
const selectAvailableTime = (value: string): void => {
|
||
form.appointmentTime = value
|
||
updateTimeSheetVisible(false)
|
||
void checkAvailability()
|
||
}
|
||
|
||
const checkAvailability = async (): Promise<{ ok: boolean; tip?: string }> => {
|
||
if (!form.appointmentDate || !form.appointmentTime || !form.artistId || !normalizeDuration(form.durationMinutes)) {
|
||
conflictTip.value = ''
|
||
return { ok: true }
|
||
}
|
||
const calendar = withoutCurrentAppointment(await managerPlanningApi.calendar({
|
||
startDate: form.appointmentDate,
|
||
endDate: resolveAppointmentCalendarEndDate(form),
|
||
artistId: form.artistId
|
||
}))
|
||
const daySummary = (calendar.daySummaries || []).find(item => item.date === form.appointmentDate)
|
||
if (daySummary?.dayStatus === 'rest') {
|
||
conflictTip.value = '当前日期为店铺休息日'
|
||
conflictLevel.value = 'danger'
|
||
return { ok: false, tip: conflictTip.value }
|
||
}
|
||
const availableGroups = buildAvailableTimeGroups(calendar, form)
|
||
const selectedTime = availableGroups.flatMap(group => group.times).find(item => item.value === form.appointmentTime)
|
||
if (!selectedTime) {
|
||
conflictTip.value = '当前时间不可预约,请重新选择可约时间'
|
||
conflictLevel.value = 'danger'
|
||
return { ok: false, tip: conflictTip.value }
|
||
}
|
||
conflictTip.value = `当前时段 ${selectedTime.rangeText} 可预约`
|
||
conflictLevel.value = 'free'
|
||
return { ok: true, tip: conflictTip.value }
|
||
}
|
||
|
||
const submit = async (): Promise<void> => {
|
||
const hasManualProduct = form.manualProductTitle.trim() && form.manualProductPrice.trim()
|
||
const customerSelectionError = validateAppointmentCustomerSelection(form.customerUserId, form.customerName, form.phone)
|
||
if (customerSelectionError) {
|
||
toast.error(customerSelectionError)
|
||
return
|
||
}
|
||
if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.artistId || !form.appointmentTime) {
|
||
toast.error('请补全预约信息')
|
||
return
|
||
}
|
||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||
if (!durationMinutes) {
|
||
toast.error('请填写预约时长')
|
||
return
|
||
}
|
||
const availability = await checkAvailability()
|
||
if (!availability.ok) {
|
||
toast.error(availability.tip || '该时段已被预约')
|
||
return
|
||
}
|
||
loading.value = true
|
||
try {
|
||
const payload = {
|
||
...form,
|
||
customerUserId: form.customerUserId || undefined,
|
||
productId: form.productId || undefined,
|
||
artistId: form.artistId,
|
||
durationMinutes,
|
||
manualProductPrice: hasManualProduct ? Number(form.manualProductPrice) : undefined,
|
||
clientRequestId: buildClientRequestId(isEditMode.value ? 'manager-appointment-update' : 'manager-appointment')
|
||
}
|
||
const result = isEditMode.value
|
||
? await managerPlanningApi.updateAppointment(editAppointmentId.value, payload)
|
||
: await managerPlanningApi.createAppointment(payload)
|
||
if (!isEditMode.value) {
|
||
removeDraftForm(session.userInfo)
|
||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
}
|
||
const refreshTarget = isEditMode.value
|
||
? { date: result.date || form.appointmentDate, artistId: String(result.artistId ?? form.artistId) }
|
||
: { date: result.date || form.appointmentDate }
|
||
markPlanningRefresh(refreshTarget)
|
||
toast.success(isEditMode.value ? '预约已修改' : '预约已创建')
|
||
await router.replace(`/appointments/${result.orderItemId}`)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const saveDraft = (): void => {
|
||
if (isEditMode.value) {
|
||
return
|
||
}
|
||
writeDraftForm(session.userInfo, { ...form })
|
||
draftSavedAtText.value = '草稿已保存到本机'
|
||
toast.success('草稿已保存')
|
||
}
|
||
|
||
const clearDraft = (): void => {
|
||
if (isEditMode.value) {
|
||
return
|
||
}
|
||
removeDraftForm(session.userInfo)
|
||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
draftSavedAtText.value = ''
|
||
toast.success('草稿已清空')
|
||
}
|
||
|
||
onMounted(async () => {
|
||
loading.value = true
|
||
try {
|
||
await loadOptions()
|
||
if (isEditMode.value) {
|
||
await loadAppointmentForEdit()
|
||
}
|
||
if (form.customerUserId) {
|
||
const customer = await managerCustomersApi.detail(form.customerUserId)
|
||
selectCustomer(customer)
|
||
}
|
||
await searchCustomers()
|
||
await refreshAvailableTimes()
|
||
} catch (error) {
|
||
const message = error instanceof Error ? error.message : '预约信息加载失败'
|
||
toast.error(message)
|
||
if (isEditMode.value) {
|
||
await router.replace(`/appointments/${editAppointmentId.value}`)
|
||
}
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
})
|
||
|
||
watch(form, syncAppointmentDraft, { deep: true })
|
||
watch(productModalVisible, syncAppointmentModalState)
|
||
watch(timeSheetVisible, syncAppointmentModalState)
|
||
watch(createCustomerForm, syncCreateCustomerDraft, { deep: true })
|
||
watch(createCustomerModalVisible, syncCreateCustomerDraft)
|
||
watch(productCreateForm, syncProductCreateDraft, { deep: true })
|
||
watch(productCreateModalVisible, value => {
|
||
syncProductCreateDraft()
|
||
if (value) {
|
||
void nextTick(syncDescriptionEditor)
|
||
}
|
||
})
|
||
|
||
if (productCreateModalVisible.value) {
|
||
void nextTick(syncDescriptionEditor)
|
||
}
|
||
</script>
|
||
|
||
<style scoped>
|
||
.draft-tip {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
margin-bottom: 12px;
|
||
padding: 10px 12px;
|
||
color: var(--accent);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.customer-combobox {
|
||
position: relative;
|
||
z-index: 30;
|
||
overflow: visible;
|
||
}
|
||
|
||
.customer-combobox:focus-within {
|
||
z-index: 80;
|
||
}
|
||
|
||
.customer-control {
|
||
flex: 1;
|
||
min-width: 0;
|
||
position: relative;
|
||
width: 100%;
|
||
}
|
||
|
||
.customer-control input {
|
||
padding-right: 42px;
|
||
}
|
||
|
||
.customer-selected-row {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
width: 100%;
|
||
min-height: 24px;
|
||
padding-right: 42px;
|
||
color: var(--ink);
|
||
font-size: 14px;
|
||
text-align: left;
|
||
}
|
||
|
||
.customer-selected-name,
|
||
.customer-selected-row strong {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.customer-selected-name {
|
||
display: flex;
|
||
flex: 1 1 auto;
|
||
align-items: center;
|
||
gap: 8px;
|
||
min-width: 0;
|
||
}
|
||
|
||
.customer-selected-name b {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.customer-selected-row strong {
|
||
flex: 0 0 auto;
|
||
max-width: 54%;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.customer-avatar {
|
||
display: grid;
|
||
flex: 0 0 auto;
|
||
width: 30px;
|
||
height: 30px;
|
||
place-items: center;
|
||
overflow: hidden;
|
||
border-radius: 50%;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
font-size: 13px;
|
||
font-weight: 800;
|
||
}
|
||
|
||
.customer-avatar--small {
|
||
width: 28px;
|
||
height: 28px;
|
||
font-size: 12px;
|
||
}
|
||
|
||
.customer-avatar img {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
}
|
||
|
||
.customer-avatar i {
|
||
font-style: normal;
|
||
}
|
||
|
||
.duration-control {
|
||
display: flex;
|
||
flex: 1;
|
||
align-items: center;
|
||
gap: 8px;
|
||
min-width: 0;
|
||
width: 100%;
|
||
}
|
||
|
||
.duration-control input {
|
||
min-width: 0;
|
||
padding-right: 0;
|
||
}
|
||
|
||
.duration-control span {
|
||
flex: 0 0 auto;
|
||
color: var(--muted);
|
||
font-size: 14px;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.customer-clear {
|
||
position: absolute;
|
||
top: 50%;
|
||
right: 8px;
|
||
width: 28px;
|
||
height: 28px;
|
||
transform: translateY(-50%);
|
||
border-radius: 50%;
|
||
background: var(--subtle);
|
||
color: var(--muted);
|
||
font-size: 18px;
|
||
line-height: 1;
|
||
}
|
||
|
||
.customer-dropdown {
|
||
position: absolute;
|
||
z-index: 90;
|
||
top: calc(100% + 8px);
|
||
right: 0;
|
||
left: 0;
|
||
display: grid;
|
||
max-height: min(520px, 70vh);
|
||
gap: 8px;
|
||
overflow-y: auto;
|
||
padding: 10px;
|
||
border: 1px solid var(--border);
|
||
border-radius: 12px;
|
||
background: var(--card-bg);
|
||
box-shadow: 0 16px 36px rgba(19, 32, 24, 0.16);
|
||
isolation: isolate;
|
||
-webkit-overflow-scrolling: touch;
|
||
}
|
||
|
||
.customer-options-scroll {
|
||
display: grid;
|
||
max-height: calc(48px * 5 + 8px * 4);
|
||
gap: 8px;
|
||
overflow-y: auto;
|
||
padding-right: 2px;
|
||
-webkit-overflow-scrolling: touch;
|
||
}
|
||
|
||
.customer-option {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
width: 100%;
|
||
min-height: 48px;
|
||
padding: 9px 10px;
|
||
border-radius: 10px;
|
||
background: var(--subtle);
|
||
text-align: left;
|
||
}
|
||
|
||
.customer-option-main,
|
||
.customer-option-phone {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.customer-option-main {
|
||
display: flex;
|
||
flex: 1 1 auto;
|
||
align-items: center;
|
||
gap: 8px;
|
||
min-width: 0;
|
||
}
|
||
|
||
.customer-option-main strong {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.customer-option-phone,
|
||
.customer-muted {
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.customer-option-phone {
|
||
flex: 0 0 auto;
|
||
max-width: 52%;
|
||
text-align: right;
|
||
}
|
||
|
||
.customer-new-entry {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
padding-top: 8px;
|
||
border-top: 1px solid var(--border);
|
||
}
|
||
|
||
.customer-new-entry span {
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.customer-create-form {
|
||
display: grid;
|
||
gap: 12px;
|
||
padding: 14px;
|
||
}
|
||
|
||
.picker-value {
|
||
justify-content: flex-start;
|
||
color: var(--ink);
|
||
text-align: left;
|
||
}
|
||
|
||
.conflict-tip {
|
||
margin-top: 12px;
|
||
padding: 10px 12px;
|
||
border-radius: 12px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.conflict-tip--danger {
|
||
background: #fff0ee;
|
||
color: var(--danger);
|
||
}
|
||
|
||
.product-modal,
|
||
.product-create-form,
|
||
.time-sheet {
|
||
display: grid;
|
||
gap: 12px;
|
||
padding: 14px;
|
||
}
|
||
|
||
.product-create-form {
|
||
gap: 0;
|
||
padding: 4px 14px 14px;
|
||
}
|
||
|
||
.product-create-row {
|
||
align-items: flex-start;
|
||
padding: 10px 0;
|
||
}
|
||
|
||
.product-create-row > label {
|
||
padding-top: 11px;
|
||
}
|
||
|
||
.product-create-control {
|
||
display: grid;
|
||
flex: 1;
|
||
min-width: 0;
|
||
gap: 8px;
|
||
}
|
||
|
||
.product-create-control .search-input {
|
||
height: 40px;
|
||
}
|
||
|
||
.product-create-control .textarea {
|
||
min-height: 92px;
|
||
}
|
||
|
||
.product-create-actions {
|
||
padding-top: 14px;
|
||
}
|
||
|
||
.product-create-actions .primary-btn {
|
||
width: 100%;
|
||
}
|
||
|
||
.product-modal__actions {
|
||
display: grid;
|
||
grid-template-columns: 1fr auto;
|
||
align-items: center;
|
||
gap: 8px;
|
||
}
|
||
|
||
.product-add-btn {
|
||
min-width: 72px;
|
||
}
|
||
|
||
.product-card {
|
||
display: grid;
|
||
grid-template-columns: 76px 1fr;
|
||
gap: 12px;
|
||
width: 100%;
|
||
padding: 10px;
|
||
border: 1px solid var(--border);
|
||
border-radius: 12px;
|
||
background: var(--card-bg);
|
||
text-align: left;
|
||
}
|
||
|
||
.product-card__cover {
|
||
display: grid;
|
||
width: 76px;
|
||
height: 76px;
|
||
place-items: center;
|
||
overflow: hidden;
|
||
border-radius: 12px;
|
||
background: var(--subtle);
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
font-weight: 800;
|
||
}
|
||
|
||
.product-card__cover img {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
}
|
||
|
||
.product-card__body {
|
||
min-width: 0;
|
||
}
|
||
|
||
.product-card__body strong,
|
||
.product-card__body p,
|
||
.product-card__body small {
|
||
display: block;
|
||
}
|
||
|
||
.product-card__body strong,
|
||
.product-card__body p {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.product-card__body p,
|
||
.product-card__body small {
|
||
margin: 3px 0 0;
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.product-card__meta {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
margin-top: 8px;
|
||
color: var(--ink);
|
||
font-size: 13px;
|
||
font-weight: 850;
|
||
}
|
||
|
||
.tag-row {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 5px;
|
||
margin-top: 6px;
|
||
}
|
||
|
||
.tag-row i {
|
||
padding: 2px 6px;
|
||
border-radius: 999px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
font-size: 11px;
|
||
font-style: normal;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.rich-editor {
|
||
overflow: hidden;
|
||
border: 1px solid var(--border);
|
||
border-radius: 12px;
|
||
background: var(--card-bg);
|
||
}
|
||
|
||
.rich-editor__toolbar {
|
||
display: flex;
|
||
gap: 6px;
|
||
padding: 8px;
|
||
border-bottom: 1px solid var(--border);
|
||
background: var(--subtle);
|
||
}
|
||
|
||
.rich-tool {
|
||
display: grid;
|
||
width: 30px;
|
||
height: 30px;
|
||
place-items: center;
|
||
border-radius: 8px;
|
||
background: var(--card-bg);
|
||
color: var(--ink);
|
||
font-size: 13px;
|
||
font-weight: 900;
|
||
}
|
||
|
||
.rich-tool--italic {
|
||
font-style: italic;
|
||
}
|
||
|
||
.rich-tool--underline {
|
||
text-decoration: underline;
|
||
}
|
||
|
||
.rich-editor__content {
|
||
min-height: 116px;
|
||
padding: 10px 12px;
|
||
outline: 0;
|
||
color: var(--ink);
|
||
font-size: 14px;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.rich-editor__content:empty::before {
|
||
color: var(--muted);
|
||
content: attr(data-placeholder);
|
||
}
|
||
|
||
.rich-editor__content ul,
|
||
.rich-editor__content ol {
|
||
margin: 6px 0;
|
||
padding-left: 20px;
|
||
}
|
||
|
||
.manual-image-row {
|
||
display: grid;
|
||
grid-template-columns: 64px minmax(0, 1fr);
|
||
align-items: center;
|
||
gap: 10px;
|
||
}
|
||
|
||
.manual-image-row img,
|
||
.manual-image-placeholder {
|
||
width: 64px;
|
||
height: 64px;
|
||
border-radius: 12px;
|
||
object-fit: cover;
|
||
}
|
||
|
||
.manual-image-placeholder {
|
||
display: grid;
|
||
place-items: center;
|
||
background: var(--subtle);
|
||
color: var(--muted);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.manual-image-actions {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
align-items: center;
|
||
gap: 8px;
|
||
min-width: 0;
|
||
}
|
||
|
||
.image-upload {
|
||
position: relative;
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
min-height: 40px;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.image-upload input {
|
||
position: absolute;
|
||
inset: 0;
|
||
opacity: 0;
|
||
}
|
||
|
||
.textarea {
|
||
min-height: 88px;
|
||
padding: 12px;
|
||
resize: vertical;
|
||
}
|
||
|
||
.choice-grid {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
}
|
||
|
||
.choice-empty {
|
||
padding: 10px 12px;
|
||
border: 1px dashed var(--border);
|
||
border-radius: 12px;
|
||
background: var(--subtle);
|
||
color: var(--muted);
|
||
font-size: 13px;
|
||
}
|
||
|
||
.choice-chip {
|
||
position: relative;
|
||
display: inline-flex;
|
||
}
|
||
|
||
.choice-chip input {
|
||
position: absolute;
|
||
opacity: 0;
|
||
pointer-events: none;
|
||
}
|
||
|
||
.choice-chip span {
|
||
padding: 7px 10px;
|
||
border: 1px solid var(--border);
|
||
border-radius: 999px;
|
||
background: var(--card-bg);
|
||
color: var(--ink);
|
||
font-size: 12px;
|
||
font-weight: 760;
|
||
}
|
||
|
||
.choice-chip input:checked + span {
|
||
border-color: var(--accent);
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
}
|
||
|
||
.time-sheet h3 {
|
||
margin: 0 0 8px;
|
||
}
|
||
|
||
.time-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(3, 1fr);
|
||
gap: 8px;
|
||
}
|
||
|
||
.time-grid button {
|
||
display: grid;
|
||
gap: 2px;
|
||
padding: 10px 4px;
|
||
border-radius: 12px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
}
|
||
|
||
.time-grid span {
|
||
font-size: 11px;
|
||
}
|
||
</style>
|