refactor: 拆分会话控制器模块

This commit is contained in:
que01 committed 2026-09-23 11:26:46 +08:00
1 parent 1ea757555d
commit 4c8bda57b1
3 files changed
+43 -35

No files matched your search

@@ -0,0 +1,39 @@
import { BadRequestException, Body, Controller, Post } from "@nestjs/common";
import { PrismaService } from "../../prisma.service.js";
import { WechatAuthService } from "../../wechat-auth.js";
import { AdminAuthService } from "../../admin-auth.service.js";
import { AdminLoginDto, WechatLoginDto } from "../../admin-auth.dto.js";
import { signIdentity } from "../../auth.js";
@Controller("customer/session")
export class CustomerSessionController {
constructor(
private readonly wechatAuth: WechatAuthService,
private readonly prisma: PrismaService,
) {}
@Post("wechat")
async wechatLogin(@Body() body: WechatLoginDto): Promise<{ token: string }> {
const session = await this.wechatAuth.exchangeCode(body.code);
const brand = await this.prisma.brandProfile.findFirst({ select: { id: true } });
if (!brand) throw new BadRequestException("品牌尚未初始化");
await this.prisma.customerUser.upsert({
where: { brandId_openid: { brandId: brand.id, openid: session.openid } },
update: { lastLoginAt: new Date() },
create: { brandId: brand.id, openid: session.openid },
});
return {
token: signIdentity({ subject: session.openid, brandId: brand.id, role: "customer" }),
};
}
}
@Controller("admin/session")
export class AdminSessionController {
constructor(private readonly auth: AdminAuthService) {}
@Post("login")
login(@Body() body: AdminLoginDto): Promise<{ token: string; displayName: string }> {
return this.auth.login(body.username, body.password);
}
}