feat(users): 拆分 C 端身份并迁移历史聊天

- digital.users: 独立开户登录与凭据版本校验,后台查看用户及原始会话
- 017迁移: 保留聊天消息、实验分组和人工指定期限,迁移历史账号归属
- tests: 覆盖身份隔离、凭据失效、会话归属和长账号登录,同步 README
This commit is contained in:
湛兮
2026-10-27 18:56:19 +08:00
parent 57c89f4f32
commit b9d1a7f6bd
14 changed files with 747 additions and 32 deletions
+10 -7
View File
@@ -1,5 +1,6 @@
/** 现有账号登录与密码接口,数字人入口同时接受管理员与员工。 */
import { authRepository } from "./auth.repository";
/** 后台/员工登录保持原体系,数字人入口只使用独立 C 端账号。 */
import { z } from "zod";
import { loginDigitalUser } from "../digital/digital.users";
import type { FastifyInstance } from "fastify";
import { env } from "../../config/env";
import { ok } from "../../shared/response";
@@ -10,11 +11,13 @@ import { authService } from "./auth.service";
/** 账号登录、当前用户与本人改密接口。 */
export async function authRoutes(app: FastifyInstance): Promise<void> {
app.post("/auth/digital/login", async (request) => {
const body = loginBodySchema.parse(request.body);
const admin = await authRepository.findByUsername(body.username);
const { user, payload } = admin
? await authService.loginManagement(body)
: await authService.loginEmployee(body);
// C 端开户支持120字符账号,不继承后台50字符限制。
const body = loginBodySchema
.extend({
username: z.string().trim().min(1).max(120),
})
.parse(request.body);
const { user, payload } = await loginDigitalUser(body);
return ok({
token: app.jwt.sign(payload),
tokenType: "Bearer",
+16 -2
View File
@@ -1,3 +1,8 @@
/** 管理员、员工与 C 端身份分别校验,禁止按同名账号跨体系回退。 */
import {
currentDigitalUser,
changeDigitalPassword,
} from "../digital/digital.users";
import { badRequest, forbidden, unauthorized } from "../../shared/http-error";
import { authRepository } from "./auth.repository";
import type {
@@ -190,6 +195,8 @@ export const authService = {
},
async getCurrentUser(payload: AuthJwtPayload): Promise<AuthUser> {
if (payload.accountType === "DIGITAL_USER")
return currentDigitalUser(payload);
if (payload.accountType === "SUPER_ADMIN" && payload.adminId) {
const admin = await authRepository.findActiveById(payload.adminId);
@@ -230,17 +237,24 @@ export const authService = {
input: { oldPassword: string; newPassword: string },
meta: { ip?: string | null; userAgent?: string | null },
): Promise<AuthUser> {
if (payload.accountType === "DIGITAL_USER")
return changeDigitalPassword(payload, input);
if (payload.accountType !== "EMPLOYEE" || !payload.employeeId) {
throw forbidden("超级管理员暂不通过员工端接口修改密码");
}
const employee = await authRepository.findActiveEmployeeById(payload.employeeId);
const employee = await authRepository.findActiveEmployeeById(
payload.employeeId,
);
if (!employee) {
throw unauthorized("登录已失效,请重新登录");
}
const passwordMatched = await verifyPassword(input.oldPassword, employee.passwordHash);
const passwordMatched = await verifyPassword(
input.oldPassword,
employee.passwordHash,
);
if (!passwordMatched) {
throw badRequest("旧密码不正确");
+5 -2
View File
@@ -1,8 +1,9 @@
/** 后台与独立 C 端身份声明,令牌用途与账号命名空间分别校验。 */
export const SUPER_ADMIN_STATUS = ["ACTIVE", "INACTIVE"] as const;
export type SuperAdminStatus = (typeof SUPER_ADMIN_STATUS)[number];
export type AuthAccountType = "SUPER_ADMIN" | "EMPLOYEE";
export type LoginScene = "MANAGEMENT" | "EMPLOYEE_APP";
export type AuthAccountType = "SUPER_ADMIN" | "EMPLOYEE" | "DIGITAL_USER";
export type LoginScene = "MANAGEMENT" | "EMPLOYEE_APP" | "DIGITAL_APP";
export interface LoginInput {
username: string;
@@ -63,6 +64,8 @@ export interface AuthJwtPayload {
scene: LoginScene;
adminId?: number;
employeeId?: number;
digitalUserId?: number;
sessionVersion?: number;
username: string;
roleCodes: string[];
permissions: string[];