|
|
@@ -4,6 +4,8 @@ import { MiniAuthService } from '../../../modules/auth/mini-auth.service';
|
|
|
import { AppDataSource } from '../../../data-source';
|
|
|
import { ErrorSchema } from '../../../utils/errorHandler';
|
|
|
import { UserEntity } from '../../../modules/users/user.entity';
|
|
|
+import { UserResponseSchema } from '../../../modules/users/user.schema';
|
|
|
+import { parseWithAwait } from '../../../utils/parseWithAwait';
|
|
|
|
|
|
const MiniLoginSchema = z.object({
|
|
|
code: z.string().openapi({
|
|
|
@@ -21,15 +23,7 @@ const MiniLoginResponseSchema = z.object({
|
|
|
example: 'jwt.token.here',
|
|
|
description: 'JWT Token'
|
|
|
}),
|
|
|
- user: z.object({
|
|
|
- id: z.number(),
|
|
|
- username: z.string(),
|
|
|
- nickname: z.string().nullable(),
|
|
|
- phone: z.string().nullable(),
|
|
|
- email: z.string().nullable(),
|
|
|
- avatarFileId: z.number().nullable(),
|
|
|
- registrationSource: z.string()
|
|
|
- }),
|
|
|
+ user: UserResponseSchema,
|
|
|
isNewUser: z.boolean().openapi({
|
|
|
example: true,
|
|
|
description: '是否为新注册用户'
|
|
|
@@ -102,19 +96,27 @@ const app = new OpenAPIHono().openapi(miniLoginRoute, async (c) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return c.json({
|
|
|
+ // 获取完整的用户信息(包含所有字段)
|
|
|
+ const fullUser = await AppDataSource.getRepository(UserEntity).findOne({
|
|
|
+ where: { id: result.user.id },
|
|
|
+ relations: ['avatarFile', 'roles']
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!fullUser) {
|
|
|
+ return c.json({ code: 500, message: '用户信息获取失败' }, 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建用户响应数据
|
|
|
+ const userResponseData = {
|
|
|
token: result.token,
|
|
|
- user: {
|
|
|
- id: result.user.id,
|
|
|
- username: result.user.username,
|
|
|
- nickname: result.user.nickname,
|
|
|
- phone: result.user.phone,
|
|
|
- email: result.user.email,
|
|
|
- avatarFileId: result.user.avatarFileId,
|
|
|
- registrationSource: result.user.registrationSource
|
|
|
- },
|
|
|
+ user: fullUser,
|
|
|
isNewUser: result.isNewUser
|
|
|
- }, 200);
|
|
|
+ };
|
|
|
+
|
|
|
+ // 使用parseWithAwait处理异步字段
|
|
|
+ const validatedResponse = await parseWithAwait(MiniLoginResponseSchema, userResponseData);
|
|
|
+
|
|
|
+ return c.json(validatedResponse, 200);
|
|
|
} catch (error) {
|
|
|
const { code = 500, message = '登录失败' } = error as Error & { code?: number };
|
|
|
return c.json({ code, message }, 500);
|