feat: 完善员工门店软删除与密码管理
This commit is contained in:
@@ -22,8 +22,10 @@ interface EmployeeLoginRow extends RowDataPacket {
|
||||
username: string;
|
||||
password_hash: string;
|
||||
display_name: string;
|
||||
status: "ACTIVE" | "INACTIVE";
|
||||
store_id: number;
|
||||
store_name: string;
|
||||
store_status: "ACTIVE" | "INACTIVE";
|
||||
}
|
||||
|
||||
interface EmployeeRoleRow extends RowDataPacket {
|
||||
@@ -67,6 +69,22 @@ export const authRepository = {
|
||||
return rows[0] ? toSuperAdmin(rows[0]) : null;
|
||||
},
|
||||
|
||||
async findByUsername(
|
||||
username: string,
|
||||
): Promise<SuperAdminWithPassword | null> {
|
||||
const [rows] = await pool.execute<SuperAdminRow[]>(
|
||||
`
|
||||
SELECT id, username, password_hash, display_name, status, last_login_at, created_at, updated_at
|
||||
FROM super_admins
|
||||
WHERE username = ?
|
||||
LIMIT 1
|
||||
`,
|
||||
[username],
|
||||
);
|
||||
|
||||
return rows[0] ? toSuperAdmin(rows[0]) : null;
|
||||
},
|
||||
|
||||
async findActiveById(id: number): Promise<SuperAdminWithPassword | null> {
|
||||
const [rows] = await pool.execute<SuperAdminRow[]>(
|
||||
`
|
||||
@@ -102,8 +120,10 @@ export const authRepository = {
|
||||
e.phone AS username,
|
||||
e.password_hash,
|
||||
e.name AS display_name,
|
||||
e.status,
|
||||
e.store_id,
|
||||
s.name AS store_name
|
||||
s.name AS store_name,
|
||||
s.status AS store_status
|
||||
FROM employees e
|
||||
INNER JOIN stores s ON s.id = e.store_id
|
||||
WHERE e.phone = ?
|
||||
@@ -127,6 +147,41 @@ export const authRepository = {
|
||||
);
|
||||
},
|
||||
|
||||
async findEmployeeByPhoneForLogin(
|
||||
phone: string,
|
||||
): Promise<EmployeeLoginAccount | null> {
|
||||
const [rows] = await pool.execute<EmployeeLoginRow[]>(
|
||||
`
|
||||
SELECT
|
||||
e.id,
|
||||
e.phone AS username,
|
||||
e.password_hash,
|
||||
e.name AS display_name,
|
||||
e.status,
|
||||
e.store_id,
|
||||
s.name AS store_name,
|
||||
s.status AS store_status
|
||||
FROM employees e
|
||||
INNER JOIN stores s ON s.id = e.store_id
|
||||
WHERE e.phone = ?
|
||||
AND e.deleted_at IS NULL
|
||||
AND s.deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`,
|
||||
[phone],
|
||||
);
|
||||
|
||||
if (!rows[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rolesByEmployeeId = await this.findRolesByEmployeeIds([rows[0].id]);
|
||||
return toEmployeeLoginAccount(
|
||||
rows[0],
|
||||
rolesByEmployeeId.get(rows[0].id) ?? [],
|
||||
);
|
||||
},
|
||||
|
||||
async findActiveEmployeeById(
|
||||
id: number,
|
||||
): Promise<EmployeeLoginAccount | null> {
|
||||
@@ -137,8 +192,10 @@ export const authRepository = {
|
||||
e.phone AS username,
|
||||
e.password_hash,
|
||||
e.name AS display_name,
|
||||
e.status,
|
||||
e.store_id,
|
||||
s.name AS store_name
|
||||
s.name AS store_name,
|
||||
s.status AS store_status
|
||||
FROM employees e
|
||||
INNER JOIN stores s ON s.id = e.store_id
|
||||
WHERE e.id = ?
|
||||
@@ -189,6 +246,8 @@ export const authRepository = {
|
||||
FROM employee_roles er
|
||||
INNER JOIN roles r ON r.id = er.role_id
|
||||
WHERE er.employee_id IN (${placeholders})
|
||||
AND er.deleted_at IS NULL
|
||||
AND r.deleted_at IS NULL
|
||||
ORDER BY r.id ASC
|
||||
`,
|
||||
employeeIds,
|
||||
@@ -217,8 +276,10 @@ function toEmployeeLoginAccount(
|
||||
username: row.username,
|
||||
passwordHash: row.password_hash,
|
||||
displayName: row.display_name,
|
||||
status: row.status,
|
||||
storeId: row.store_id,
|
||||
storeName: row.store_name,
|
||||
storeStatus: row.store_status,
|
||||
roles,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ export const authService = {
|
||||
user: AuthUser;
|
||||
payload: AuthJwtPayload;
|
||||
}> {
|
||||
const admin = await authRepository.findActiveByUsername(input.username);
|
||||
const admin = await authRepository.findByUsername(input.username);
|
||||
|
||||
if (admin) {
|
||||
const passwordMatched = await verifyPassword(
|
||||
@@ -95,6 +95,10 @@ export const authService = {
|
||||
throw unauthorized("用户名或密码错误");
|
||||
}
|
||||
|
||||
if (admin.status === "INACTIVE") {
|
||||
throw unauthorized("账号已被禁用");
|
||||
}
|
||||
|
||||
await authRepository.updateLastLoginAt(admin.id);
|
||||
|
||||
const user = await toAuthUser(admin);
|
||||
@@ -105,7 +109,7 @@ export const authService = {
|
||||
};
|
||||
}
|
||||
|
||||
const employee = await authRepository.findActiveEmployeeByPhone(
|
||||
const employee = await authRepository.findEmployeeByPhoneForLogin(
|
||||
input.username,
|
||||
);
|
||||
|
||||
@@ -122,6 +126,14 @@ export const authService = {
|
||||
throw unauthorized("用户名或密码错误");
|
||||
}
|
||||
|
||||
if (employee.status === "INACTIVE") {
|
||||
throw unauthorized("账号已被禁用");
|
||||
}
|
||||
|
||||
if (employee.storeStatus === "INACTIVE") {
|
||||
throw unauthorized("所属门店已被禁用");
|
||||
}
|
||||
|
||||
await authRepository.updateEmployeeLastLoginAt(employee.id);
|
||||
|
||||
const user = await toEmployeeAuthUser(employee);
|
||||
@@ -140,7 +152,7 @@ export const authService = {
|
||||
user: AuthUser;
|
||||
payload: AuthJwtPayload;
|
||||
}> {
|
||||
const employee = await authRepository.findActiveEmployeeByPhone(
|
||||
const employee = await authRepository.findEmployeeByPhoneForLogin(
|
||||
input.username,
|
||||
);
|
||||
|
||||
@@ -157,6 +169,14 @@ export const authService = {
|
||||
throw unauthorized("用户名或密码错误");
|
||||
}
|
||||
|
||||
if (employee.status === "INACTIVE") {
|
||||
throw unauthorized("账号已被禁用");
|
||||
}
|
||||
|
||||
if (employee.storeStatus === "INACTIVE") {
|
||||
throw unauthorized("所属门店已被禁用");
|
||||
}
|
||||
|
||||
await authRepository.updateEmployeeLastLoginAt(employee.id);
|
||||
|
||||
const user = await toEmployeeAuthUser(employee);
|
||||
|
||||
@@ -28,8 +28,10 @@ export interface EmployeeLoginAccount {
|
||||
username: string;
|
||||
passwordHash: string;
|
||||
displayName: string;
|
||||
status: "ACTIVE" | "INACTIVE";
|
||||
storeId: number;
|
||||
storeName: string;
|
||||
storeStatus: "ACTIVE" | "INACTIVE";
|
||||
roles: Array<{
|
||||
id: number;
|
||||
code: string;
|
||||
|
||||
@@ -6,6 +6,9 @@ const HASH_SCHEME = "pbkdf2";
|
||||
const HASH_ALGORITHM = "sha256";
|
||||
const ITERATIONS = 310000;
|
||||
const KEY_LENGTH = 32;
|
||||
export const INITIAL_EMPLOYEE_PASSWORD = "pw111111";
|
||||
export const INITIAL_EMPLOYEE_PASSWORD_HASH =
|
||||
"pbkdf2$sha256$310000$QnXyjrpm0QzcGYLEPdunWg$CfR-CywGl1c_Omh_3PyOWPmo93EcbMY1FEjjd5MDjFo";
|
||||
|
||||
function toBase64Url(buffer: Buffer): string {
|
||||
return buffer.toString("base64url");
|
||||
|
||||
Reference in New Issue
Block a user