This commit is contained in:
2026-06-30 10:33:56 +08:00
commit 6e047dc0a5
607 changed files with 65966 additions and 0 deletions
@@ -0,0 +1,89 @@
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';
import { 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';
import { ClientApp } from '@dukang/shared-types';
@Controller()
export class UserAuthController {
constructor(private readonly authService: AuthService) {}
@Post('auth/sms/send')
sendSms(@Body() dto: SendSmsDto) {
return this.authService.sendSms(dto.phone, dto.scene);
}
@Post('auth/login/sms')
login(@Body() dto: LoginSmsDto) {
return this.authService.loginUser(dto.phone, dto.code, ClientApp.USER_H5);
}
@Post('auth/login/wechat')
wechatLogin() {
return this.authService.wechatDisabled();
}
@Post('auth/wechat/bind-phone')
bindPhone() {
return this.authService.wechatDisabled();
}
@Get('auth/me')
@UseGuards(JwtAuthGuard)
me(@CurrentUser() user: AuthUser) {
return this.authService.getMe(user.actorType, user.actorId);
}
}
@Controller('shop/auth')
export class ShopAuthController {
constructor(private readonly authService: AuthService) {}
@Post('sms/send')
sendSms(@Body() dto: SendSmsDto) {
return this.authService.sendSms(dto.phone, dto.scene);
}
@Post('login/sms')
login(@Body() dto: LoginSmsDto) {
return this.authService.loginStore(dto.phone, dto.code, ClientApp.SHOP_H5);
}
@Post('login/wechat')
wechatLogin() {
return this.authService.wechatDisabled();
}
}
@Controller('partner/auth')
export class PartnerAuthController {
constructor(private readonly authService: AuthService) {}
@Post('sms/send')
sendSms(@Body() dto: SendSmsDto) {
return this.authService.sendSms(dto.phone, dto.scene);
}
@Post('login/sms')
login(@Body() dto: LoginSmsDto) {
return this.authService.loginPartner(dto.phone, dto.code, ClientApp.PARTNER_H5);
}
@Post('login/wechat')
wechatLogin() {
return this.authService.wechatDisabled();
}
}
@Controller('user')
export class UserProfileController {
constructor(private readonly authService: AuthService) {}
@Get('profile')
@UseGuards(JwtAuthGuard)
profile(@CurrentUser() user: AuthUser) {
return this.authService.getMe(user.actorType, user.actorId);
}
}
@@ -0,0 +1,145 @@
import {
BadRequestException,
Inject,
Injectable,
NotImplementedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ClientApp, SmsScene } from '@dukang/shared-types';
import { generateUserNo } from '@dukang/domain';
import { PrismaService } from '../../common/prisma/prisma.module';
import { SMS_PROVIDER } from '../../integrations/integrations.constants';
import { ISmsProvider } from '../../integrations/sms/sms.interface';
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
@Injectable()
export class AuthService {
constructor(
private readonly prisma: PrismaService,
private readonly jwtService: JwtService,
@Inject(SMS_PROVIDER) private readonly smsProvider: ISmsProvider,
) {}
async sendSms(phone: string, scene: string) {
await this.smsProvider.send(phone, scene);
return { sent: true };
}
async loginUser(phone: string, code: string, clientApp: ClientApp) {
await this.smsProvider.verify(phone, code, SmsScene.USER_LOGIN);
let user = await this.prisma.user.findUnique({ where: { phone } });
if (!user) {
user = await this.prisma.user.create({
data: {
phone,
userNo: generateUserNo(),
nickname: `用户${phone.slice(-4)}`,
},
});
await this.prisma.userCityPreference.create({
data: { userId: user.id, selectedCityCode: '410100', selectedDistrict: '郑州市' },
});
}
return this.issueToken('USER', user.id, clientApp, {
id: user.id.toString(),
userNo: user.userNo,
phone: user.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'),
nickname: user.nickname,
hasWechat: !!user.wxOpenId,
});
}
async loginStore(phone: string, code: string, clientApp: ClientApp) {
await this.smsProvider.verify(phone, code, SmsScene.STORE_LOGIN);
const account = await this.prisma.storeAccount.findUnique({
where: { phone },
include: { store: true },
});
if (!account) throw new BadRequestException('门店账号不存在');
await this.prisma.storeAccount.update({
where: { id: account.id },
data: { lastLoginAt: new Date() },
});
return this.issueToken('STORE', account.id, clientApp, undefined, {
id: account.id.toString(),
storeId: account.storeId.toString(),
name: account.name,
phone: account.phone,
storeName: account.store.name,
});
}
async loginPartner(phone: string, code: string, clientApp: ClientApp) {
await this.smsProvider.verify(phone, code, SmsScene.PARTNER_LOGIN);
const account = await this.prisma.partnerAccount.findUnique({
where: { phone },
include: { partner: true },
});
if (!account) throw new BadRequestException('合伙人账号不存在');
await this.prisma.partnerAccount.update({
where: { id: account.id },
data: { lastLoginAt: new Date() },
});
return this.issueToken('PARTNER', account.id, clientApp, undefined, undefined, {
id: account.id.toString(),
partnerId: account.partnerId.toString(),
name: account.name,
phone: account.phone,
isPrimary: account.isPrimary === 1,
companyName: account.partner.companyName,
});
}
async getMe(actorType: string, actorId: bigint) {
if (actorType === 'USER') {
const user = await this.prisma.user.findUnique({ where: { id: actorId } });
return serializeBigInt(user);
}
if (actorType === 'STORE') {
const account = await this.prisma.storeAccount.findUnique({
where: { id: actorId },
include: { store: true },
});
return serializeBigInt(account);
}
if (actorType === 'PARTNER') {
const account = await this.prisma.partnerAccount.findUnique({
where: { id: actorId },
include: { partner: true },
});
return serializeBigInt(account);
}
return null;
}
wechatDisabled() {
throw new NotImplementedException('FEATURE_DISABLED');
}
private issueToken(
actorType: string,
actorId: bigint,
clientApp: ClientApp,
user?: Record<string, unknown>,
store?: Record<string, unknown>,
partner?: Record<string, unknown>,
) {
const payload = {
sub: actorId.toString(),
actorType,
actorId: actorId.toString(),
clientApp,
};
const accessToken = this.jwtService.sign(payload);
const refreshToken = this.jwtService.sign(payload, { expiresIn: '30d' });
return {
accessToken,
refreshToken,
actorType,
actorId: actorId.toString(),
user,
store,
partner,
};
}
}
@@ -0,0 +1,21 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class SendSmsDto {
@IsString()
@IsNotEmpty()
phone: string;
@IsString()
@IsNotEmpty()
scene: string;
}
export class LoginSmsDto {
@IsString()
@IsNotEmpty()
phone: string;
@IsString()
@IsNotEmpty()
code: string;
}
@@ -0,0 +1,33 @@
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { IntegrationsModule } from '../../integrations/integrations.module';
import { AuthService } from './auth.service';
import {
PartnerAuthController,
ShopAuthController,
UserAuthController,
UserProfileController,
} from './auth.controller';
import { UserAddressController } from './user-address.controller';
import { UserAddressService } from './user-address.service';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
@Module({
imports: [
IntegrationsModule,
JwtModule.register({
secret: process.env.JWT_SECRET || 'dukang-prev1-dev-secret',
signOptions: { expiresIn: process.env.JWT_EXPIRES_IN || '7d' },
}),
],
controllers: [
UserAuthController,
ShopAuthController,
PartnerAuthController,
UserProfileController,
UserAddressController,
],
providers: [AuthService, UserAddressService, JwtAuthGuard],
exports: [AuthService, JwtModule, JwtAuthGuard],
})
export class IamModule {}
@@ -0,0 +1,34 @@
import { Body, Controller, Delete, Get, Param, Post, Put, UseGuards } from '@nestjs/common';
import { UserAddressService } from './user-address.service';
import { JwtAuthGuard, AuthUser } from '../../common/guards/jwt-auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
@Controller('user/addresses')
@UseGuards(JwtAuthGuard)
export class UserAddressController {
constructor(private readonly addressService: UserAddressService) {}
@Get()
list(@CurrentUser() user: AuthUser) {
return this.addressService.list(user.actorId);
}
@Post()
create(@CurrentUser() user: AuthUser, @Body() body: Record<string, unknown>) {
return this.addressService.create(user.actorId, body);
}
@Put(':id')
update(
@CurrentUser() user: AuthUser,
@Param('id') id: string,
@Body() body: Record<string, unknown>,
) {
return this.addressService.update(user.actorId, BigInt(id), body);
}
@Delete(':id')
remove(@CurrentUser() user: AuthUser, @Param('id') id: string) {
return this.addressService.remove(user.actorId, BigInt(id));
}
}
@@ -0,0 +1,64 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../common/prisma/prisma.module';
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
@Injectable()
export class UserAddressService {
constructor(private readonly prisma: PrismaService) {}
async list(userId: bigint) {
const list = await this.prisma.userAddress.findMany({
where: { userId },
orderBy: [{ isDefault: 'desc' }, { updatedAt: 'desc' }],
});
return serializeBigInt(list);
}
async create(userId: bigint, body: Record<string, unknown>) {
const isDefault = body.isDefault ? 1 : 0;
if (isDefault) {
await this.prisma.userAddress.updateMany({ where: { userId }, data: { isDefault: 0 } });
}
const address = await this.prisma.userAddress.create({
data: {
userId,
receiverName: String(body.receiverName),
phone: String(body.phone),
province: String(body.province),
city: String(body.city),
district: String(body.district),
detail: String(body.detail),
isDefault,
},
});
return serializeBigInt(address);
}
async update(userId: bigint, id: bigint, body: Record<string, unknown>) {
const existing = await this.prisma.userAddress.findFirst({ where: { id, userId } });
if (!existing) throw new NotFoundException('地址不存在');
if (body.isDefault) {
await this.prisma.userAddress.updateMany({ where: { userId }, data: { isDefault: 0 } });
}
const address = await this.prisma.userAddress.update({
where: { id },
data: {
receiverName: body.receiverName ? String(body.receiverName) : undefined,
phone: body.phone ? String(body.phone) : undefined,
province: body.province ? String(body.province) : undefined,
city: body.city ? String(body.city) : undefined,
district: body.district ? String(body.district) : undefined,
detail: body.detail ? String(body.detail) : undefined,
isDefault: body.isDefault ? 1 : undefined,
},
});
return serializeBigInt(address);
}
async remove(userId: bigint, id: bigint) {
const existing = await this.prisma.userAddress.findFirst({ where: { id, userId } });
if (!existing) throw new NotFoundException('地址不存在');
await this.prisma.userAddress.delete({ where: { id } });
return { deleted: true };
}
}