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

214 lines
6.7 KiB
Vue

<template>
<div class="export-view">
<section class="card form-card">
<label class="field">
<span class="field-label">导出类型</span>
<select v-model="type" @change="loadPreview">
<option v-for="item in types" :key="item" :value="item">{{ item }}</option>
</select>
</label>
<label class="field">
<span class="field-label">开始日期</span>
<input v-model="startDate" class="field-value" type="date" @change="loadPreview" />
</label>
<label class="field">
<span class="field-label">结束日期</span>
<input v-model="endDate" class="field-value" type="date" @change="loadPreview" />
</label>
</section>
<section class="card preview-card section">
<div class="preview-head">
<strong>数据预览</strong>
<span>最多展示 10 条</span>
</div>
<LoadingState v-if="previewLoading" text="预览加载中" />
<EmptyState v-else-if="previewError" title="预览加载失败" :description="previewError" />
<EmptyState v-else-if="previewRows.length === 0" title="暂无可预览数据" description="调整日期后再试" />
<div v-else class="preview-scroll">
<table>
<thead>
<tr>
<th v-for="column in previewColumns" :key="column">{{ column }}</th>
</tr>
</thead>
<tbody>
<tr v-for="row in previewRows" :key="row.key">
<td v-for="column in previewColumns" :key="`${row.key}-${column}`">{{ row[column] }}</td>
</tr>
</tbody>
</table>
</div>
</section>
<div class="fixed-bottom">
<div class="fixed-bottom__inner">
<button class="primary-btn" type="button" @click="exportExcel">导出 Excel</button>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import EmptyState from '@/components/EmptyState.vue'
import LoadingState from '@/components/LoadingState.vue'
import { managerCustomersApi } from '@/api/managerCustomers'
import { managerEmployeesApi } from '@/api/managerEmployees'
import { managerPlanningApi } from '@/api/managerPlanning'
import { formatDate, startOfMonth } from '@/utils/date'
import { useToastStore } from '@/stores/toast'
import type { Appointment, Artist } from '@/types/manager'
import type { UserInfo } from '@/types/common'
type ExportType = '订单数据' | '客户数据' | '员工数据'
type PreviewRow = { key: string } & Record<string, string>
const PREVIEW_SIZE = 10
const types: ExportType[] = ['订单数据', '客户数据', '员工数据']
const toast = useToastStore()
const type = ref<ExportType>('订单数据')
const startDate = ref(formatDate(startOfMonth(new Date())))
const endDate = ref(formatDate(new Date()))
const previewColumns = ref<string[]>([])
const previewRows = ref<PreviewRow[]>([])
const previewLoading = ref(false)
const previewError = ref('')
const shortDateTime = (value: unknown): string => {
const text = String(value || '')
return text.length > 16 ? text.slice(0, 16) : text || '-'
}
const timeRange = (item: Appointment): string => {
const start = String(item.startTime || '').slice(11, 16)
const end = String(item.endTime || '').slice(11, 16)
if (!start && !end) {
return '-'
}
return `${start || '-'}-${end || '-'}`
}
const scheduleSummary = (item: Artist): string => {
const source = item as Artist & { scheduleModeSummary?: string; scheduleMode?: string; scheduleTimeSummary?: string; ruleTimeSummary?: string }
const mode = source.scheduleModeSummary || source.scheduleMode || ''
const time = source.scheduleTimeSummary || source.ruleTimeSummary || ''
return [mode, time].filter(Boolean).join(' ') || '未设置'
}
const loadOrderPreview = async (): Promise<void> => {
const data = await managerPlanningApi.calendar({ startDate: startDate.value, endDate: endDate.value })
previewColumns.value = ['日期', '客户', '项目', '美甲师', '时间', '状态']
previewRows.value = (data.appointments || []).slice(0, PREVIEW_SIZE).map((item, index) => ({
key: `order-${item.orderItemId || index}`,
日期: item.date || String(item.startTime || '').slice(0, 10) || '-',
客户: item.customerName || '到店客户',
项目: item.productTitle || '预约作品',
美甲师: item.artistName || '未分配',
时间: timeRange(item),
状态: item.statusText || '-'
}))
}
const loadCustomerPreview = async (): Promise<void> => {
const data = await managerCustomersApi.list({ page: 1, size: PREVIEW_SIZE })
previewColumns.value = ['客户', '手机号', '最近时间']
previewRows.value = (data.list || []).slice(0, PREVIEW_SIZE).map((item: UserInfo & { name?: string; lastOrderTime?: string; lastConsumeTime?: string }, index) => ({
key: `customer-${item.id || index}`,
客户: item.nickname || item.name || item.account || '客户',
手机号: item.contactPhone || item.phone || '-',
最近时间: shortDateTime(item.lastOrderTime || item.lastConsumeTime)
}))
}
const loadEmployeePreview = async (): Promise<void> => {
const list = await managerEmployeesApi.list()
previewColumns.value = ['员工', '手机号', '排班', '状态']
previewRows.value = list.slice(0, PREVIEW_SIZE).map((item, index) => ({
key: `employee-${item.id || item.userId || index}`,
员工: item.nickname || item.name || '美甲师',
手机号: item.phone || '-',
排班: scheduleSummary(item),
状态: Number(item.status) === 0 ? '停用' : '启用'
}))
}
const loadPreview = async (): Promise<void> => {
previewLoading.value = true
previewError.value = ''
try {
if (type.value === '客户数据') {
await loadCustomerPreview()
} else if (type.value === '员工数据') {
await loadEmployeePreview()
} else {
await loadOrderPreview()
}
} catch (error) {
previewRows.value = []
previewError.value = error instanceof Error ? error.message : '预览数据加载失败'
} finally {
previewLoading.value = false
}
}
const exportExcel = (): void => {
toast.success('导出任务已创建')
}
onMounted(loadPreview)
</script>
<style scoped>
.export-view {
padding-bottom: 86px;
}
.preview-card {
padding: 14px;
}
.preview-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
.preview-head span {
color: var(--muted);
font-size: 12px;
}
.preview-scroll {
overflow-x: auto;
padding-bottom: 2px;
}
table {
width: max-content;
min-width: 100%;
border-collapse: collapse;
}
th,
td {
min-width: 88px;
padding: 10px 8px;
border-bottom: 1px solid var(--border);
text-align: left;
white-space: nowrap;
}
th {
color: #5c554e;
font-size: 12px;
}
td {
color: #3e3932;
font-size: 13px;
}
</style>