From 25f8d3f6f96bb4965ddb75701b66ad84519c408f Mon Sep 17 00:00:00 2001 From: andyesfly <35127884@qq.com> Date: Wed, 19 Aug 2026 15:09:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(user.service):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=86=85=E7=BD=AE=E7=AE=A1=E7=90=86=E5=91=98=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在用户列表查询、更新和更新状态接口中,添加对id为1的内置管理员账号的保护: 1. 列表查询时自动排除内置管理员账号 2. 禁止编辑内置管理员账号 3. 禁止修改内置管理员账号状态 --- server/src/service/user.service.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/server/src/service/user.service.ts b/server/src/service/user.service.ts index eccf7f5..104f4b7 100644 --- a/server/src/service/user.service.ts +++ b/server/src/service/user.service.ts @@ -21,7 +21,10 @@ export class UserService { async list(query: { page?: any; pageSize?: any; keyword?: string; status?: number; roleId?: number }): Promise { const page = Number(query.page) || 1; const pageSize = Number(query.pageSize) || 10; - const where: any = {}; + // 排除内置管理员账号(id=1) + const where: any = { + id: { [Op.ne]: 1 } + }; if (query.keyword) { where[Op.or] = [{ username: { [Op.like]: `%${query.keyword}%` } }, { realName: { [Op.like]: `%${query.keyword}%` } }, { phone: { [Op.like]: `%${query.keyword}%` } }]; @@ -154,6 +157,15 @@ export class UserService { } async update(id: string, data: any): Promise { + // 禁止编辑内置管理员账号 + if (Number(id) === 1) { + return { + code: 400, + message: "内置管理员账号不允许编辑", + data: null + }; + } + const user = await this.userModel.findByPk(id); if (!user) { @@ -282,6 +294,15 @@ export class UserService { } async updateStatus(id: string, status: number): Promise { + // 禁止修改内置管理员账号状态 + if (Number(id) === 1) { + return { + code: 400, + message: "内置管理员账号状态不允许修改", + data: null + }; + } + const user = await this.userModel.findByPk(id); if (!user) {