超管用户登录;

代码核销功能
This commit is contained in:
2026-07-06 22:33:30 +08:00
parent d708a10095
commit a66207ffa9
8 changed files with 203 additions and 38 deletions
@@ -0,0 +1,20 @@
import { randomBytes, scryptSync, timingSafeEqual } from 'crypto';
const SALT_LEN = 16;
const KEY_LEN = 64;
export function hashPassword(password: string): string {
const salt = randomBytes(SALT_LEN);
const hash = scryptSync(password, salt, KEY_LEN);
return `${salt.toString('hex')}:${hash.toString('hex')}`;
}
export function verifyPassword(password: string, stored: string): boolean {
const [saltHex, hashHex] = stored.split(':');
if (!saltHex || !hashHex) return false;
const salt = Buffer.from(saltHex, 'hex');
const expected = Buffer.from(hashHex, 'hex');
const actual = scryptSync(password, salt, expected.length);
if (actual.length !== expected.length) return false;
return timingSafeEqual(actual, expected);
}
@@ -1,6 +1,6 @@
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LoginSmsDto, SendSmsDto } from './dto/auth.dto';
import { LoginPasswordDto, LoginSmsDto, SendSmsDto } 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';
@@ -20,6 +20,11 @@ export class AdminAuthController {
return this.authService.loginHq(dto.phone, dto.code, ClientApp.HQ_WEB);
}
@Post('login/password')
loginPassword(@Body() dto: LoginPasswordDto) {
return this.authService.loginHqPassword(dto.loginName, dto.password, ClientApp.HQ_WEB);
}
@Get('me')
@UseGuards(JwtAuthGuard)
me(@CurrentUser() user: AuthUser) {
@@ -19,6 +19,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 { AnalyticsService } from '../analytics/analytics.service';
import { UserAddressService } from './user-address.service';
@@ -450,6 +451,30 @@ export class AuthService {
});
}
async loginHqPassword(loginName: string, password: string, clientApp: ClientApp) {
const normalizedLogin = loginName.trim();
if (!normalizedLogin) throw new BadRequestException('请输入账号');
const account = await this.prisma.hqAccount.findUnique({
where: { loginName: normalizedLogin },
});
if (!account?.passwordHash) throw new BadRequestException('账号或密码错误');
if (account.status !== 'ACTIVE') throw new BadRequestException('账号已停用');
if (!verifyPassword(password, account.passwordHash)) {
throw new BadRequestException('账号或密码错误');
}
await this.prisma.hqAccount.update({
where: { id: account.id },
data: { lastLoginAt: new Date() },
});
return this.issueToken('HQ', account.id, clientApp, false, undefined, undefined, undefined, undefined, {
id: account.id.toString(),
phone: account.phone,
name: account.name,
adminRole: account.adminRole,
status: account.status,
});
}
async getMe(actorType: string, actorId: bigint) {
if (actorType === 'USER') {
const user = await this.assertActiveUser(actorId);
@@ -69,6 +69,16 @@ export class BindWechatPhoneDto {
code: string;
}
export class LoginPasswordDto {
@IsString()
@IsNotEmpty()
loginName: string;
@IsString()
@IsNotEmpty()
password: string;
}
export class BindWechatDto {
@IsString()
@IsOptional()