343 lines
12 KiB
C#
343 lines
12 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using WalkingTec.Mvvm.Core;
|
|
using WalkingTec.Mvvm.Core.Extensions;
|
|
using WalkingTec.Mvvm.Mvc;
|
|
using wtmProject.Model;
|
|
using wtmProject.ViewModel._Admin.FrameworkUserVMs;
|
|
|
|
|
|
namespace wtmProject.Controllers
|
|
{
|
|
[Area("_Admin")]
|
|
[AuthorizeJwtWithCookie]
|
|
[ActionDescription("用户维护")]
|
|
[ApiController]
|
|
[Route("api/FrameworkUser")]
|
|
[AllRights]
|
|
public partial class FrameworkUserController : BaseApiController
|
|
{
|
|
[ActionDescription("Sys.Search")]
|
|
[HttpPost("Search")]
|
|
public IActionResult Search(FrameworkUserSearcher searcher)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm).Result;
|
|
}
|
|
if (ModelState.IsValid)
|
|
{
|
|
var vm = Wtm.CreateVM<FrameworkUserListVM>(passInit: true);
|
|
vm.Searcher = searcher;
|
|
return Content(vm.GetJson());
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
}
|
|
|
|
[ActionDescription("Sys.Get")]
|
|
[HttpGet("{id}")]
|
|
public FrameworkUserVM Get(string id)
|
|
{
|
|
var vm = Wtm.CreateVM<FrameworkUserVM>(id);
|
|
return vm;
|
|
}
|
|
|
|
[ActionDescription("Sys.Create")]
|
|
[HttpPost("Add")]
|
|
public async Task<IActionResult> Add(FrameworkUserVM vm)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
await vm.DoAddAsync();
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
return Ok(vm.Entity);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
[ActionDescription("Sys.Edit")]
|
|
[HttpPut("Edit")]
|
|
public async Task<IActionResult> Edit(FrameworkUserVM vm)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
ModelState.Remove("Entity.Password");
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
await vm.DoEditAsync(false);
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
return Ok(vm.Entity);
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpPost("BatchDelete")]
|
|
[ActionDescription("Sys.Delete")]
|
|
public async Task<IActionResult> BatchDelete(string[] ids)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
var vm = Wtm.CreateVM<FrameworkUserBatchVM>();
|
|
List<string> itcode = new List<string>();
|
|
if (ids != null && ids.Count() > 0)
|
|
{
|
|
vm.Ids = ids;
|
|
itcode = DC.Set<FrameworkUser>().CheckIDs(new List<string>(ids)).Select(x => x.ITCode).ToList();
|
|
}
|
|
else
|
|
{
|
|
return Ok();
|
|
}
|
|
if (!ModelState.IsValid || !vm.DoBatchDelete())
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
using (var tran = DC.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var ur = DC.Set<FrameworkUserRole>().Where(x => itcode.Contains(x.UserCode));
|
|
DC.Set<FrameworkUserRole>().RemoveRange(ur);
|
|
var ug = DC.Set<FrameworkUserGroup>().Where(x => itcode.Contains(x.UserCode));
|
|
DC.Set<FrameworkUserGroup>().RemoveRange(ug);
|
|
DC.SaveChanges();
|
|
tran.Commit();
|
|
}
|
|
catch
|
|
{
|
|
tran.Rollback();
|
|
}
|
|
}
|
|
|
|
await Wtm.RemoveUserCache(itcode.ToArray());
|
|
return Ok(ids.Count());
|
|
}
|
|
}
|
|
|
|
|
|
[ActionDescription("Sys.Export")]
|
|
[HttpPost("ExportExcel")]
|
|
public IActionResult ExportExcel(FrameworkUserSearcher searcher)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
var vm = Wtm.CreateVM<FrameworkUserListVM>();
|
|
vm.Searcher = searcher;
|
|
vm.SearcherMode = ListVMSearchModeEnum.Export;
|
|
return vm.GetExportData();
|
|
}
|
|
|
|
[ActionDescription("Sys.CheckExport")]
|
|
[HttpPost("ExportExcelByIds")]
|
|
public IActionResult ExportExcelByIds(string[] ids)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
var vm = Wtm.CreateVM<FrameworkUserListVM>();
|
|
if (ids != null && ids.Count() > 0)
|
|
{
|
|
vm.Ids = new List<string>(ids);
|
|
vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
|
|
}
|
|
return vm.GetExportData();
|
|
}
|
|
|
|
[ActionDescription("Sys.DownloadTemplate")]
|
|
[HttpGet("GetExcelTemplate")]
|
|
public IActionResult GetExcelTemplate()
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
var vm = Wtm.CreateVM<FrameworkUserImportVM>();
|
|
var qs = new Dictionary<string, string>();
|
|
if (Request != null)
|
|
{
|
|
foreach (var item in Request.Query.Keys)
|
|
{
|
|
qs.Add(item, Request.Query[item]);
|
|
}
|
|
}
|
|
vm.SetParms(qs);
|
|
var data = vm.GenerateTemplate(out string fileName);
|
|
return File(data, "application/vnd.ms-excel", fileName);
|
|
}
|
|
|
|
[ActionDescription("Sys.Import")]
|
|
[HttpPost("Import")]
|
|
public ActionResult Import(FrameworkUserImportVM vm)
|
|
{
|
|
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Content(Localizer["_Admin.HasMainHost"]);
|
|
}
|
|
if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
|
|
{
|
|
return BadRequest(vm.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
return Ok(vm.EntityList.Count);
|
|
}
|
|
}
|
|
|
|
|
|
[HttpGet("GetFrameworkGroups")]
|
|
public ActionResult GetFrameworkGroups()
|
|
{
|
|
return Ok(DC.Set<FrameworkGroup>().GetSelectListItems(Wtm, x => x.GroupName));
|
|
}
|
|
[HttpGet("GetFrameworkRoles")]
|
|
[ActionDescription("GetRoles")]
|
|
[AllRights]
|
|
public IActionResult GetFrameworkRoles()
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetFrameworkRoles").Result;
|
|
}
|
|
return Ok(DC.Set<FrameworkRole>().GetSelectListItems(Wtm, x => x.RoleName, x => x.RoleCode));
|
|
}
|
|
[HttpGet("GetFrameworkProjects")]
|
|
[ActionDescription("GetProjects")]
|
|
[AllRights]
|
|
public IActionResult GetFrameworkProjects()
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetFrameworkProjects").Result;
|
|
}
|
|
return Ok(DC.Set<WTM_PROJECT>().GetSelectListItems(Wtm, x => x.ProjectName, x => x.ProjectCode));
|
|
}
|
|
[HttpGet("GetFrameworkAreas")]
|
|
[ActionDescription("GetAreas")]
|
|
[AllRights]
|
|
public IActionResult GetFrameworkAreas()
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetFrameworkAreas").Result;
|
|
}
|
|
return Ok(DC.Set<WTM_AREA>().GetSelectListItems(Wtm, x => x.AreaName, x => x.AreaCode).OrderBy(m=>m.Value));
|
|
}
|
|
[HttpGet("GetFrameworkGroupsTree")]
|
|
[ActionDescription("GetGroupsTree")]
|
|
[AllRights]
|
|
public IActionResult GetFrameworkGroupsTree()
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetFrameworkGroupsTree").Result;
|
|
}
|
|
return Ok(DC.Set<FrameworkGroup>().GetTreeSelectListItems(Wtm, x => x.GroupName, x => x.GroupCode));
|
|
}
|
|
|
|
|
|
[HttpGet("GetUserById")]
|
|
[AllRights]
|
|
public IActionResult GetUserById(string keywords)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetUserById").Result;
|
|
}
|
|
var users = DC.Set<FrameworkUser>().Where(x => x.ITCode.ToLower().StartsWith(keywords.ToLower())).GetSelectListItems(Wtm, x => x.Name + "(" + x.ITCode + ")", x => x.ITCode);
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpGet("GetUserByGroup")]
|
|
[AllRights]
|
|
public IActionResult GetUserByGroup(string keywords)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetUserByGroup").Result;
|
|
}
|
|
var users = DC.Set<FrameworkUserGroup>().Where(x => x.GroupCode == keywords).Select(x => x.UserCode).ToList();
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpGet("GetUserByRole")]
|
|
[AllRights]
|
|
public IActionResult GetUserByRole(string keywords)
|
|
{
|
|
if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
|
|
{
|
|
return Request.RedirectCall(Wtm, "/api/_frameworkuser/GetUserByRole").Result;
|
|
}
|
|
var users = DC.Set<FrameworkUserRole>().Where(x => x.RoleCode == keywords).Select(x => x.UserCode).ToList();
|
|
return Ok(users);
|
|
}
|
|
[ActionDescription("Login.ChangePassword")]
|
|
[HttpPut("[action]")]
|
|
public IActionResult Password(FrameworkUserVM vm)
|
|
{
|
|
var keys = ModelState.Keys.ToList();
|
|
foreach (var item in keys)
|
|
{
|
|
if (item != "Entity.Password")
|
|
{
|
|
ModelState.Remove(item);
|
|
}
|
|
}
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
vm.ChangePassword();
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorJson());
|
|
}
|
|
else
|
|
{
|
|
return Ok(vm.Entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|