Files
certd/packages/ui/certd-server/src/controller/user/mine/mine-controller.ts
T

116 lines
4.2 KiB
TypeScript
Raw Normal View History

import { BaseController, Constants, SysSettingsService } from '@certd/lib-server';
2026-03-12 18:11:02 +08:00
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { PasskeyService } from '../../../modules/login/service/passkey-service.js';
2025-01-15 01:05:34 +08:00
import { RoleService } from '../../../modules/sys/authority/service/role-service.js';
2026-03-12 18:11:02 +08:00
import { UserService } from '../../../modules/sys/authority/service/user-service.js';
2026-03-15 14:01:34 +08:00
import { ApiTags } from '@midwayjs/swagger';
import { CodeService } from '../../../modules/basic/service/code-service.js';
2023-06-28 23:15:37 +08:00
/**
*/
@Provide()
@Controller('/api/mine')
2026-03-15 14:01:34 +08:00
@ApiTags(['mine'])
2023-06-28 23:15:37 +08:00
export class MineController extends BaseController {
@Inject()
userService: UserService;
2026-03-12 18:11:02 +08:00
2024-10-03 01:29:12 +08:00
@Inject()
roleService: RoleService;
2026-03-12 18:11:02 +08:00
@Inject()
passkeyService: PasskeyService;
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
2026-03-12 18:11:02 +08:00
@Post('/info', { description: Constants.per.authOnly, summary: '查询用户信息' })
2023-06-28 23:15:37 +08:00
public async info() {
const userId = this.getUserId();
const user = await this.userService.info(userId);
2024-11-30 22:35:26 +08:00
const isWeak = await this.userService.checkPassword('123456', user.password, user.passwordVersion);
if (isWeak) {
//@ts-ignore
user.isWeak = true;
}
2024-10-03 01:29:12 +08:00
user.roleIds = await this.roleService.getRoleIdsByUserId(userId);
2023-06-28 23:15:37 +08:00
delete user.password;
return this.ok(user);
}
@Post('/changePassword', { description: Constants.per.authOnly, summary: '修改密码' })
2023-06-28 23:15:37 +08:00
public async changePassword(@Body(ALL) body: any) {
const userId = this.getUserId();
await this.userService.changePassword(userId, body);
return this.ok({});
}
2025-07-01 16:30:07 +08:00
@Post('/updateProfile', { description: Constants.per.authOnly, summary: '更新用户资料' })
2025-07-01 16:30:07 +08:00
public async updateProfile(@Body(ALL) body: any) {
const userId = this.getUserId();
2025-07-01 16:30:07 +08:00
await this.userService.updateProfile(userId, {
avatar: body.avatar,
nickName: body.nickName,
});
return this.ok({});
}
@Post('/contact/capability', { description: Constants.per.authOnly, summary: '查询联系方式绑定能力' })
public async contactCapability() {
const settings = await this.sysSettingsService.getPrivateSettings();
return this.ok({
smsEnabled: !!settings.sms?.config?.accessId,
});
}
@Post('/contact/verifyIdentity', { description: Constants.per.authOnly, summary: '验证本人操作' })
public async verifyContactIdentity(@Body(ALL) body: { identityType: 'password' | 'email' | 'mobile'; identityPassword?: string; identityValidateCode?: string }) {
const userId = this.getUserId();
await this.userService.verifyIdentity(userId, body, this.codeService);
const validationCode = this.codeService.setValidationValue({
type: 'contactIdentity',
userId,
identityType: body.identityType,
});
return this.ok({ validationCode });
}
@Post('/contact/mobile', { description: Constants.per.authOnly, summary: '绑定或修改手机号' })
public async updateMobile(@Body(ALL) body: { phoneCode?: string; mobile: string; validateCode: string; identityValidationCode: string }) {
const userId = this.getUserId();
this.userService.checkContactIdentityValidation(userId, body.identityValidationCode, this.codeService);
await this.codeService.checkSmsCode({
mobile: body.mobile,
phoneCode: body.phoneCode || '86',
smsCode: body.validateCode,
verificationType: 'bindMobile',
throwError: true,
});
await this.userService.updateMobile(userId, {
phoneCode: body.phoneCode,
mobile: body.mobile,
});
return this.ok({});
}
@Post('/contact/email', { description: Constants.per.authOnly, summary: '绑定或修改邮箱' })
public async updateEmail(@Body(ALL) body: { email: string; validateCode: string; identityValidationCode: string }) {
const userId = this.getUserId();
this.userService.checkContactIdentityValidation(userId, body.identityValidationCode, this.codeService);
this.codeService.checkEmailCode({
email: body.email,
validateCode: body.validateCode,
verificationType: 'bindEmail',
throwError: true,
});
await this.userService.updateEmail(userId, {
email: body.email,
});
return this.ok({});
}
2023-06-28 23:15:37 +08:00
}