|
|
@@ -17,11 +17,23 @@ const UpdateParams = z.object({
|
|
|
})
|
|
|
});
|
|
|
|
|
|
-const UpdateUserSchema = UserSchema.omit({
|
|
|
- id: true,
|
|
|
- createdAt: true,
|
|
|
- updatedAt: true
|
|
|
-}).partial();
|
|
|
+const UpdateUserSchema = z.object({
|
|
|
+ username: UserSchema.shape.username.optional(),
|
|
|
+ password: UserSchema.shape.password.optional(),
|
|
|
+ phone: UserSchema.shape.phone.optional(),
|
|
|
+ email: UserSchema.shape.email.optional(),
|
|
|
+ nickname: UserSchema.shape.nickname.optional(),
|
|
|
+ name: UserSchema.shape.name.optional(),
|
|
|
+ avatar: UserSchema.shape.avatar.optional(),
|
|
|
+ isDisabled: UserSchema.shape.isDisabled.optional(),
|
|
|
+ isDeleted: UserSchema.shape.isDeleted.optional(),
|
|
|
+ defaultDepartmentId: z.number().int().positive().nullable().optional(),
|
|
|
+ dataScopeType: z.enum(['PERSONAL', 'DEPARTMENT', 'SUB_DEPARTMENT', 'COMPANY', 'CUSTOM']).optional(),
|
|
|
+ roleIds: z.array(z.number().int().positive()).optional().openapi({
|
|
|
+ description: '角色ID列表',
|
|
|
+ example: [1, 2]
|
|
|
+ }),
|
|
|
+});
|
|
|
|
|
|
const routeDef = createRoute({
|
|
|
method: 'put',
|
|
|
@@ -61,15 +73,36 @@ const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
|
|
|
try {
|
|
|
const { id } = c.req.valid('param');
|
|
|
const data = c.req.valid('json');
|
|
|
- const user = await userService.updateUser(id, data);
|
|
|
+ const { roleIds, ...userData } = data;
|
|
|
+
|
|
|
+ // 转换数据类型
|
|
|
+ const userUpdateData: any = { ...userData };
|
|
|
+ if (userUpdateData.defaultDepartmentId === null) {
|
|
|
+ delete userUpdateData.defaultDepartmentId;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新用户基本信息
|
|
|
+ const user = await userService.updateUser(id, userUpdateData);
|
|
|
if (!user) {
|
|
|
return c.json({ code: 404, message: '用户不存在' }, 404);
|
|
|
}
|
|
|
- return c.json(user, 200);
|
|
|
+
|
|
|
+ // 更新角色
|
|
|
+ if (roleIds !== undefined) {
|
|
|
+ await userService.assignRoles(id, roleIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重新获取用户(包含角色信息)
|
|
|
+ const userWithRoles = await userService.getUserById(id);
|
|
|
+ if (!userWithRoles) {
|
|
|
+ return c.json({ code: 404, message: '用户不存在' }, 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ return c.json(userWithRoles, 200);
|
|
|
} catch (error) {
|
|
|
- return c.json({
|
|
|
- code: 500,
|
|
|
- message: error instanceof Error ? error.message : '更新用户失败'
|
|
|
+ return c.json({
|
|
|
+ code: 500,
|
|
|
+ message: error instanceof Error ? error.message : '更新用户失败'
|
|
|
}, 500);
|
|
|
}
|
|
|
});
|