|
@@ -7,7 +7,7 @@ import { AppDataSource } from '../../data-source';
|
|
|
import { AuthContext } from '../../types/context';
|
|
import { AuthContext } from '../../types/context';
|
|
|
import { UserListResponse } from '../../modules/users/user.schema';
|
|
import { UserListResponse } from '../../modules/users/user.schema';
|
|
|
|
|
|
|
|
-const userService = new UserService(AppDataSource);
|
|
|
|
|
|
|
+// UserService实例将在路由处理函数中创建
|
|
|
|
|
|
|
|
const PaginationQuery = z.object({
|
|
const PaginationQuery = z.object({
|
|
|
page: z.coerce.number().int().positive().default(1).openapi({
|
|
page: z.coerce.number().int().positive().default(1).openapi({
|
|
@@ -62,12 +62,18 @@ const listUsersRoute = createRoute({
|
|
|
const app = new OpenAPIHono<AuthContext>().openapi(listUsersRoute, async (c) => {
|
|
const app = new OpenAPIHono<AuthContext>().openapi(listUsersRoute, async (c) => {
|
|
|
try {
|
|
try {
|
|
|
const { page, pageSize, keyword } = c.req.valid('query');
|
|
const { page, pageSize, keyword } = c.req.valid('query');
|
|
|
- const [users, total] = await userService.getUsersWithPagination({
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 在函数内部创建UserService实例
|
|
|
|
|
+ const userService = new UserService(AppDataSource);
|
|
|
|
|
+ const result = await userService.getUsersWithPagination({
|
|
|
page,
|
|
page,
|
|
|
pageSize,
|
|
pageSize,
|
|
|
keyword
|
|
keyword
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 确保结果是数组格式
|
|
|
|
|
+ const [users, total] = Array.isArray(result) ? result : [result, 0];
|
|
|
|
|
+
|
|
|
return c.json({
|
|
return c.json({
|
|
|
data: users,
|
|
data: users,
|
|
|
pagination: {
|
|
pagination: {
|
|
@@ -78,7 +84,11 @@ const app = new OpenAPIHono<AuthContext>().openapi(listUsersRoute, async (c) =>
|
|
|
}, 200);
|
|
}, 200);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
if (error instanceof z.ZodError) {
|
|
if (error instanceof z.ZodError) {
|
|
|
- return c.json({ code: 400, message: '参数错误' }, 400);
|
|
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 400,
|
|
|
|
|
+ message: '参数错误',
|
|
|
|
|
+ errors: error.issues
|
|
|
|
|
+ }, 400);
|
|
|
}
|
|
}
|
|
|
return c.json({
|
|
return c.json({
|
|
|
code: 500,
|
|
code: 500,
|