@@ -1,6 +1,6 @@
|
||||
import { Body, Controller, Param, Put, UseGuards } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { SaveHqListColumnPrefsDto } from './dto/auth.dto';
|
||||
import { SaveHqListColumnPrefsDto, UpdateMyHqCredentialsDto } from './dto/auth.dto';
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
import { AuthUser } from '../../common/guards/jwt-auth.guard';
|
||||
@@ -18,4 +18,9 @@ export class AdminMeController {
|
||||
) {
|
||||
return this.authService.updateMyListColumnPrefs(user.actorType, user.actorId, listKey, dto);
|
||||
}
|
||||
|
||||
@Put('credentials')
|
||||
updateCredentials(@CurrentUser() user: AuthUser, @Body() dto: UpdateMyHqCredentialsDto) {
|
||||
return this.authService.updateMyHqCredentials(user.actorType, user.actorId, dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import type { SmsActorRef } from '../../integrations/sms/sms.interface';
|
||||
import { SmsCodeStore } from '../../integrations/sms/sms-code.store';
|
||||
import type { IWechatProvider } from '../../integrations/wechat/wechat.interface';
|
||||
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
||||
import { verifyPassword } from '../../common/crypto/password.util';
|
||||
import { verifyPassword, hashPassword } from '../../common/crypto/password.util';
|
||||
import { AnalyticsService } from '../analytics/analytics.service';
|
||||
import { UserAddressService } from './user-address.service';
|
||||
import { ResourceService } from '../common/resource.service';
|
||||
@@ -1287,6 +1287,50 @@ export class AuthService {
|
||||
return { listColumnPrefs: parseListColumnPrefs(updated.listColumnPrefs) };
|
||||
}
|
||||
|
||||
async updateMyHqCredentials(
|
||||
actorType: string,
|
||||
actorId: bigint,
|
||||
dto: { loginName?: string; oldPassword?: string; newPassword?: string },
|
||||
) {
|
||||
if (actorType !== 'HQ') throw new ForbiddenException('仅总部账号可修改');
|
||||
const account = await this.prisma.hqAccount.findUnique({ where: { id: actorId } });
|
||||
if (!account) throw new NotFoundException('账号不存在');
|
||||
|
||||
const data: { loginName?: string; passwordHash?: string } = {};
|
||||
const nextLogin = dto.loginName?.trim();
|
||||
if (nextLogin !== undefined) {
|
||||
if (!nextLogin) throw new BadRequestException('用户名不能为空');
|
||||
if (nextLogin !== account.loginName) {
|
||||
const dup = await this.prisma.hqAccount.findUnique({ where: { loginName: nextLogin } });
|
||||
if (dup && dup.id !== account.id) throw new BadRequestException('用户名已被占用');
|
||||
data.loginName = nextLogin;
|
||||
}
|
||||
}
|
||||
|
||||
if (dto.newPassword !== undefined) {
|
||||
const pwd = dto.newPassword.trim();
|
||||
if (pwd.length < 6) throw new BadRequestException('新密码至少 6 位');
|
||||
if (account.passwordHash) {
|
||||
if (!dto.oldPassword?.trim()) throw new BadRequestException('请输入当前密码');
|
||||
if (!verifyPassword(dto.oldPassword.trim(), account.passwordHash)) {
|
||||
throw new BadRequestException('当前密码不正确');
|
||||
}
|
||||
}
|
||||
data.passwordHash = hashPassword(pwd);
|
||||
}
|
||||
|
||||
if (!Object.keys(data).length) {
|
||||
throw new BadRequestException('请填写要修改的内容');
|
||||
}
|
||||
|
||||
const updated = await this.prisma.hqAccount.update({
|
||||
where: { id: actorId },
|
||||
data,
|
||||
});
|
||||
const { passwordHash: _ph, ...safe } = updated;
|
||||
return serializeBigInt(safe);
|
||||
}
|
||||
|
||||
wechatDisabled() {
|
||||
throw new NotImplementedException('FEATURE_DISABLED');
|
||||
}
|
||||
|
||||
@@ -130,6 +130,22 @@ export class CheckPartnerPhoneDto {
|
||||
phone: string;
|
||||
}
|
||||
|
||||
export class UpdateMyHqCredentialsDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
loginName?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
oldPassword?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
newPassword?: string;
|
||||
}
|
||||
|
||||
export class SaveHqListColumnPrefsDto {
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
|
||||
Reference in New Issue
Block a user