chore: 目录调整,controller转移到外部单独的目录

This commit is contained in:
xiaojunnuo
2024-10-13 21:59:29 +08:00
parent ccfe72a0d9
commit 417971d15d
37 changed files with 79 additions and 81 deletions
@@ -0,0 +1,54 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { CrudController } from '@certd/lib-server';
import { PermissionService } from '../../../modules/sys/authority/service/permission-service.js';
/**
* 权限资源
*/
@Provide()
@Controller('/api/sys/authority/permission')
export class PermissionController extends CrudController<PermissionService> {
@Inject()
service: PermissionService;
getService() {
return this.service;
}
@Post('/page', { summary: 'sys:auth:per:view' })
async page(
@Body(ALL)
body
) {
return await super.page(body);
}
@Post('/add', { summary: 'sys:auth:per:add' })
async add(
@Body(ALL)
bean
) {
return await super.add(bean);
}
@Post('/update', { summary: 'sys:auth:per:edit' })
async update(
@Body(ALL)
bean
) {
return await super.update(bean);
}
@Post('/delete', { summary: 'sys:auth:per:remove' })
async delete(
@Query('id')
id: number
) {
return await super.delete(id);
}
@Post('/tree', { summary: 'sys:auth:per:view' })
async tree() {
const tree = await this.service.tree({});
return this.ok(tree);
}
}
@@ -0,0 +1,91 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { CrudController } from '@certd/lib-server';
import { RoleService } from '../../../modules/sys/authority/service/role-service.js';
/**
* 系统用户
*/
@Provide()
@Controller('/api/sys/authority/role')
export class RoleController extends CrudController<RoleService> {
@Inject()
service: RoleService;
getService() {
return this.service;
}
@Post('/page', { summary: 'sys:auth:role:view' })
async page(
@Body(ALL)
body
) {
return await super.page(body);
}
@Post('/list', { summary: 'sys:auth:role:view' })
async list() {
const ret = await this.service.find({});
return this.ok(ret);
}
@Post('/add', { summary: 'sys:auth:role:add' })
async add(
@Body(ALL)
bean
) {
return await super.add(bean);
}
@Post('/update', { summary: 'sys:auth:role:edit' })
async update(
@Body(ALL)
bean
) {
return await super.update(bean);
}
@Post('/delete', { summary: 'sys:auth:role:remove' })
async delete(
@Query('id')
id: number
) {
if (id === 1) {
throw new Error('不能删除默认的管理员角色');
}
return await super.delete(id);
}
@Post('/getPermissionTree', { summary: 'sys:auth:role:view' })
async getPermissionTree(
@Query('id')
id: number
) {
const ret = await this.service.getPermissionTreeByRoleId(id);
return this.ok(ret);
}
@Post('/getPermissionIds', { summary: 'sys:auth:role:view' })
async getPermissionIds(
@Query('id')
id: number
) {
const ret = await this.service.getPermissionIdsByRoleId(id);
return this.ok(ret);
}
/**
* 给角色授予权限
* @param roleId
* @param permissionIds
*/
@Post('/authz', { summary: 'sys:auth:role:edit' })
async authz(
@Body('roleId')
roleId,
@Body('permissionIds')
permissionIds
) {
await this.service.authz(roleId, permissionIds);
return this.ok(null);
}
}
@@ -0,0 +1,114 @@
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';
/**
* 系统用户
*/
@Provide()
@Controller('/api/sys/authority/user')
export class UserController extends CrudController<UserService> {
@Inject()
service: UserService;
@Inject()
roleService: RoleService;
@Inject()
permissionService: PermissionService;
getService() {
return this.service;
}
@Post('/page', { summary: 'sys:auth:user:view' })
async page(
@Body(ALL)
body
) {
const ret = await super.page(body);
const users = ret.data.records;
//获取roles
const userIds = users.map(item => item.id);
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;
}
@Post('/add', { summary: 'sys:auth:user:add' })
async add(
@Body(ALL)
bean
) {
return await super.add(bean);
}
@Post('/update', { summary: 'sys:auth:user:edit' })
async update(
@Body(ALL)
bean
) {
return await super.update(bean);
}
@Post('/delete', { summary: 'sys:auth:user:remove' })
async delete(
@Query('id')
id: number
) {
if (id === 1) {
throw new Error('不能删除默认的管理员用户');
}
return await super.delete(id);
}
/**
* 当前登录用户的个人信息
*/
@Post('/mine', { summary: Constants.per.authOnly })
public async mine() {
const id = this.getUserId();
const info = await this.service.info(id, ['password']);
return this.ok(info);
}
/**
* 当前登录用户的权限列表
*/
@Post('/permissions', { summary: Constants.per.authOnly })
public async permissions() {
const id = this.getUserId();
const permissions = await this.service.getUserPermissions(id);
return this.ok(permissions);
}
/**
* 当前登录用户的权限树形列表
*/
@Post('/permissionTree', { summary: Constants.per.authOnly })
public async permissionTree() {
const id = this.getUserId();
const permissions = await this.service.getUserPermissions(id);
const tree = this.permissionService.buildTree(permissions);
return this.ok(tree);
}
}