|
|
@@ -0,0 +1,140 @@
|
|
|
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
|
|
|
+import { z } from 'zod';
|
|
|
+import { AppDataSource } from '@/server/data-source';
|
|
|
+import { SilverUserProfile, CertificationStatus } from '@/server/modules/silver-users/silver-user-profile.entity';
|
|
|
+import { ErrorSchema } from '@/server/utils/errorHandler';
|
|
|
+import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
+import { AuthContext } from '@/server/types/context';
|
|
|
+
|
|
|
+// 路径参数Schema
|
|
|
+const CertificationParamsSchema = z.object({
|
|
|
+ id: z.string().openapi({
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
+ example: '1',
|
|
|
+ description: '用户资料ID'
|
|
|
+ })
|
|
|
+});
|
|
|
+
|
|
|
+// 认证请求Schema
|
|
|
+const CertificationRequestSchema = z.object({
|
|
|
+ certificationStatus: z.coerce.number()
|
|
|
+ .int()
|
|
|
+ .min(0)
|
|
|
+ .max(3)
|
|
|
+ .openapi({
|
|
|
+ description: '认证状态:0-未认证,1-认证中,2-已认证,3-已拒绝',
|
|
|
+ example: 2
|
|
|
+ }),
|
|
|
+ certificationInfo: z.string()
|
|
|
+ .nullable()
|
|
|
+ .optional()
|
|
|
+ .openapi({
|
|
|
+ description: '认证信息或拒绝原因',
|
|
|
+ example: '已审核通过,具备相关资质'
|
|
|
+ })
|
|
|
+});
|
|
|
+
|
|
|
+// 响应Schema
|
|
|
+const CertificationResponseSchema = z.object({
|
|
|
+ id: z.number().int().positive().openapi({ description: '用户资料ID', example: 1 }),
|
|
|
+ realName: z.string().openapi({ description: '真实姓名', example: '张三' }),
|
|
|
+ certificationStatus: z.number().openapi({ description: '认证状态', example: 2 }),
|
|
|
+ certificationInfo: z.string().nullable().openapi({ description: '认证信息', example: '已审核通过' }),
|
|
|
+ updatedAt: z.date().openapi({ description: '更新时间', example: '2024-01-01T00:00:00Z' }),
|
|
|
+ updatedBy: z.number().nullable().openapi({ description: '更新人ID', example: 1 })
|
|
|
+});
|
|
|
+
|
|
|
+// 路由定义
|
|
|
+const routeDef = createRoute({
|
|
|
+ method: 'put',
|
|
|
+ path: '/{id}/certification',
|
|
|
+ middleware: [authMiddleware],
|
|
|
+ request: {
|
|
|
+ params: CertificationParamsSchema,
|
|
|
+ body: {
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: CertificationRequestSchema
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ responses: {
|
|
|
+ 200: {
|
|
|
+ description: '认证状态更新成功',
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: CertificationResponseSchema
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 400: {
|
|
|
+ description: '请求参数错误',
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: ErrorSchema
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 404: {
|
|
|
+ description: '用户资料不存在',
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: ErrorSchema
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 500: {
|
|
|
+ description: '服务器错误',
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: ErrorSchema
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 路由实现
|
|
|
+const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
|
|
|
+ try {
|
|
|
+ const { id } = c.req.valid('param');
|
|
|
+ const { certificationStatus, certificationInfo } = await c.req.json();
|
|
|
+ const userId = c.get('user')?.id;
|
|
|
+
|
|
|
+ const repository = AppDataSource.getRepository(SilverUserProfile);
|
|
|
+
|
|
|
+ // 查找用户资料
|
|
|
+ const profile = await repository.findOne({
|
|
|
+ where: { id: parseInt(id) },
|
|
|
+ relations: ['user']
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!profile) {
|
|
|
+ return c.json({ code: 404, message: '用户资料不存在' }, 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新认证状态
|
|
|
+ profile.certificationStatus = certificationStatus;
|
|
|
+ profile.certificationInfo = certificationInfo || null;
|
|
|
+ profile.updatedBy = userId || null;
|
|
|
+
|
|
|
+ await repository.save(profile);
|
|
|
+
|
|
|
+ return c.json({
|
|
|
+ id: profile.id,
|
|
|
+ realName: profile.realName,
|
|
|
+ certificationStatus: profile.certificationStatus,
|
|
|
+ certificationInfo: profile.certificationInfo,
|
|
|
+ updatedAt: profile.updatedAt,
|
|
|
+ updatedBy: profile.updatedBy
|
|
|
+ }, 200);
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error('认证更新失败:', error);
|
|
|
+ const message = error instanceof Error ? error.message : '认证更新失败';
|
|
|
+ return c.json({ code: 500, message }, 500);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+export default app;
|