| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { z } from '@hono/zod-openapi';
- import { ErrorSchema } from '@d8d/shared-utils';
- // 时间筛选参数Schema
- export const TimeFilterSchema = z.object({
- startDate: z.string().datetime({ offset: true }).optional().openapi({
- description: '开始时间 (ISO 8601格式,例如: 2025-01-01T00:00:00Z)',
- example: '2025-01-01T00:00:00Z'
- }),
- endDate: z.string().datetime({ offset: true }).optional().openapi({
- description: '结束时间 (ISO 8601格式,例如: 2025-01-31T23:59:59Z)',
- example: '2025-01-31T23:59:59Z'
- }),
- timeRange: z.enum(['today', 'yesterday', 'last7days', 'last30days', 'custom']).optional().openapi({
- description: '时间范围筛选 (今日、昨日、最近7天、最近30天、自定义)',
- example: 'today'
- })
- }).refine((data) => {
- // 如果提供了timeRange为custom,则必须提供startDate和endDate
- if (data.timeRange === 'custom') {
- return !!(data.startDate && data.endDate);
- }
- return true;
- }, {
- message: '当timeRange为custom时,startDate和endDate必须提供',
- path: ['timeRange']
- }).refine((data) => {
- // 如果提供了startDate和endDate,确保startDate <= endDate
- if (data.startDate && data.endDate) {
- return new Date(data.startDate) <= new Date(data.endDate);
- }
- return true;
- }, {
- message: 'startDate不能晚于endDate',
- path: ['startDate']
- });
- // 数据概览统计响应Schema
- export const SummaryStatisticsSchema = z.object({
- totalSales: z.number().openapi({
- description: '总销售额',
- example: 150000.50
- }),
- totalOrders: z.number().int().openapi({
- description: '总订单数',
- example: 120
- }),
- wechatSales: z.number().openapi({
- description: '微信支付总金额',
- example: 100000.00
- }),
- wechatOrders: z.number().int().openapi({
- description: '微信支付订单数',
- example: 80
- }),
- creditSales: z.number().openapi({
- description: '额度支付总金额',
- example: 50000.50
- }),
- creditOrders: z.number().int().openapi({
- description: '额度支付订单数',
- example: 40
- }),
- todaySales: z.number().openapi({
- description: '今日销售额',
- example: 5000.00
- }),
- todayOrders: z.number().int().openapi({
- description: '今日订单数',
- example: 10
- })
- });
- // 今日数据响应Schema
- export const TodayStatisticsSchema = z.object({
- todaySales: z.number().openapi({
- description: '今日销售额',
- example: 5000.00
- }),
- todayOrders: z.number().int().openapi({
- description: '今日订单数',
- example: 10
- })
- });
- // 统一响应Schema
- export const SummaryResponseSchema = z.object({
- data: SummaryStatisticsSchema,
- success: z.boolean().openapi({
- description: '请求是否成功',
- example: true
- }),
- message: z.string().optional().openapi({
- description: '响应消息',
- example: '请求成功'
- })
- });
- export const TodayResponseSchema = z.object({
- data: TodayStatisticsSchema,
- success: z.boolean().openapi({
- description: '请求是否成功',
- example: true
- }),
- message: z.string().optional().openapi({
- description: '响应消息',
- example: '请求成功'
- })
- });
- // 导出错误Schema
- export { ErrorSchema };
|