Skip to content

Commit

Permalink
[重大改进]:实现租户菜单管理
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamXu96 committed Jan 6, 2023
1 parent 488e8c9 commit f49833f
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public interface ITenantAppService : IApplicationService
{
Task UpdateTenantMenu(UpdateTenantMenuDto input);

Task<ListResultDto<MenusTreeDto>> GetTenantMenusList();
Task<ListResultDto<MenusListDto>> GetTenantMenusList();

Task<ListResultDto<Guid>> GetTenantMenuIds(Guid id);
Task<ListResultDto<MenusListDto>> GetTenantMenusById(Guid id);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;

namespace BaseService.Systems.UserRoleMenusManagement.Dto
{
public class MenusTreeDto : EntityDto<Guid>
public class MenusListDto : EntityDto<Guid>
{
public Guid? Pid { get; set; }

Expand All @@ -15,5 +14,7 @@ public class MenusTreeDto : EntityDto<Guid>
public int Sort { get; set; }

public string Permission { get; set; }

public bool IsHost { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IRoleMenusAppService : IApplicationService

Task<ListResultDto<Guid>> GetRoleMenuIds(Guid id);

Task<ListResultDto<MenusTreeDto>> GetMenusList();
Task<ListResultDto<MenusListDto>> GetMenusList();

Task Update(UpdateRoleMenuDto input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public BaseServiceApplicationAutoMapperProfile()
CreateMap<Job, JobDto>();

CreateMap<Menu, MenuDto>();
CreateMap<Menu, MenusTreeDto>();
CreateMap<Menu, MenusListDto>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BaseService.Systems.TenantManagement.Dto;
using BaseService.Systems.UserRoleMenusManagement.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -34,54 +33,47 @@ public TenantAppService(
_roleMenuRepository = roleMenuRepository;
}

public async Task<ListResultDto<MenusTreeDto>> GetTenantMenusList()
public async Task<ListResultDto<MenusListDto>> GetTenantMenusList()
{
var result = await _menuRepository.GetListAsync(_ => _.IsHost == false);
var dtos = ObjectMapper.Map<List<Menu>, List<MenusTreeDto>>(result);
return new ListResultDto<MenusTreeDto>(dtos.OrderBy(_ => _.Sort).ToList());
var dtos = ObjectMapper.Map<List<Menu>, List<MenusListDto>>(result);
return new ListResultDto<MenusListDto>(dtos.OrderBy(_ => _.Sort).ToList());
}

public async Task<ListResultDto<Guid>> GetTenantMenuIds(Guid id)
public async Task<ListResultDto<MenusListDto>> GetTenantMenusById(Guid id)
{
var tenant = await TenantRepository.GetAsync(id);
using (CurrentTenant.Change(tenant.Id))
{
var menus = await _menuRepository.GetListAsync();
return new ListResultDto<Guid>(menus.Select(_ => _.Id).ToList());
var result = await _menuRepository.GetListAsync();
var dtos = ObjectMapper.Map<List<Menu>, List<MenusListDto>>(result);
return new ListResultDto<MenusListDto>(dtos.OrderBy(_ => _.Sort).ToList());
}
}

public async Task UpdateTenantMenu(UpdateTenantMenuDto input)
{
var tenant = await TenantRepository.GetAsync(input.TenantId);
var menus = await (await _menuRepository.GetQueryableAsync()).Where(_ => input.MenuIds.Contains(_.Id)).ToListAsync();
using (CurrentTenant.Change(tenant.Id))
{
var tenantRoles = await RoleRepository.GetListAsync();

var menus = await _menuRepository.GetListAsync();
var adminRole = (await RoleRepository.GetListAsync()).First(t => t.Name == "admin");

//清除租户所有角色菜单,TODO:清除租户角色权限
await _roleMenuRepository.DeleteAsync(_ => tenantRoles.Select(s => s.Id).Contains(_.RoleId));
await _roleMenuRepository.DeleteAsync(_ => menus.Select(m => m.Id).Contains(_.MenuId));

foreach (var menu in menus)
{
//添加租户菜单
var menuId = GuidGenerator.Create();
await _menuRepository.InsertAsync(new Menu(menuId)
if (input.MenuIds.Contains(menu.Id))
{
FormId = menu.FormId,
Pid = menu.Pid,
CategoryId = menu.CategoryId,
Name = menu.Name,
Label = menu.Label,
Sort = menu.Sort,
Path = menu.Path,
Component = menu.Component,
Permission = menu.Permission,
Icon = menu.Icon,
Hidden = menu.Hidden,
AlwaysShow = menu.AlwaysShow
});
//添加租户admin角色菜单
await _roleMenuRepository.InsertAsync(new RoleMenu(tenantRoles.FirstOrDefault(t => t.Name == "admin").Id, menuId));
menu.IsHost = false;
//添加租户admin角色菜单
await _roleMenuRepository.InsertAsync(new RoleMenu(adminRole.Id, menu.Id));
}
else
menu.IsHost = true;

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public async Task Update(UpdateRoleMenuDto input)
}

[Authorize(IdentityPermissions.Roles.Default)]
public async Task<ListResultDto<MenusTreeDto>> GetMenusList()
public async Task<ListResultDto<MenusListDto>> GetMenusList()
{
var result = new List<Menu>();
if (!CurrentTenant.Id.HasValue)
result = await _menuRepository.GetListAsync();
else
result = await _menuRepository.GetListAsync(_ => _.IsHost == false);
var dtos = ObjectMapper.Map<List<Menu>, List<MenusTreeDto>>(result);
return new ListResultDto<MenusTreeDto>(dtos.OrderBy(_ => _.Sort).ToList());
var dtos = ObjectMapper.Map<List<Menu>, List<MenusListDto>>(result);
return new ListResultDto<MenusListDto>(dtos.OrderBy(_ => _.Sort).ToList());
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion BaseService/BaseService.Domain/Systems/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Menu : AuditedAggregateRoot<Guid>, ISoftDelete, IMultiTenant
public bool AlwaysShow { get; set; }

/// <summary>
/// 宿主菜单
/// 是否宿主菜单,if(TenantId!=null&&IsHost==true):停用租户菜单
/// </summary>
public bool IsHost { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion BaseService/BaseService.HttpApi/BaseService.HttpApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="Volo.Abp.PermissionManagement.HttpApi" Version="6.0.0" />
<PackageReference Include="Volo.Abp.TenantManagement.HttpApi" Version="6.0.0" />
<PackageReference Include="Volo.Abp.Identity.HttpApi" Version="6.0.0" />
<PackageReference Include="XCZ.Business.HttpApi" Version="2.7.8" />
<PackageReference Include="XCZ.Business.HttpApi" Version="2.7.9" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Task Update(UpdateRoleMenuDto input)

[HttpGet]
[Route("list")]
public Task<ListResultDto<MenusTreeDto>> GetMenusList()
public Task<ListResultDto<MenusListDto>> GetMenusList()
{
return _roleMenusAppService.GetMenusList();
}
Expand Down
6 changes: 3 additions & 3 deletions BaseService/BaseService.HttpApi/Systems/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public TenantController(ITenantAppService tenantAppService)

[HttpGet]
[Route("menu/{id}")]
public Task<ListResultDto<Guid>> GetTenantMenuIds(Guid id)
public Task<ListResultDto<MenusListDto>> GetTenantMenusById(Guid id)
{
return _tenantAppService.GetTenantMenuIds(id);
return _tenantAppService.GetTenantMenusById(id);
}

[HttpGet]
[Route("menu-list")]
public Task<ListResultDto<MenusTreeDto>> GetTenantMenusList()
public Task<ListResultDto<MenusListDto>> GetTenantMenusList()
{
return _tenantAppService.GetTenantMenusList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Business</RootNamespace>
<Version>2.7.8</Version>
<Version>2.7.9</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>XCZ</Authors>
<Company>XCZ</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Business</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.7.8</Version>
<Version>2.7.9</Version>
<PackageId>XCZ.Business.HttpApi</PackageId>
<Authors>XCZ</Authors>
<Description>Business服务HttpApi远程依赖</Description>
Expand Down
5 changes: 0 additions & 5 deletions WebApp/vue/src/views/role/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,6 @@ export default {
},
checkNode(data, state) {
if (!data.pid) {
// if (state.checkedKeys.indexOf(data.id) === -1) {
// data.children.forEach(element => {
// this.$refs.tree.setChecked(element.id, false);
// });
// }
} else {
if (state.checkedKeys.indexOf(data.id) > -1) {
this.$refs.tree.setChecked(data.pid, true);
Expand Down
68 changes: 20 additions & 48 deletions WebApp/vue/src/views/tenant/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<div style="padding: 6px 0;">
<el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="handleCreate"
v-permission="['AbpTenantManagement.Tenants.Create']">新增</el-button>
<el-button class="filter-item" size="mini" type="success" icon="el-icon-edit"
v-permission="['AbpTenantManagement.Tenants.Update']" @click="handleUpdate()">修改</el-button>
<el-button slot="reference" class="filter-item" type="danger" icon="el-icon-delete" size="mini"
v-permission="['AbpTenantManagement.Tenants.Delete']" @click="handleDelete()">删除</el-button>
</div>
</div>

Expand Down Expand Up @@ -78,7 +74,7 @@
</span>
</div>
<el-tree ref="tree" v-loading="treeLoading" :check-strictly="true" :data="menus" show-checkbox node-key="id"
@check="checkNode" class="permission-tree" />
class="permission-tree" />
</el-card>
</el-col>
</el-row>
Expand Down Expand Up @@ -129,18 +125,8 @@ export default {
},
created() {
this.getList();
this.getMenuList();
},
methods: {
getMenuList() {
this.treeLoading = true;
this.$axios.gets("/api/base/tenant/menu-list").then((response) => {
this.menuData = response.items;
this.menus = response.items.filter((_) => _.pid == null);
this.setChildren(this.menus, response.items);
this.treeLoading = false;
});
},
getList() {
this.listLoading = true;
this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
Expand Down Expand Up @@ -208,19 +194,6 @@ export default {
this.savePerLoading = true;
let params = {};
let checkedKeys = this.$refs.tree.getCheckedKeys();
params.permissions = [];
this.menuData.forEach((element) => {
if (element.permission) {
let perm = {};
perm.name = element.permission;
if (checkedKeys.indexOf(element.id) > -1) {
perm.isGranted = true;
} else {
perm.isGranted = false;
}
params.permissions.push(perm);
}
});
this.$axios
.posts("/api/base/tenant/menu", {
tenantId: this.multipleSelection[0].id,
Expand Down Expand Up @@ -287,6 +260,25 @@ export default {
}
}
},
handleRowClick(row, column, event) {
if (
this.multipleSelection.length == 1 &&
this.multipleSelection[0].id == row.id
) {
return;
}
this.treeLoading = true;
this.$refs.multipleTable.clearSelection();
this.$refs.multipleTable.toggleRowSelection(row);
this.$axios.gets("/api/base/tenant/menu/" + row.id).then((response) => {
this.$refs.tree.setCheckedKeys(response.items);
this.menuData = response.items;
this.menus = response.items.filter((_) => _.pid == null);
this.setChildren(this.menus, response.items);
this.$refs.tree.setCheckedKeys(response.items.filter(_ => _.isHost == false).map(_ => _.id));
this.treeLoading = false;
});
},
checkedAll() {
if (this.checked) {
//全选
Expand All @@ -296,11 +288,6 @@ export default {
this.$refs.tree.setCheckedKeys([]);
}
},
checkNode(data, state) {
if (state.checkedKeys.indexOf(data.id) > -1) {
this.$refs.tree.setChecked(data.pid, true);
}
},
setChildren(roots, items) {
roots.forEach((element) => {
items.forEach((item) => {
Expand All @@ -326,21 +313,6 @@ export default {
handleSelectionChange(val) {
this.multipleSelection = val;
},
handleRowClick(row, column, event) {
if (
this.multipleSelection.length == 1 &&
this.multipleSelection[0].id == row.id
) {
return;
}
this.treeLoading = true;
this.$refs.multipleTable.clearSelection();
this.$refs.multipleTable.toggleRowSelection(row);
this.$axios.gets("/api/base/tenant/menu/" + row.id).then((response) => {
this.$refs.tree.setCheckedKeys(response.items);
this.treeLoading = false;
});
},
cancel() {
this.form = Object.assign({}, defaultForm);
this.dialogFormVisible = false;
Expand Down

0 comments on commit f49833f

Please sign in to comment.