feat: 增加员工端工作台后端能力

This commit is contained in:
湛兮
2026-06-02 12:23:00 +08:00
parent 667dc411fc
commit 98cea63203
33 changed files with 3021 additions and 18 deletions
+11 -1
View File
@@ -2,7 +2,7 @@ import type { FastifyInstance } from "fastify";
import { env } from "../../config/env";
import { ok } from "../../shared/response";
import { authGuard } from "./auth.guard";
import { loginBodySchema } from "./auth.schema";
import { loginBodySchema, updateOwnPasswordBodySchema } from "./auth.schema";
import { authService } from "./auth.service";
export async function authRoutes(app: FastifyInstance): Promise<void> {
@@ -50,4 +50,14 @@ export async function authRoutes(app: FastifyInstance): Promise<void> {
return ok(user);
});
app.patch("/auth/me/password", { preHandler: authGuard }, async (request) => {
const body = updateOwnPasswordBodySchema.parse(request.body);
const user = await authService.updateOwnPassword(request.user, body, {
ip: request.ip,
userAgent: request.headers["user-agent"] ?? null,
});
return ok(user);
});
}
+22 -3
View File
@@ -26,6 +26,7 @@ interface EmployeeLoginRow extends RowDataPacket {
store_id: number;
store_name: string;
store_status: "ACTIVE" | "INACTIVE";
must_change_password: 0 | 1 | null;
}
interface EmployeeRoleRow extends RowDataPacket {
@@ -123,9 +124,11 @@ export const authRepository = {
e.status,
e.store_id,
s.name AS store_name,
s.status AS store_status
s.status AS store_status,
eps.must_change_password
FROM employees e
INNER JOIN stores s ON s.id = e.store_id
LEFT JOIN employee_password_states eps ON eps.employee_id = e.id
WHERE e.phone = ?
AND e.status = 'ACTIVE'
AND e.deleted_at IS NULL
@@ -160,9 +163,11 @@ export const authRepository = {
e.status,
e.store_id,
s.name AS store_name,
s.status AS store_status
s.status AS store_status,
eps.must_change_password
FROM employees e
INNER JOIN stores s ON s.id = e.store_id
LEFT JOIN employee_password_states eps ON eps.employee_id = e.id
WHERE e.phone = ?
AND e.deleted_at IS NULL
AND s.deleted_at IS NULL
@@ -195,9 +200,11 @@ export const authRepository = {
e.status,
e.store_id,
s.name AS store_name,
s.status AS store_status
s.status AS store_status,
eps.must_change_password
FROM employees e
INNER JOIN stores s ON s.id = e.store_id
LEFT JOIN employee_password_states eps ON eps.employee_id = e.id
WHERE e.id = ?
AND e.status = 'ACTIVE'
AND e.deleted_at IS NULL
@@ -230,6 +237,17 @@ export const authRepository = {
);
},
async updateEmployeePasswordHash(id: number, passwordHash: string): Promise<void> {
await pool.execute(
`
UPDATE employees
SET password_hash = ?
WHERE id = ? AND deleted_at IS NULL
`,
[passwordHash, id],
);
},
async findRolesByEmployeeIds(
employeeIds: number[],
): Promise<Map<number, EmployeeLoginAccount["roles"]>> {
@@ -280,6 +298,7 @@ function toEmployeeLoginAccount(
storeId: row.store_id,
storeName: row.store_name,
storeStatus: row.store_status,
mustChangePassword: row.must_change_password === 1,
roles,
};
}
+5
View File
@@ -4,3 +4,8 @@ export const loginBodySchema = z.object({
username: z.string().trim().min(1).max(50),
password: z.string().min(8).max(128),
});
export const updateOwnPasswordBodySchema = z.object({
oldPassword: z.string().min(8).max(128),
newPassword: z.string().min(8).max(128),
});
+42 -2
View File
@@ -1,4 +1,4 @@
import { unauthorized } from "../../shared/http-error";
import { badRequest, forbidden, unauthorized } from "../../shared/http-error";
import { authRepository } from "./auth.repository";
import type {
AuthJwtPayload,
@@ -8,7 +8,8 @@ import type {
LoginScene,
SuperAdmin,
} from "./auth.types";
import { verifyPassword } from "./password";
import { hashPassword, verifyPassword } from "./password";
import { credentialService } from "../credentials/credential.service";
import { permissionService } from "../permissions/permission.service";
async function toAuthUser(admin: SuperAdmin): Promise<AuthUser> {
@@ -54,6 +55,7 @@ async function toEmployeeAuthUser(
roles: employee.roles,
permissions,
canManage: permissions.some((permission) => permission.endsWith(":manage")),
mustChangePassword: employee.mustChangePassword,
};
}
@@ -212,4 +214,42 @@ export const authService = {
throw unauthorized("登录已失效,请重新登录");
},
async getCurrentEmployeeUser(payload: AuthJwtPayload): Promise<AuthUser> {
const user = await this.getCurrentUser(payload);
if (user.accountType !== "EMPLOYEE") {
throw forbidden("只有员工账号可以访问员工端接口");
}
return user;
},
async updateOwnPassword(
payload: AuthJwtPayload,
input: { oldPassword: string; newPassword: string },
meta: { ip?: string | null; userAgent?: string | null },
): Promise<AuthUser> {
if (payload.accountType !== "EMPLOYEE" || !payload.employeeId) {
throw forbidden("超级管理员暂不通过员工端接口修改密码");
}
const employee = await authRepository.findActiveEmployeeById(payload.employeeId);
if (!employee) {
throw unauthorized("登录已失效,请重新登录");
}
const passwordMatched = await verifyPassword(input.oldPassword, employee.passwordHash);
if (!passwordMatched) {
throw badRequest("旧密码不正确");
}
const passwordHash = await hashPassword(input.newPassword);
await authRepository.updateEmployeePasswordHash(employee.id, passwordHash);
const actor = await this.getCurrentEmployeeUser(payload);
await credentialService.recordOwnPasswordChanged(employee.id, actor, meta);
return this.getCurrentUser(payload);
},
};
+2
View File
@@ -32,6 +32,7 @@ export interface EmployeeLoginAccount {
storeId: number;
storeName: string;
storeStatus: "ACTIVE" | "INACTIVE";
mustChangePassword: boolean;
roles: Array<{
id: number;
code: string;
@@ -53,6 +54,7 @@ export interface AuthUser {
}>;
permissions: string[];
canManage: boolean;
mustChangePassword?: boolean;
}
export interface AuthJwtPayload {