2026-05-31 01:41:33 +08:00
|
|
|
import { Provide, Controller, Post, Inject, Body, Query, ALL } from "@midwayjs/core";
|
|
|
|
|
import { UserService } from "../../../modules/sys/authority/service/user-service.js";
|
|
|
|
|
import { CrudController } from "@certd/lib-server";
|
|
|
|
|
import { RoleService } from "../../../modules/sys/authority/service/role-service.js";
|
|
|
|
|
import { PermissionService } from "../../../modules/sys/authority/service/permission-service.js";
|
|
|
|
|
import { Constants } from "@certd/lib-server";
|
|
|
|
|
import { In } from "typeorm";
|
|
|
|
|
import { LoginService } from "../../../modules/login/service/login-service.js";
|
2023-01-29 15:26:58 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 系统用户
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
2026-05-31 01:41:33 +08:00
|
|
|
@Controller("/api/sys/authority/user")
|
2023-01-29 15:26:58 +08:00
|
|
|
export class UserController extends CrudController<UserService> {
|
|
|
|
|
@Inject()
|
|
|
|
|
service: UserService;
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
roleService: RoleService;
|
|
|
|
|
@Inject()
|
|
|
|
|
permissionService: PermissionService;
|
|
|
|
|
|
2025-04-14 18:09:54 +08:00
|
|
|
@Inject()
|
|
|
|
|
loginService: LoginService;
|
|
|
|
|
|
2023-01-29 15:26:58 +08:00
|
|
|
getService() {
|
|
|
|
|
return this.service;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/getSimpleUserByIds", { description: "sys:auth:user:view" })
|
|
|
|
|
async getSimpleUserByIds(@Body("ids") ids: number[]) {
|
2024-12-24 01:12:12 +08:00
|
|
|
const users = await this.service.find({
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
username: true,
|
|
|
|
|
nickName: true,
|
|
|
|
|
mobile: true,
|
|
|
|
|
phoneCode: true,
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: In(ids),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.ok(users);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/getSimpleUsers", { description: "sys:auth:user:view" })
|
2026-02-04 18:54:00 +08:00
|
|
|
async getSimpleUsers() {
|
|
|
|
|
const users = await this.service.find({
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
username: true,
|
|
|
|
|
nickName: true,
|
|
|
|
|
mobile: true,
|
|
|
|
|
phoneCode: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return this.ok(users);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/page", { description: "sys:auth:user:view" })
|
2023-01-29 15:26:58 +08:00
|
|
|
async page(
|
|
|
|
|
@Body(ALL)
|
2026-05-31 01:41:33 +08:00
|
|
|
body
|
2023-01-29 15:26:58 +08:00
|
|
|
) {
|
|
|
|
|
const ret = await super.page(body);
|
|
|
|
|
|
|
|
|
|
const users = ret.data.records;
|
|
|
|
|
|
|
|
|
|
//获取roles
|
2023-06-25 23:25:56 +08:00
|
|
|
const userIds = users.map(item => item.id);
|
2023-01-29 15:26:58 +08:00
|
|
|
const userRoles = await this.roleService.getByUserIds(userIds);
|
|
|
|
|
const userRolesMap = new Map();
|
|
|
|
|
for (const ur of userRoles) {
|
|
|
|
|
let roles = userRolesMap.get(ur.userId);
|
|
|
|
|
if (roles == null) {
|
|
|
|
|
roles = [];
|
|
|
|
|
userRolesMap.set(ur.userId, roles);
|
|
|
|
|
}
|
|
|
|
|
roles.push(ur.roleId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const record of users) {
|
|
|
|
|
//withRoles
|
|
|
|
|
record.roles = userRolesMap.get(record.id);
|
|
|
|
|
//删除密码字段
|
|
|
|
|
delete record.password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/add", { description: "sys:auth:user:add" })
|
2023-01-29 15:26:58 +08:00
|
|
|
async add(
|
|
|
|
|
@Body(ALL)
|
2026-05-31 01:41:33 +08:00
|
|
|
bean
|
2023-01-29 15:26:58 +08:00
|
|
|
) {
|
|
|
|
|
return await super.add(bean);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/update", { description: "sys:auth:user:edit" })
|
2023-01-29 15:26:58 +08:00
|
|
|
async update(
|
|
|
|
|
@Body(ALL)
|
2026-05-31 01:41:33 +08:00
|
|
|
bean
|
2023-01-29 15:26:58 +08:00
|
|
|
) {
|
|
|
|
|
return await super.update(bean);
|
|
|
|
|
}
|
2025-04-14 18:09:54 +08:00
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/delete", { description: "sys:auth:user:remove" })
|
2023-01-29 15:26:58 +08:00
|
|
|
async delete(
|
2026-05-31 01:41:33 +08:00
|
|
|
@Query("id")
|
|
|
|
|
id: number
|
2023-01-29 15:26:58 +08:00
|
|
|
) {
|
2024-10-03 01:29:12 +08:00
|
|
|
if (id === 1) {
|
2026-05-31 01:41:33 +08:00
|
|
|
throw new Error("不能删除默认的管理员角色");
|
2024-10-15 12:03:37 +08:00
|
|
|
}
|
|
|
|
|
if (id === 3) {
|
2026-05-31 01:41:33 +08:00
|
|
|
throw new Error("不能删除默认的普通用户角色");
|
2024-10-03 01:29:12 +08:00
|
|
|
}
|
2023-01-29 15:26:58 +08:00
|
|
|
return await super.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:09:54 +08:00
|
|
|
/**
|
|
|
|
|
* 解除登录锁定
|
|
|
|
|
*/
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/unlockBlock", { description: "sys:auth:user:edit" })
|
|
|
|
|
public async unlockBlock(@Body("id") id: number) {
|
|
|
|
|
const info = await this.service.info(id, ["password"]);
|
|
|
|
|
this.loginService.clearCacheOnSuccess(info.username);
|
2025-04-14 18:09:54 +08:00
|
|
|
if (info.mobile) {
|
2026-05-31 01:41:33 +08:00
|
|
|
this.loginService.clearCacheOnSuccess(info.mobile);
|
2025-04-14 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
return this.ok(info);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 15:26:58 +08:00
|
|
|
/**
|
|
|
|
|
* 当前登录用户的个人信息
|
|
|
|
|
*/
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/mine", { description: Constants.per.authOnly })
|
2023-01-29 15:26:58 +08:00
|
|
|
public async mine() {
|
2024-10-10 02:15:05 +08:00
|
|
|
const id = this.getUserId();
|
2026-05-31 01:41:33 +08:00
|
|
|
const info = await this.service.info(id, ["password"]);
|
2023-01-29 15:26:58 +08:00
|
|
|
return this.ok(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当前登录用户的权限列表
|
|
|
|
|
*/
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/permissions", { description: Constants.per.authOnly })
|
2023-01-29 15:26:58 +08:00
|
|
|
public async permissions() {
|
2024-10-10 02:15:05 +08:00
|
|
|
const id = this.getUserId();
|
2023-01-29 15:26:58 +08:00
|
|
|
const permissions = await this.service.getUserPermissions(id);
|
|
|
|
|
return this.ok(permissions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当前登录用户的权限树形列表
|
|
|
|
|
*/
|
2026-05-31 01:41:33 +08:00
|
|
|
@Post("/permissionTree", { description: Constants.per.authOnly })
|
2023-01-29 15:26:58 +08:00
|
|
|
public async permissionTree() {
|
2024-10-10 02:15:05 +08:00
|
|
|
const id = this.getUserId();
|
2023-01-29 15:26:58 +08:00
|
|
|
const permissions = await this.service.getUserPermissions(id);
|
|
|
|
|
const tree = this.permissionService.buildTree(permissions);
|
|
|
|
|
return this.ok(tree);
|
|
|
|
|
}
|
|
|
|
|
}
|