本文档描述了 188-179 招聘系统的 API 设计规范。系统采用 RESTful API 设计风格,使用 Hono 框架实现,并通过 @hono/zod-openapi 自动生成 OpenAPI 文档。
web/server.js (Hono应用)
├── /api/v1/
│ ├── /auth # 认证相关
│ ├── /users # 用户管理
│ ├── /orders # 订单管理
│ ├── /companies # 企业管理
│ ├── /platforms # 平台管理
│ ├── /channels # 渠道管理
│ └── /files # 文件管理
└── /ui # Swagger UI
Content-Type: application/json
Authorization: Bearer <token>
{
"id": 1,
"username": "admin",
"email": "admin@example.com"
}
{
"code": 400,
"message": "参数错误"
}
{
"code": "VALIDATION_ERROR",
"message": "参数验证失败",
"errors": [
{
"path": ["username"],
"message": "用户名不能为空"
}
]
}
| 状态码 | 含义 | 使用场景 |
|---|---|---|
| 200 | OK | 操作成功 |
| 201 | Created | 创建成功 |
| 400 | Bad Request | 请求参数错误 |
| 401 | Unauthorized | 未认证 |
| 403 | Forbidden | 权限不足 |
| 404 | Not Found | 资源不存在 |
| 500 | Internal Server Error | 服务器错误 |
GET /api/v1/users?page=1&pageSize=10&keyword=搜索词
GET /api/v1/users/:id
POST /api/v1/users
Content-Type: application/json
{
"username": "newuser",
"email": "user@example.com",
"password": "password123"
}
PUT /api/v1/users/:id
Content-Type: application/json
{
"email": "newemail@example.com"
}
DELETE /api/v1/users/:id
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| page | number | 1 | 页码 |
| pageSize | number | 10 | 每页数量 |
| keyword | string | - | 搜索关键词 |
| sortBy | string | createTime | 排序字段 |
| sortOrder | string | DESC | 排序方向 (ASC/DESC) |
{
"data": [...],
"pagination": {
"total": 100,
"current": 1,
"pageSize": 10,
"totalPages": 10
}
}
http://localhost:8080/ui
import { z } from '@hono/zod-openapi';
// 用户Schema
export const UserSchema = z.object({
id: z.number().int().positive().openapi({
description: '用户ID',
example: 1
}),
username: z.string().max(50).openapi({
description: '用户名',
example: 'admin'
}),
email: z.string().email().nullable().openapi({
description: '邮箱',
example: 'admin@example.com'
}),
createTime: z.coerce.date<Date>().openapi({
description: '创建时间'
}),
updateTime: z.coerce.date<Date>().openapi({
description: '更新时间'
})
});
// 创建用户DTO
export const CreateUserSchema = z.object({
username: z.string().min(1).max(50).openapi({
description: '用户名',
example: 'newuser'
}),
email: z.string().email().optional().openapi({
description: '邮箱',
example: 'user@example.com'
}),
password: z.string().min(6).openapi({
description: '密码',
example: 'password123'
})
});
import { OpenAPIHono, createRoute } from '@hono/zod-openapi';
import { z } from 'zod';
const app = new OpenAPIHono();
// 定义路由
const getUserRoute = createRoute({
method: 'get',
path: '/users/{id}',
request: {
params: z.object({
id: z.string().openapi({
description: '用户ID',
example: '1'
})
})
},
responses: {
200: {
description: '获取成功',
content: {
'application/json': { schema: UserSchema }
}
},
404: {
description: '用户不存在',
content: {
'application/json': { schema: ErrorSchema }
}
}
}
});
// 实现路由
app.openapi(getUserRoute, async (c) => {
const { id } = c.req.valid('param');
const user = await userService.findById(Number(id));
if (!user) {
return c.json({ code: 404, message: '用户不存在' }, 404);
}
return c.json(user, 200);
});
UI包通过 hc 客户端获得类型安全的API调用:
import { hc } from 'hono/client';
import type AppRoutes from '@d8d/server';
const client = hc<AppRoutes>('/api/v1');
// 完全类型安全
const result = await client.users.$get({
query: { page: 1, pageSize: 10 }
});
const user = await client.users[':id'].$get({
param: { id: '1' }
});
POST /api/v1/auth/login
Content-Type: application/json
{
"username": "admin",
"password": "password123"
}
响应:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"username": "admin"
}
}
GET /api/v1/users
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
// 简单错误
return c.json({
code: 404,
message: '用户不存在'
}, 404);
// Zod验证错误
if (error instanceof z.ZodError) {
return c.json({
code: 'VALIDATION_ERROR',
message: '参数验证失败',
errors: error.errors
}, 400);
}
/api/v1/users # 当前版本
/api/v2/users # 未来版本
文档版本: 1.0 最后更新: 2026-01-07