index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { z } from '@hono/zod-openapi';
  2. import { ErrorSchema } from '@d8d/shared-utils';
  3. // 时间筛选参数Schema
  4. export const TimeFilterSchema = z.object({
  5. startDate: z.string().datetime({ offset: true }).optional().openapi({
  6. description: '开始时间 (ISO 8601格式,例如: 2025-01-01T00:00:00Z)',
  7. example: '2025-01-01T00:00:00Z'
  8. }),
  9. endDate: z.string().datetime({ offset: true }).optional().openapi({
  10. description: '结束时间 (ISO 8601格式,例如: 2025-01-31T23:59:59Z)',
  11. example: '2025-01-31T23:59:59Z'
  12. }),
  13. timeRange: z.enum(['today', 'yesterday', 'last7days', 'last30days', 'custom']).optional().openapi({
  14. description: '时间范围筛选 (今日、昨日、最近7天、最近30天、自定义)',
  15. example: 'today'
  16. })
  17. }).refine((data) => {
  18. // 如果提供了timeRange为custom,则必须提供startDate和endDate
  19. if (data.timeRange === 'custom') {
  20. return !!(data.startDate && data.endDate);
  21. }
  22. return true;
  23. }, {
  24. message: '当timeRange为custom时,startDate和endDate必须提供',
  25. path: ['timeRange']
  26. }).refine((data) => {
  27. // 如果提供了startDate和endDate,确保startDate <= endDate
  28. if (data.startDate && data.endDate) {
  29. return new Date(data.startDate) <= new Date(data.endDate);
  30. }
  31. return true;
  32. }, {
  33. message: 'startDate不能晚于endDate',
  34. path: ['startDate']
  35. });
  36. // 数据概览统计响应Schema
  37. export const SummaryStatisticsSchema = z.object({
  38. totalSales: z.number().openapi({
  39. description: '总销售额',
  40. example: 150000.50
  41. }),
  42. totalOrders: z.number().int().openapi({
  43. description: '总订单数',
  44. example: 120
  45. }),
  46. wechatSales: z.number().openapi({
  47. description: '微信支付总金额',
  48. example: 100000.00
  49. }),
  50. wechatOrders: z.number().int().openapi({
  51. description: '微信支付订单数',
  52. example: 80
  53. }),
  54. creditSales: z.number().openapi({
  55. description: '额度支付总金额',
  56. example: 50000.50
  57. }),
  58. creditOrders: z.number().int().openapi({
  59. description: '额度支付订单数',
  60. example: 40
  61. }),
  62. todaySales: z.number().openapi({
  63. description: '今日销售额',
  64. example: 5000.00
  65. }),
  66. todayOrders: z.number().int().openapi({
  67. description: '今日订单数',
  68. example: 10
  69. })
  70. });
  71. // 今日数据响应Schema
  72. export const TodayStatisticsSchema = z.object({
  73. todaySales: z.number().openapi({
  74. description: '今日销售额',
  75. example: 5000.00
  76. }),
  77. todayOrders: z.number().int().openapi({
  78. description: '今日订单数',
  79. example: 10
  80. })
  81. });
  82. // 统一响应Schema
  83. export const SummaryResponseSchema = z.object({
  84. data: SummaryStatisticsSchema,
  85. success: z.boolean().openapi({
  86. description: '请求是否成功',
  87. example: true
  88. }),
  89. message: z.string().optional().openapi({
  90. description: '响应消息',
  91. example: '请求成功'
  92. })
  93. });
  94. export const TodayResponseSchema = z.object({
  95. data: TodayStatisticsSchema,
  96. success: z.boolean().openapi({
  97. description: '请求是否成功',
  98. example: true
  99. }),
  100. message: z.string().optional().openapi({
  101. description: '响应消息',
  102. example: '请求成功'
  103. })
  104. });
  105. // 导出错误Schema
  106. export { ErrorSchema };