OpenAuth.Net/OpenAuth.WebApi/Controllers/ModulesController.cs

273 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;
namespace OpenAuth.WebApi.Controllers
{
/// <summary>
/// 模块及菜单管理
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[ApiExplorerSettings(GroupName = "模块菜单_Modules")]
public class ModulesController : ControllerBase
{
private ModuleManagerApp _app;
private IAuth _authUtil;
public ModulesController(IAuth authUtil, ModuleManagerApp app)
{
_app = app;
_authUtil = authUtil;
}
/// <summary>
/// 加载角色模块
/// </summary>
/// <param name="firstId">The role identifier.</param>
/// <returns>System.String.</returns>
[HttpGet]
public Response<IEnumerable<Module>> LoadForRole(string firstId)
{
var result = new Response<IEnumerable<Module>>();
try
{
result.Result = _app.LoadForRole(firstId);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 根据某角色ID获取可访问某模块的菜单项
/// </summary>
/// <returns></returns>
[HttpGet]
public Response<IEnumerable<ModuleElement>> LoadMenusForRole(string moduleId, string firstId)
{
var result = new Response<IEnumerable<ModuleElement>>();
try
{
result.Result = _app.LoadMenusForRole(moduleId, firstId);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 获取角色已经分配的字段
/// </summary>
/// <param name="roleId">角色id</param>
/// <param name="moduleCode">模块代码如Category</param>
/// <returns></returns>
[HttpGet]
public Response<IEnumerable<string>> LoadPropertiesForRole(string roleId, string moduleCode)
{
var result = new Response<IEnumerable<string>>();
try
{
result.Result = _app.LoadPropertiesForRole(roleId, moduleCode);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 获取发起页面的菜单权限
/// </summary>
/// <returns>System.String.</returns>
[HttpGet]
public Response<List<ModuleElement>> LoadMenus(string moduleId)
{
var result = new Response<List<ModuleElement>>();
try
{
var user = _authUtil.GetCurrentUser();
if (string.IsNullOrEmpty(moduleId))
{
result.Result = user.ModuleElements;
}
else
{
var module = user.Modules.First(u => u.Id == moduleId);
if (module == null)
{
throw new Exception("模块不存在");
}
result.Result = module.Elements;
}
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
#region
//添加或修改
[HttpPost]
public Response<Module> Add([FromBody] Module obj)
{
var result = new Response<Module>();
try
{
_app.Add(obj);
result.Result = obj;
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
//添加或修改
[HttpPost]
public Response Update([FromBody] Module obj)
{
var result = new Response();
try
{
_app.Update(obj);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
[HttpPost]
public Response Delete([FromBody] string[] ids)
{
var result = new Response();
try
{
_app.Delete(ids);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
#endregion
/// <summary>
/// 新增菜单
/// <para>当前登录用户的所有角色会自动分配菜单</para>
/// </summary>
[HttpPost]
public Response<ModuleElement> AddMenu([FromBody] ModuleElement obj)
{
var result = new Response<ModuleElement>();
try
{
_app.AddMenu(obj);
result.Result = obj;
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 修改菜单属性
/// </summary>
[HttpPost]
public Response UpdateMenu([FromBody] ModuleElement obj)
{
var result = new Response();
try
{
_app.UpdateMenu(obj);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 删除菜单
/// </summary>
[HttpPost]
public Response DeleteMenu([FromBody] string[] ids)
{
var result = new Response();
try
{
_app.DelMenu(ids);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 获取发起页面的打印方案
/// </summary>
[HttpGet]
public Response<List<SysPrinterPlan>> LoadPrinterPlans(string moduleId)
{
var result = new Response<List<SysPrinterPlan>>();
try
{
result.Result = _app.LoadPrinterPlans(moduleId).ToList();
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
}
}