Files
JayuCloud/JY-Salon-Front/src/views/PlanningView.vue
T
2026-06-29 23:26:43 +08:00

911 lines
24 KiB
Vue

<template>
<MobileShell>
<section class="card control-card">
<div class="date-row">
<button class="icon-button" type="button" @click="shiftRange(-1)"><ChevronLeft :size="20" /></button>
<div class="date-row__center">
<strong>{{ dateTitle }}</strong>
<span>{{ businessStartTime }}-{{ businessEndTime }}</span>
</div>
<div class="date-row__tools">
<button class="today-btn" type="button" @click="goToday">今</button>
<button class="icon-button" type="button" @click="shiftRange(1)"><ChevronRight :size="20" /></button>
</div>
</div>
<div class="segmented">
<button v-for="mode in viewModes" :key="mode.value" type="button" :class="{ active: viewMode === mode.value }" @click="switchView(mode.value)">
{{ mode.label }}
</button>
</div>
<div class="artist-filter">
<button class="chip" :class="{ 'chip--active': !selectedArtistId }" type="button" @click="selectArtist('')">全部员工</button>
<button
v-for="artist in artists"
:key="String(artist.id)"
class="chip artist-chip"
:class="{ 'chip--active': String(selectedArtistId) === String(artist.id) }"
type="button"
@click="selectArtist(artist.id)"
>
<span class="dot" :style="{ backgroundColor: artist.color || '#b8b0a6' }"></span>
{{ artist.name || artist.nickname }}
<small>{{ artist.rangeCount || 0 }}</small>
</button>
</div>
</section>
<section v-if="calendarError" class="calendar-error-card">
<div>
<strong>规划数据加载失败</strong>
<p>{{ calendarError }}</p>
</div>
<button class="secondary-btn" type="button" @click="loadCalendar">重试</button>
</section>
<LoadingState v-else-if="loading" text="正在加载规划" />
<template v-else>
<section v-if="viewMode === 'day'" class="section">
<div class="day-summary">
<div class="card summary-item"><strong>{{ dayTotal }}</strong><span>今日预约</span></div>
<div class="card summary-item"><strong>{{ freeArtistCount }}</strong><span>可插单员工</span></div>
<div class="card summary-item"><strong>{{ pendingCount }}</strong><span>待确认</span></div>
</div>
<div class="card day-board">
<div class="time-axis">
<span v-for="hour in hours" :key="hour.value" :style="{ top: `${hour.top}px` }">{{ hour.label }}</span>
</div>
<div class="lane-scroll">
<div class="lanes" :style="{ width: `${laneWidth}px`, minHeight: `${timelineHeight}px` }">
<div v-for="lane in dayLanes" :key="String(lane.id)" class="artist-lane" :style="{ width: `${laneItemWidth}px` }">
<div class="lane-head">
<span class="dot" :style="{ backgroundColor: lane.color || '#b8b0a6' }"></span>
{{ lane.name || lane.nickname }}
</div>
<button
v-for="block in lane.blocks"
:key="String(block.orderItemId)"
class="appointment-block"
:class="{ pending: block.status === 0 }"
type="button"
:style="{ top: `${block.top}px`, height: `${block.height}px`, borderLeftColor: lane.color || '#b8b0a6' }"
@click="openAppointment(block.orderItemId)"
>
<strong>{{ block.customerName || '客户' }}</strong>
<span>{{ block.timeText }} · {{ block.productTitle || block.manualProductTitle || '预约作品' }}</span>
<small>{{ block.statusText || '待处理' }}</small>
</button>
<div v-if="lane.blocks.length === 0" class="lane-empty">全天可约</div>
</div>
</div>
</div>
</div>
</section>
<section v-if="viewMode === 'week'" class="section">
<div class="week-strip">
<button
v-for="day in weekDays"
:key="day.date"
class="card week-day-card"
:class="{ active: day.date === currentDateText }"
type="button"
@click="selectDate(day.date)"
>
<span>{{ day.weekName }}</span>
<strong>{{ day.day }}</strong>
<small>{{ day.totalCount }}单</small>
</button>
</div>
<div class="card week-grid">
<div class="week-grid__head">
<strong>本周排班密度</strong>
<span>{{ weekRange }}</span>
</div>
<div v-for="day in weekDays" :key="`${day.date}-line`" class="week-line">
<span>{{ day.weekName }} {{ day.day }}</span>
<div class="week-line__bars" :class="{ empty: day.totalCount === 0 }">
<div
v-for="count in day.artistCounts"
:key="String(count.artistId)"
class="week-count-bar"
:style="{ width: `${count.barWidth}%`, backgroundColor: count.color }"
>
{{ count.shortName }}{{ count.count }}
</div>
</div>
</div>
</div>
</section>
<section v-if="viewMode === 'month'" class="section">
<div class="month-toolbar">
<span>月历显示</span>
<div class="month-mode-switch">
<button v-for="mode in monthDisplayModes" :key="mode.value" :class="{ active: monthDisplayMode === mode.value }" type="button" @click="monthDisplayMode = mode.value">
{{ mode.label }}
</button>
</div>
<button v-if="monthDisplayMode === 'rows'" class="row-name-switch" :class="{ active: monthRowLabelVisible }" type="button" @click="monthRowLabelVisible = !monthRowLabelVisible">
姓名
</button>
</div>
<div class="card month-card">
<div class="month-week-head">
<span v-for="label in weekLabels" :key="label">{{ label }}</span>
</div>
<div class="month-grid">
<button
v-for="day in monthDays"
:key="day.date"
class="month-cell"
:class="{ selected: day.date === currentDateText, 'out-month': !day.isCurrentMonth }"
type="button"
@click="selectDate(day.date)"
>
<span class="month-day">{{ day.day }}</span>
<span v-if="!day.isPast && day.dayStatus === 'full'" class="day-mark full">满</span>
<span v-else-if="!day.isPast && day.dayStatus === 'rest'" class="day-mark rest">休</span>
<div v-if="monthDisplayMode === 'count'" class="month-bars">
<span v-for="count in day.artistCounts.filter(item => item.count > 0)" :key="String(count.artistId)" :style="{ backgroundColor: count.color }">
{{ count.shortName }}{{ count.count }}
</span>
<small v-if="day.totalCount === 0">空</small>
</div>
<div v-else-if="monthDisplayMode === 'blocks'" class="month-blocks">
<span v-for="block in day.expandedBlocks" :key="block.key" :style="{ backgroundColor: block.color }"></span>
<small v-if="day.totalCount === 0">空</small>
</div>
<div v-else class="month-blocks month-blocks--rows">
<span v-for="block in day.rowBlocks" :key="block.key" :style="{ backgroundColor: block.color }">{{ monthRowLabelVisible ? block.artistName : '' }}</span>
<small v-if="day.totalCount === 0">空</small>
</div>
</button>
</div>
</div>
</section>
</template>
<button v-if="canCreate && !calendarError" class="fab" type="button" @click="createAppointment">+</button>
</MobileShell>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ChevronLeft, ChevronRight } from 'lucide-vue-next'
import MobileShell from '@/components/MobileShell.vue'
import LoadingState from '@/components/LoadingState.vue'
import { managerPlanningApi } from '@/api/managerPlanning'
import type { Appointment, Artist, DaySummary } from '@/types/manager'
import type { IdType } from '@/types/common'
import { useSessionStore } from '@/stores/session'
import { useToastStore } from '@/stores/toast'
import { canCreateAppointment } from '@/utils/permissions'
import {
addDays,
addMonths,
endOfMonth,
endOfWeek,
formatCnDate,
formatDate,
formatMonth,
monthGrid,
minutesOfDay,
startOfMonth,
startOfWeek,
timeFromMinutes
} from '@/utils/date'
import { resolveAppointmentDate, resolveAppointmentStartMinute, resolveAppointmentTimeText, summaryMap } from '@/utils/planning'
type ViewMode = 'day' | 'week' | 'month'
type MonthDisplayMode = 'count' | 'blocks' | 'rows'
interface NormalizedCount {
artistId: IdType
artistName?: string
shortName?: string
color: string
count: number
status?: string
width?: number
barWidth?: number
}
interface MonthBlock {
key: string
artistId: IdType
color: string
shortName?: string
artistName?: string
}
interface MonthDay {
date: string
day: number
isCurrentMonth: boolean
isPast: boolean
totalCount: number
dayStatus: string
artistCounts: NormalizedCount[]
expandedBlocks: MonthBlock[]
rowBlocks: MonthBlock[]
}
interface WeekDay {
date: string
weekName: string
day: number
totalCount: number
artistCounts: NormalizedCount[]
}
interface DayLane extends Artist {
blocks: Array<Appointment & { top: number; height: number; timeText: string }>
}
const router = useRouter()
const session = useSessionStore()
const toast = useToastStore()
const viewModes: Array<{ value: ViewMode; label: string }> = [
{ value: 'day', label: '日' },
{ value: 'week', label: '周' },
{ value: 'month', label: '月' }
]
const monthDisplayModes: Array<{ value: MonthDisplayMode; label: string }> = [
{ value: 'count', label: '单量' },
{ value: 'blocks', label: '点阵' },
{ value: 'rows', label: '横条' }
]
const weekLabels = ['一', '二', '三', '四', '五', '六', '日']
const viewMode = ref<ViewMode>('month')
const monthDisplayMode = ref<MonthDisplayMode>('count')
const monthRowLabelVisible = ref(false)
const currentDateText = ref(formatDate(new Date()))
const selectedArtistId = ref<IdType | ''>('')
const businessStartTime = ref('10:00')
const businessEndTime = ref('22:00')
const artists = ref<Artist[]>([])
const appointments = ref<Appointment[]>([])
const daySummaries = ref<DaySummary[]>([])
const loading = ref(false)
const calendarError = ref('')
const canCreate = computed(() => canCreateAppointment(session.userInfo))
const dateTitle = computed(() => {
const range = resolveRange()
if (viewMode.value === 'month') {
return formatMonth(currentDateText.value)
}
if (viewMode.value === 'week') {
return `${formatCnDate(range.start)} - ${formatCnDate(range.end)}`
}
return formatCnDate(currentDateText.value)
})
const weekRange = computed(() => {
const range = resolveRange()
return `${formatCnDate(range.start)} - ${formatCnDate(range.end)}`
})
const resolveRange = (): { start: string; end: string } => {
if (viewMode.value === 'week') {
return { start: formatDate(startOfWeek(currentDateText.value)), end: formatDate(endOfWeek(currentDateText.value)) }
}
if (viewMode.value === 'month') {
return { start: formatDate(startOfMonth(currentDateText.value)), end: formatDate(endOfMonth(currentDateText.value)) }
}
return { start: currentDateText.value, end: currentDateText.value }
}
const normalizeArtistCounts = (counts: NonNullable<DaySummary['artistCounts']>): NormalizedCount[] => {
const artistMap = artists.value.reduce<Record<string, Artist>>((result, artist) => {
result[String(artist.id)] = artist
return result
}, {})
return counts.map(count => {
const artist = artistMap[String(count.artistId)]
return {
artistId: count.artistId,
artistName: count.artistName || artist?.name,
shortName: count.shortName || artist?.shortName || String(count.artistName || artist?.name || '?').slice(0, 1),
color: count.color || artist?.color || '#b8b0a6',
count: Number(count.count || 0),
status: count.status
}
})
}
const buildExpandedBlocks = (counts: NormalizedCount[]): MonthBlock[] => {
return counts.flatMap(count => Array.from({ length: count.count }, (_item, index) => ({
key: `${count.artistId}-${index}`,
artistId: count.artistId,
color: count.color,
shortName: count.shortName,
artistName: count.artistName
})))
}
const buildMonthDays = (): MonthDay[] => {
const map = summaryMap(daySummaries.value)
const todayText = formatDate(new Date())
const artistTemplate = artists.value.map(artist => ({
artistId: artist.id,
artistName: artist.name,
shortName: artist.shortName,
color: artist.color,
count: 0,
status: 'free'
}))
return monthGrid(currentDateText.value).map(day => {
const summary = map[day.date]
const artistCounts = normalizeArtistCounts(summary?.artistCounts || artistTemplate)
return {
...day,
isPast: day.date < todayText,
totalCount: summary?.totalCount || 0,
dayStatus: summary?.dayStatus || 'free',
artistCounts,
expandedBlocks: buildExpandedBlocks(artistCounts),
rowBlocks: buildExpandedBlocks(artistCounts)
}
})
}
const buildWeekDays = (): WeekDay[] => {
const start = startOfWeek(currentDateText.value)
const map = summaryMap(daySummaries.value)
return weekLabels.map((label, index) => {
const target = addDays(start, index)
const date = formatDate(target)
const summary = map[date]
const counts = normalizeArtistCounts(summary?.artistCounts || [])
.filter(count => count.count > 0)
.map(count => ({
...count,
width: Math.max(12, Math.min(100, count.count * 22)),
barWidth: Math.max(14, Math.min(100, count.count * 18))
}))
return { date, weekName: `周${label}`, day: target.getDate(), totalCount: summary?.totalCount || 0, artistCounts: counts }
})
}
const buildDayLanes = (): DayLane[] => {
const startMinute = minutesOfDay(businessStartTime.value)
const filteredArtists = selectedArtistId.value
? artists.value.filter(item => String(item.id) === String(selectedArtistId.value))
: artists.value
const dayAppointments = appointments.value.filter(item => resolveAppointmentDate(item) === currentDateText.value)
return filteredArtists.map(artist => {
const blocks = dayAppointments
.filter(item => String(item.artistId) === String(artist.id))
.map(item => {
const itemStart = resolveAppointmentStartMinute(item)
return {
...item,
top: Math.max(0, itemStart - startMinute) + 54,
height: Math.max(64, item.durationMinutes || 60),
timeText: resolveAppointmentTimeText(item)
}
})
return { ...artist, blocks }
})
}
const monthDays = computed(buildMonthDays)
const weekDays = computed(buildWeekDays)
const dayLanes = computed(buildDayLanes)
const hours = computed(() => {
const startMinute = minutesOfDay(businessStartTime.value)
const endMinute = minutesOfDay(businessEndTime.value)
const result: Array<{ value: number; label: string; top: number }> = []
for (let cursor = startMinute; cursor <= endMinute; cursor += 60) {
result.push({ value: cursor, label: timeFromMinutes(cursor), top: cursor - startMinute + 54 })
}
return result
})
const timelineHeight = computed(() => Math.max(560, minutesOfDay(businessEndTime.value) - minutesOfDay(businessStartTime.value)) + 54)
const laneItemWidth = 248
const laneWidth = computed(() => Math.max(720, dayLanes.value.length * laneItemWidth))
const dayAppointments = computed(() => appointments.value.filter(item => resolveAppointmentDate(item) === currentDateText.value))
const dayTotal = computed(() => dayAppointments.value.length)
const freeArtistCount = computed(() => dayLanes.value.filter(item => item.blocks.length === 0).length)
const pendingCount = computed(() => dayAppointments.value.filter(item => item.status === 0).length)
const loadCalendar = async (): Promise<void> => {
const range = resolveRange()
loading.value = true
try {
const data = await managerPlanningApi.calendar({
startDate: range.start,
endDate: range.end,
artistId: selectedArtistId.value || undefined
})
businessStartTime.value = String(data.businessStartTime || '10:00').slice(0, 5)
businessEndTime.value = String(data.businessEndTime || '22:00').slice(0, 5)
artists.value = data.artists || []
appointments.value = data.appointments || []
daySummaries.value = data.daySummaries || []
calendarError.value = ''
} catch (error) {
artists.value = []
appointments.value = []
daySummaries.value = []
calendarError.value = error instanceof Error ? error.message : '规划数据加载失败'
toast.error('规划数据加载失败')
} finally {
loading.value = false
}
}
const switchView = async (mode: ViewMode): Promise<void> => {
viewMode.value = mode
await loadCalendar()
}
const selectArtist = (id: IdType | ''): void => {
selectedArtistId.value = id
}
const selectDate = (date: string): void => {
currentDateText.value = date
if (viewMode.value === 'month') {
viewMode.value = 'day'
void loadCalendar()
}
}
const shiftRange = async (direction: number): Promise<void> => {
if (viewMode.value === 'month') {
currentDateText.value = formatDate(addMonths(currentDateText.value, direction))
} else if (viewMode.value === 'week') {
currentDateText.value = formatDate(addDays(currentDateText.value, direction * 7))
} else {
currentDateText.value = formatDate(addDays(currentDateText.value, direction))
}
await loadCalendar()
}
const goToday = async (): Promise<void> => {
currentDateText.value = formatDate(new Date())
await loadCalendar()
}
const createAppointment = (): void => {
if (!canCreate.value) {
toast.error('美甲师账号只能查看预约')
return
}
void router.push({ path: '/appointments/new', query: { date: currentDateText.value, artistId: String(selectedArtistId.value || '') } })
}
const openAppointment = (id: IdType): void => {
void router.push(`/appointments/${id}`)
}
onMounted(() => {
void loadCalendar()
})
</script>
<style scoped>
.control-card {
padding: 12px;
}
.date-row,
.date-row__tools {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.date-row__center {
display: grid;
justify-items: center;
}
.date-row__center strong {
font-size: 17px;
}
.date-row__center span {
color: var(--muted);
font-size: 12px;
}
.today-btn {
width: 36px;
height: 36px;
border-radius: 50%;
background: var(--accent-soft);
color: var(--accent);
font-weight: 900;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 12px;
padding: 4px;
border-radius: 14px;
background: var(--subtle);
}
.segmented button,
.month-mode-switch button,
.row-name-switch {
min-height: 34px;
border-radius: 12px;
color: var(--muted);
font-weight: 800;
}
.segmented .active,
.month-mode-switch .active,
.row-name-switch.active {
background: var(--card-bg);
color: var(--accent);
box-shadow: 0 6px 16px rgba(58, 48, 34, 0.06);
}
.artist-filter,
.week-strip {
display: flex;
gap: 8px;
margin-top: 12px;
overflow-x: auto;
padding-bottom: 2px;
}
.artist-chip {
gap: 6px;
white-space: nowrap;
}
.dot {
display: inline-block;
flex: 0 0 8px;
width: 8px;
height: 8px;
border-radius: 50%;
}
.calendar-error-card {
display: grid;
grid-template-columns: 1fr 92px;
gap: 12px;
margin-top: 14px;
padding: 14px;
border-radius: 12px;
background: #fff4ee;
color: #8c4a30;
}
.calendar-error-card p {
margin: 4px 0 0;
}
.day-summary {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.summary-item {
display: grid;
justify-items: center;
padding: 14px 6px;
}
.summary-item strong {
color: var(--accent);
font-size: 22px;
}
.summary-item span {
color: var(--muted);
font-size: 12px;
}
.day-board {
position: relative;
min-height: 620px;
margin-top: 12px;
overflow: hidden;
}
.time-axis {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 54px;
border-right: 1px solid var(--border);
background: rgba(255, 253, 248, 0.92);
}
.time-axis span {
position: absolute;
right: 8px;
color: var(--muted);
font-size: 11px;
}
.lane-scroll {
margin-left: 54px;
overflow-x: auto;
}
.lanes {
display: flex;
position: relative;
}
.artist-lane {
position: relative;
flex: 0 0 auto;
min-height: 620px;
border-right: 1px solid var(--border);
}
.lane-head {
position: sticky;
top: 0;
z-index: 1;
display: flex;
align-items: center;
gap: 6px;
height: 54px;
padding: 0 10px;
background: var(--card-bg);
font-weight: 800;
}
.appointment-block {
position: absolute;
right: 8px;
left: 8px;
display: grid;
align-content: start;
gap: 3px;
overflow: hidden;
padding: 8px;
border-left: 4px solid var(--accent);
border-radius: 10px;
background: #f7f2ea;
color: var(--ink);
text-align: left;
}
.appointment-block.pending {
background: #fff0de;
}
.appointment-block strong,
.appointment-block span,
.appointment-block small {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.appointment-block span,
.appointment-block small {
color: var(--muted);
font-size: 11px;
}
.lane-empty {
position: absolute;
top: 88px;
right: 12px;
left: 12px;
color: #b0a79d;
text-align: center;
}
.week-day-card {
display: grid;
justify-items: center;
flex: 0 0 86px;
padding: 12px 8px;
}
.week-day-card.active {
color: var(--accent);
outline: 2px solid var(--accent-soft);
}
.week-day-card strong {
font-size: 22px;
}
.week-grid {
margin-top: 12px;
padding: 14px;
}
.week-grid__head,
.week-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.week-grid__head span,
.week-line > span {
color: var(--muted);
font-size: 12px;
}
.week-line {
margin-top: 12px;
}
.week-line__bars {
display: flex;
align-items: center;
flex: 1;
min-height: 28px;
overflow: hidden;
border-radius: 999px;
background: var(--subtle);
}
.week-line__bars.empty::before {
content: "空";
margin-left: 12px;
color: #b0a79d;
font-size: 12px;
}
.week-count-bar {
display: grid;
place-items: center;
min-width: 28px;
height: 28px;
color: #fff;
font-size: 11px;
font-weight: 800;
}
.month-toolbar {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
}
.month-toolbar > span {
flex: 1;
font-weight: 800;
}
.month-mode-switch {
display: flex;
gap: 4px;
padding: 3px;
border-radius: 12px;
background: var(--subtle);
}
.month-mode-switch button {
min-height: 30px;
padding: 0 8px;
font-size: 12px;
}
.row-name-switch {
min-height: 30px;
padding: 0 10px;
background: var(--subtle);
font-size: 12px;
}
.month-card {
padding: 10px;
}
.month-week-head,
.month-grid {
display: grid;
grid-template-columns: repeat(7, minmax(0, 1fr));
}
.month-week-head span {
padding-bottom: 8px;
color: var(--muted);
font-size: 12px;
text-align: center;
}
.month-cell {
position: relative;
min-height: 78px;
padding: 6px 4px;
border: 1px solid var(--border);
background: transparent;
text-align: left;
}
.month-cell.selected {
background: var(--accent-soft);
}
.month-cell.out-month {
color: #b7afa5;
background: #faf7f0;
}
.month-day {
font-weight: 900;
}
.day-mark {
position: absolute;
top: 4px;
right: 4px;
border-radius: 999px;
padding: 1px 4px;
color: #fff;
font-size: 10px;
}
.day-mark.full {
background: var(--danger);
}
.day-mark.rest {
background: #9b948b;
}
.month-bars,
.month-blocks {
display: flex;
flex-wrap: wrap;
gap: 3px;
margin-top: 4px;
}
.month-bars span {
max-width: 100%;
overflow: hidden;
border-radius: 6px;
padding: 1px 4px;
color: #fff;
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.month-bars small,
.month-blocks small {
color: #b0a79d;
}
.month-blocks span {
width: 8px;
height: 8px;
border-radius: 50%;
}
.month-blocks--rows {
display: grid;
gap: 2px;
}
.month-blocks--rows span {
width: 100%;
height: 8px;
overflow: hidden;
border-radius: 999px;
color: #fff;
font-size: 8px;
line-height: 8px;
}
</style>