Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增角色管理模块&菜单管理模块 #151

Merged
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IsNotEmpty } from 'class-validator';

export class DeleteMenuDto {
import { PartialType } from '@nestjs/mapped-types';
import { CreateMenuDto } from './create-menu.dto';
export class DeleteMenuDto extends PartialType(CreateMenuDto) {
GaoNeng-wWw marked this conversation as resolved.
Show resolved Hide resolved
@IsNotEmpty()
id: number;
@IsNotEmpty()
name: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateMenuDto } from './create-menu.dto';
import {IsNotEmpty} from "class-validator";

export class UpdateMenuDto extends PartialType(CreateMenuDto) {
@IsNotEmpty()
id: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Body,
Patch,
Req,
Delete,
Delete, Param,
} from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
Expand All @@ -17,9 +17,15 @@ import { DeleteMenuDto } from './dto/delete-menu.dto';
export class MenuController {
constructor(private readonly menuService: MenuService) {}

@Get('/role/:email')
async getMenus(@Param('email') email: string) {
return this.menuService.findRoleMenu(email);
}

@Get()
async getMenus(@Req() req) {
return this.menuService.findAll(req.user);
@Permission('menu::query')
async getAllMenus() {
return this.menuService.findAllMenu();
}

@Post()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export interface ITreeNodeData {
url: string;
//组件
component: string;
//图标
icon: string;
//类型
menuType: string;
//父节点
parentId: number;
//排序
order: number;
}

interface MenuMap {
Expand All @@ -30,6 +38,10 @@ const toNode = (menu: Menu): ITreeNodeData => {
children: [],
url: menu.path,
component: menu.component,
icon: menu.icon,
menuType: menu.menuType,
parentId: menu.parentId,
order: menu.order,
};
};

Expand Down Expand Up @@ -58,13 +70,13 @@ export class MenuService {
@InjectRepository(Menu)
private menu: Repository<Menu>
) {}
async findAll(user: User) {
async findRoleMenu(email: string) {
const userInfo = await this.user
.createQueryBuilder('user')
.leftJoinAndSelect('user.role', 'role')
.leftJoinAndSelect('role.menus', 'menus')
.where({
email: user.email,
email: email,
})
.orderBy('menus.order', 'ASC')
.getOne();
Expand All @@ -75,6 +87,12 @@ export class MenuService {
});
return convertToTree(menus);
}

async findAllMenu(){
const menu = this.menu.find();
return convertToTree(await menu)
}

async createMenu(dto: CreateMenuDto) {
const {
order,
Expand Down Expand Up @@ -111,9 +129,15 @@ export class MenuService {
const menu = this.menu.findOne({
where: {
id: dto.id,
name: dto.name,
},
});
const allMenu = await this.menu.find();
for (const tmp of allMenu){
if(tmp.parentId === dto.id){
tmp.parentId = dto.parentId
await this.updateMenu(tmp)
}
}
return this.menu.remove(await menu);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { DeleteRoleDto } from './dto/delete-role.dto';
import { Permission } from '../public/permission.decorator';

@Controller('role')
Expand All @@ -29,16 +28,22 @@ export class RoleController {
return this.roleService.findAll();
}

@Permission('role::get')
@Get('/detail')
getAllRoleDetail() {
return this.roleService.findAllDetail();
}

@Patch()
@Permission('role::update')
updateRole(@Body() dto: UpdateRoleDto) {
return this.roleService.update(dto);
}

@Delete()
@Delete('/:id')
@Permission('role::remove')
deleteRole(@Body() dto: DeleteRoleDto) {
return this.roleService.delete(dto);
deleteRole(@Param('id') id: number) {
return this.roleService.delete(id);
}

@Permission('role::get')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { DeleteRoleDto } from './dto/delete-role.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Menu, Permission, Role } from '@app/models';
import { DataSource, In, Repository } from 'typeorm';
import { convertToTree } from "../menu/menu.service";

@Injectable()
export class RoleService {
Expand Down Expand Up @@ -42,7 +42,25 @@ export class RoleService {
return this.role.save({ name, permission: permissions, menus });
}
findAll() {
return this.role.find();
return this.role.find()
}

async findAllDetail() {
const roleInfo = await this.role
.createQueryBuilder('role')
.leftJoinAndSelect('role.menus', 'menus')
.leftJoinAndSelect('role.permission', 'permission')
.getMany();
const menuTree = [] as any;
for(const item of roleInfo){
const temp = convertToTree(item.menus);
menuTree.push(temp)
}

return {
roleInfo: roleInfo,
menuTree: menuTree,
}
}

async findOne(id: string) {
Expand Down Expand Up @@ -83,10 +101,10 @@ export class RoleService {
role.menus = menus.length ? menus : undefined;
return this.role.save(role);
}
async delete(data: DeleteRoleDto) {
async delete(id: number) {
const role = await this.role.find({
where: {
name: data.name,
id: id,
},
});
return this.role.remove(role);
Expand Down
23 changes: 23 additions & 0 deletions packages/toolkits/pro/template/tinyvue/src/api/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import axios from "axios";



export function getAllMenu() {
return axios.get('/api/menu');
}

export function getRoleMenu(email: string) {
return axios.get(`/api/menu/role/${email}`);
}

export function updateMenu(data: any) {
return axios.patch(`/api/menu`, data);
}

export function deleteMenu(data: any) {
return axios.delete(`/api/menu`, data);
}

export function createMenu(data: any) {
return axios.post(`/api/menu`, data);
}
18 changes: 17 additions & 1 deletion packages/toolkits/pro/template/tinyvue/src/api/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import axios from "axios";



export function getRoles() {
export function getAllRole() {
return axios.get('/api/role');
}

export function getAllRoleDetail() {
return axios.get('/api/role/detail');
}

export function updateRole(data: any) {
return axios.patch(`/api/role`, data);
}

export function deleteRole(id: number) {
return axios.delete(`/api/role/${id}`);
}

export function createRole(data: any) {
return axios.post(`/api/role`, data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
IconApplication,
IconGroup,
IconFolderOpened,
IconActivation,
IconGrade,
} from '@opentiny/vue-icon';
import { TreeMenu as tinyTreeMenu } from '@opentiny/vue';
import router from '@/router';
Expand All @@ -52,6 +54,8 @@
const iconApplication = IconApplication();
const iconFolderOpened = IconFolderOpened();
const iconGroup = IconGroup();
const iconActivation = IconActivation();
const iconGrade = IconGrade();
const tree = ref();
const expandeArr = ref();
const routerTitle = [
Expand Down Expand Up @@ -230,14 +234,26 @@
bold: 'title',
},
{
value: 'PermissionSetting',
name: 'menu.permission.setting',
value: 'Role',
name: 'menu.role',
icon: iconActivation,
bold: 'main-title',
},
{
value: 'AllRole',
name: 'menu.role.info',
icon: null,
bold: 'title',
},
{
value: 'PermissionAdd',
name: 'menu.permission.permissionAdd',
value: 'Menu',
name: 'menu.menu',
icon: iconGrade,
bold: 'main-title',
},
{
value: 'AllMenu',
name: 'menu.menu.info',
icon: null,
bold: 'title',
},
Expand Down Expand Up @@ -311,6 +327,9 @@
'User',
'Cloud',
'UserManager',
'Permission',
'Role',
'Menu',
];
if (filter.indexOf(data.id) === -1) {
router.push({ name: data.id });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
show-header
show-footer
mask-closable="true"
height="350"
height="auto"
width="600"
:title="$t('userInfo.modal.title.pwdUpdate')"
>
Expand Down Expand Up @@ -213,8 +213,8 @@ const state = reactive<{
{ label: 'messageBox.switchRoles', value: 1 },
{ label: 'messageBox.userCenter', value: 2 },
{ label: 'messageBox.userSettings', value: 3 },
{ label: 'messageBox.logout', value: 4 },
{ label: 'messageBox.updatePwd',value: 5 },
{ label: 'messageBox.updatePwd',value: 4 },
{ label: 'messageBox.logout', value: 5 },
];

const switchRoles = async () => {
Expand All @@ -238,10 +238,10 @@ const state = reactive<{
router.push({ name: 'Setting' });
break;
case 4:
logout();
handlePwdUpdate();
break;
case 5:
handlePwdUpdate();
logout();
break;
default:
// eslint-disable-next-line no-console
Expand Down
10 changes: 10 additions & 0 deletions packages/toolkits/pro/template/tinyvue/src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import localeUserManagerUserAdd from '@/views/userManager/useradd/locale/en-US';

import localePermission from '@/views/permission/info/locale/en-US';

import localeRole from '@/views/role/info/locale/en-US';

import localeMenu from '@/views/menu/info/locale/en-US';

import localeSettings from './en-US/settings';

import localeHttpError from './en-US/httpError';
Expand All @@ -56,6 +60,10 @@ export default {
'menu.permission.info':'All Permission Info',
'menu.permission.setting':'Permission Setting',
'menu.permission.permissionAdd':'Add Permission',
'menu.role': 'Role Manager',
'menu.role.info':'All Role Info',
'menu.menu': 'Menu Manager',
'menu.menu.info': 'All Menu Info',
'navbar.docs': 'Docs',
'navbar.action.locale': 'Switch to English',
'messageBox.switchRoles': 'Switch Roles',
Expand Down Expand Up @@ -86,4 +94,6 @@ export default {
...localeUserManagerSetting,
...localeUserManagerUserAdd,
...localePermission,
...localeRole,
...localeMenu,
};
Loading