|
|
@@ -0,0 +1,156 @@
|
|
|
+import { z } from '@hono/zod-openapi';
|
|
|
+import { DeleteStatus, DisabledStatus } from '@/share/types';
|
|
|
+import { RoleSchema } from './role.schema';
|
|
|
+
|
|
|
+// 基础用户 schema(包含所有字段)
|
|
|
+export const UserSchema = z.object({
|
|
|
+ id: z.number().int().positive().openapi({ description: '用户ID' }),
|
|
|
+ username: z.string().min(3).max(255).openapi({
|
|
|
+ example: 'admin',
|
|
|
+ description: '用户名,3-255个字符'
|
|
|
+ }),
|
|
|
+ password: z.string().min(6).max(255).openapi({
|
|
|
+ example: 'password123',
|
|
|
+ description: '密码,最少6位'
|
|
|
+ }),
|
|
|
+ phone: z.string().max(255).nullable().openapi({
|
|
|
+ example: '13800138000',
|
|
|
+ description: '手机号'
|
|
|
+ }),
|
|
|
+ email: z.string().email().max(255).nullable().openapi({
|
|
|
+ example: 'user@example.com',
|
|
|
+ description: '邮箱'
|
|
|
+ }),
|
|
|
+ nickname: z.string().max(255).nullable().openapi({
|
|
|
+ example: '昵称',
|
|
|
+ description: '用户昵称'
|
|
|
+ }),
|
|
|
+ name: z.string().max(255).nullable().openapi({
|
|
|
+ example: '张三',
|
|
|
+ description: '真实姓名'
|
|
|
+ }),
|
|
|
+ avatar: z.string().max(255).nullable().openapi({
|
|
|
+ example: 'https://example.com/avatar.jpg',
|
|
|
+ description: '用户头像'
|
|
|
+ }),
|
|
|
+ isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED).openapi({
|
|
|
+ example: DisabledStatus.ENABLED,
|
|
|
+ description: '是否禁用(0:启用,1:禁用)'
|
|
|
+ }),
|
|
|
+ isDeleted: z.number().int().min(0).max(1).default(DeleteStatus.NOT_DELETED).openapi({
|
|
|
+ example: DeleteStatus.NOT_DELETED,
|
|
|
+ description: '是否删除(0:未删除,1:已删除)'
|
|
|
+ }),
|
|
|
+ roles: z.array(RoleSchema).optional().openapi({
|
|
|
+ example: [
|
|
|
+ {
|
|
|
+ id: 1,
|
|
|
+ name: 'admin',
|
|
|
+ description: '管理员',
|
|
|
+ permissions: ['user:create'],
|
|
|
+ createdAt: new Date(),
|
|
|
+ updatedAt: new Date()
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ description: '用户角色列表'
|
|
|
+ }),
|
|
|
+ createdAt: z.date().openapi({ description: '创建时间' }),
|
|
|
+ updatedAt: z.date().openapi({ description: '更新时间' })
|
|
|
+});
|
|
|
+
|
|
|
+// 创建用户请求 schema
|
|
|
+export const CreateUserDto = z.object({
|
|
|
+ username: z.string().min(3).max(255).openapi({
|
|
|
+ example: 'admin',
|
|
|
+ description: '用户名,3-255个字符'
|
|
|
+ }),
|
|
|
+ password: z.string().min(6).max(255).openapi({
|
|
|
+ example: 'password123',
|
|
|
+ description: '密码,最少6位'
|
|
|
+ }),
|
|
|
+ phone: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: '13800138000',
|
|
|
+ description: '手机号'
|
|
|
+ }),
|
|
|
+ email: z.string().email().max(255).nullable().optional().openapi({
|
|
|
+ example: 'user@example.com',
|
|
|
+ description: '邮箱'
|
|
|
+ }),
|
|
|
+ nickname: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: '昵称',
|
|
|
+ description: '用户昵称'
|
|
|
+ }),
|
|
|
+ name: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: '张三',
|
|
|
+ description: '真实姓名'
|
|
|
+ }),
|
|
|
+ avatar: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: 'https://example.com/avatar.jpg',
|
|
|
+ description: '用户头像'
|
|
|
+ }),
|
|
|
+ isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED).optional().openapi({
|
|
|
+ example: DisabledStatus.ENABLED,
|
|
|
+ description: '是否禁用(0:启用,1:禁用)'
|
|
|
+ })
|
|
|
+});
|
|
|
+
|
|
|
+// 更新用户请求 schema
|
|
|
+export const UpdateUserDto = z.object({
|
|
|
+ username: z.string().min(3).max(255).optional().openapi({
|
|
|
+ example: 'admin',
|
|
|
+ description: '用户名,3-255个字符'
|
|
|
+ }),
|
|
|
+ password: z.string().min(6).max(255).optional().openapi({
|
|
|
+ example: 'password123',
|
|
|
+ description: '密码,最少6位'
|
|
|
+ }),
|
|
|
+ phone: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: '13800138000',
|
|
|
+ description: '手机号'
|
|
|
+ }),
|
|
|
+ email: z.string().email().max(255).nullable().optional().openapi({
|
|
|
+ example: 'user@example.com',
|
|
|
+ description: '邮箱'
|
|
|
+ }),
|
|
|
+ nickname: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: '昵称',
|
|
|
+ description: '用户昵称'
|
|
|
+ }),
|
|
|
+ name: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: '张三',
|
|
|
+ description: '真实姓名'
|
|
|
+ }),
|
|
|
+ avatar: z.string().max(255).nullable().optional().openapi({
|
|
|
+ example: 'https://example.com/avatar.jpg',
|
|
|
+ description: '用户头像'
|
|
|
+ }),
|
|
|
+ isDisabled: z.number().int().min(0).max(1).optional().openapi({
|
|
|
+ example: DisabledStatus.ENABLED,
|
|
|
+ description: '是否禁用(0:启用,1:禁用)'
|
|
|
+ })
|
|
|
+});
|
|
|
+
|
|
|
+// 用户列表响应 schema
|
|
|
+export const UserListResponse = z.object({
|
|
|
+ data: z.array(UserSchema),
|
|
|
+ pagination: z.object({
|
|
|
+ total: z.number().openapi({
|
|
|
+ example: 100,
|
|
|
+ description: '总记录数'
|
|
|
+ }),
|
|
|
+ current: z.number().openapi({
|
|
|
+ example: 1,
|
|
|
+ description: '当前页码'
|
|
|
+ }),
|
|
|
+ pageSize: z.number().openapi({
|
|
|
+ example: 10,
|
|
|
+ description: '每页数量'
|
|
|
+ })
|
|
|
+ })
|
|
|
+});
|
|
|
+
|
|
|
+// 类型导出
|
|
|
+export type User = z.infer<typeof UserSchema>;
|
|
|
+export type CreateUserRequest = z.infer<typeof CreateUserDto>;
|
|
|
+export type UpdateUserRequest = z.infer<typeof UpdateUserDto>;
|
|
|
+export type UserListResponseType = z.infer<typeof UserListResponse>;
|