BaseSysInfoController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Abp.Auditing;
  10. using Abp.Authorization;
  11. using Abp.Domain.Repositories;
  12. using Abp.Runtime.Caching;
  13. using Abp.Timing;
  14. using Abp.Web.Models;
  15. using Abp.Web.Mvc.Authorization;
  16. using Abp.Web.Security.AntiForgery;
  17. using ShwasherSys.Authorization.Permissions;
  18. using ShwasherSys.BaseSysInfo.AuditLog;
  19. using ShwasherSys.BaseSysInfo.AuditLog.Dto;
  20. using ShwasherSys.BaseSysInfo.Functions;
  21. using ShwasherSys.BaseSysInfo.Roles;
  22. using ShwasherSys.BaseSysInfo.Roles.Dto;
  23. using ShwasherSys.BaseSysInfo.States;
  24. using ShwasherSys.BaseSysInfo.Users;
  25. using IwbZero;
  26. using IwbZero.Auditing;
  27. using IwbZero.Authorization.Users;
  28. using IwbZero.Setting;
  29. using Abp.Extensions;
  30. using ShwasherSys.BaseSysInfo;
  31. using ShwasherSys.BasicInfo.Departments;
  32. using ShwasherSys.BasicInfo.Dutys;
  33. using ShwasherSys.BasicInfo.Factory;
  34. namespace ShwasherSys.Controllers
  35. {
  36. [AuditLog("系统基础信息")]
  37. public class SystemController : ShwasherControllerBase
  38. {
  39. private readonly IUsersAppService _usersAppService;
  40. private readonly IRolesAppService _rolesAppService;
  41. private readonly IFunctionsAppService _funsAppService;
  42. private readonly IAuditLogsAppService _logsAppService;
  43. private readonly IFactoriesAppService _factoriesAppService;
  44. private readonly IDepartmentsAppService _departmentsAppService;
  45. private readonly IDutysAppService _dutysAppService;
  46. private readonly IRepository<SysHelp> _sysHelpRepository;
  47. public SystemController(
  48. IUsersAppService usersAppService,
  49. IRolesAppService rolesAppService,
  50. IFunctionsAppService funsAppService,
  51. IAuditLogsAppService logsAppService,
  52. ICacheManager cacheManager,
  53. IStatesAppService statesAppService, IFactoriesAppService factoriesAppService, IDepartmentsAppService departmentsAppService, IDutysAppService dutysAppService, IRepository<SysHelp> sysHelpRepository)
  54. {
  55. _usersAppService = usersAppService;
  56. _rolesAppService = rolesAppService;
  57. _funsAppService = funsAppService;
  58. _logsAppService = logsAppService;
  59. _factoriesAppService = factoriesAppService;
  60. _departmentsAppService = departmentsAppService;
  61. _dutysAppService = dutysAppService;
  62. _sysHelpRepository = sysHelpRepository;
  63. CacheManager = cacheManager;
  64. StatesAppService = statesAppService;
  65. }
  66. [AbpMvcAuthorize(PermissionNames.PagesSystemUsers), AuditLog("用户管理页面")]
  67. public ActionResult SysUsers()
  68. {
  69. var user = GetCurrentUser();
  70. ViewBag.UserType = _usersAppService.GetUserTypeSelect();
  71. ViewBag.IsActive = StatesAppService.GetSelectLists("SysUser", "IsActive");
  72. ViewBag.CurrentUser = user;
  73. ViewBag.Roles = _usersAppService.GetRoleSelects();
  74. ViewBag.Factories = _factoriesAppService.GetFactoriesSelects();
  75. ViewBag.Departments = _departmentsAppService.GetDepartmentsSelects();
  76. ViewBag.Duties = _dutysAppService.GetDutysSelects();
  77. return View();
  78. }
  79. [AbpMvcAuthorize(PermissionNames.PagesSystemRoles), AuditLog("角色管理页面")]
  80. public ActionResult SysRoles()
  81. {
  82. ViewBag.RoleType = _rolesAppService.GetRoleTypeSelect();
  83. return View();
  84. }
  85. #region Auth
  86. /// <summary>
  87. /// 用户权限
  88. /// </summary>
  89. /// <param name="userId"></param>
  90. /// <returns></returns>
  91. [HttpPost, DisableAuditing, AbpAuthorize(PermissionNames.PagesSystemUsersAuth)]
  92. public async Task<ActionResult> GetUserPermission(long userId)
  93. {
  94. var permissions = (await _usersAppService.GetAllPermissions()).Items;
  95. List<PermissionDto> currentPerms = new List<PermissionDto>();
  96. if (AbpSession.UserName == UserBase.AdminUserName)
  97. {
  98. currentPerms.AddRange(permissions);
  99. }
  100. else
  101. {
  102. foreach (var perm in permissions)
  103. {
  104. if (await PermissionChecker.IsGrantedAsync(perm.Name))
  105. currentPerms.Add(perm);
  106. }
  107. }
  108. var permission = permissions.FirstOrDefault(a => a.Name == PermissionNames.Pages);
  109. var model = new PermissionViewModel();
  110. if (permission != null)
  111. {
  112. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  113. .GetAsync(permission.Name, () => _funsAppService.GetFunByPermissionName(permission.Name));
  114. model.Name = permission.Name;
  115. model.IsAuth = await _usersAppService.IsGrantedOnlyUserAsync(userId, permission.Name);
  116. model.PermDisplayName = fun.FunctionName;
  117. model.Sort = fun.Sort;
  118. model.Icon = fun.Icon;
  119. model.IsOpen = fun.Depth < 2;
  120. model.Children = await GetPermissionTree(permission.Name, currentPerms, userId);
  121. }
  122. return AbpJson(model);
  123. }
  124. /// <summary>
  125. /// 获取用户权限树
  126. /// </summary>
  127. /// <param name="parentName"></param>
  128. /// <param name="permissions"></param>
  129. /// <param name="userId"></param>
  130. /// <returns></returns>
  131. private async Task<List<PermissionViewModel>> GetPermissionTree(string parentName, List<PermissionDto> permissions, long userId)
  132. {
  133. var parentPerms = permissions.Where(a => a.Parent?.Name == parentName).OrderBy(a => a.Sort).ToList();
  134. var list = new List<PermissionViewModel>();
  135. if (parentPerms.Any())
  136. {
  137. foreach (var p in parentPerms)
  138. {
  139. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  140. .GetAsync(p.Name, () => _funsAppService.GetFunByPermissionName(p.Name));
  141. var model = new PermissionViewModel
  142. {
  143. Name = p.Name,
  144. IsAuth = await _usersAppService.IsGrantedOnlyUserAsync(userId, p.Name),
  145. PermDisplayName = fun.FunctionName,
  146. Sort = fun.Sort,
  147. Icon = fun.Icon,
  148. IsOpen = fun.Depth < 2,
  149. Children = await GetPermissionTree(p.Name, permissions, userId)
  150. };
  151. list.Add(model);
  152. }
  153. }
  154. return list;
  155. }
  156. /// <summary>
  157. /// 角色权限
  158. /// </summary>
  159. /// <param name="roleId"></param>
  160. /// <returns></returns>
  161. [HttpPost, DisableAuditing, AbpAuthorize(PermissionNames.PagesSystemUsersAuth)]
  162. public async Task<ActionResult> GetRolePermission(int roleId)
  163. {
  164. var permissions = (await _rolesAppService.GetAllPermissions()).Items;
  165. List<PermissionDto> currentPerms = new List<PermissionDto>();
  166. if (AbpSession.UserName == UserBase.AdminUserName)
  167. {
  168. currentPerms.AddRange(permissions);
  169. }
  170. else
  171. {
  172. foreach (var perm in permissions)
  173. {
  174. if (await PermissionChecker.IsGrantedAsync(perm.Name))
  175. currentPerms.Add(perm);
  176. }
  177. }
  178. var permission = permissions.FirstOrDefault(a => a.Name == PermissionNames.Pages);
  179. var model = new PermissionViewModel();
  180. if (permission != null)
  181. {
  182. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  183. .GetAsync(permission.Name, () => _funsAppService.GetFunByPermissionName(permission.Name));
  184. model.Name = permission.Name;
  185. model.IsAuth = await _rolesAppService.IsGrantedAsync(roleId, permission.Name);
  186. model.PermDisplayName = fun.FunctionName;
  187. model.Sort = fun.Sort;
  188. model.Icon = fun.Icon;
  189. model.IsOpen = fun.Depth < 2;
  190. model.Children = await GetPermissionTree(permission.Name, currentPerms, roleId);
  191. }
  192. return AbpJson(model);
  193. }
  194. /// <summary>
  195. /// 获取角色权限树
  196. /// </summary>
  197. /// <param name="parentName"></param>
  198. /// <param name="permissions"></param>
  199. /// <param name="userId"></param>
  200. /// <returns></returns>
  201. private async Task<List<PermissionViewModel>> GetPermissionTree(string parentName, List<PermissionDto> permissions, int userId)
  202. {
  203. var parentPerms = permissions.Where(a => a.Parent?.Name == parentName).OrderBy(a => a.Sort).ToList();
  204. var list = new List<PermissionViewModel>();
  205. if (parentPerms.Any())
  206. {
  207. foreach (var p in parentPerms)
  208. {
  209. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  210. .GetAsync(p.Name, () => _funsAppService.GetFunByPermissionName(p.Name));
  211. var model = new PermissionViewModel
  212. {
  213. Name = p.Name,
  214. IsAuth = await _rolesAppService.IsGrantedAsync(userId, p.Name),
  215. PermDisplayName = fun.FunctionName,
  216. Sort = fun.Sort,
  217. Icon = fun.Icon,
  218. IsOpen = fun.Depth < 2,
  219. Children = await GetPermissionTree(p.Name, permissions, userId)
  220. };
  221. list.Add(model);
  222. }
  223. }
  224. return list;
  225. }
  226. public class PermissionViewModel
  227. {
  228. public string Name { get; set; }
  229. public string PermDisplayName { get; set; }
  230. public int Sort { get; set; }
  231. public string Icon { get; set; }
  232. public bool IsOpen { get; set; }
  233. public bool IsAuth { get; set; }
  234. public List<PermissionViewModel> Children { get; set; }
  235. }
  236. #endregion
  237. [AbpMvcAuthorize(PermissionNames.PagesSystemSysFunction), AuditLog("功能菜单页面")]
  238. public ActionResult SysFunctions()
  239. {
  240. ViewBag.FunctionType = StatesAppService.GetSelectLists("SysFunction", "FunctionType");
  241. ViewBag.CurrentUser = GetCurrentUser();
  242. return View();
  243. }
  244. [AbpMvcAuthorize(PermissionNames.PagesSystemSysState), AuditLog("系统字典页面")]
  245. public ActionResult SysStates()
  246. {
  247. return View();
  248. }
  249. [AbpMvcAuthorize(PermissionNames.PagesSystemSysSetting), AuditLog("系统配置页面")]
  250. public ActionResult SysSettings()
  251. {
  252. return View();
  253. }
  254. [AbpMvcAuthorize(PermissionNames.PagesSystemSysLog), AuditLog("操作日志页面")]
  255. public async Task<ActionResult> SysLogs()
  256. {
  257. ViewBag.ServiceNames = await _logsAppService.GetLogServiceSelectListStrs();
  258. ViewBag.MethodNames = await _logsAppService.GetLogMethodSelectListStrs(new QueryMethodName());
  259. return View();
  260. }
  261. [AbpMvcAuthorize(PermissionNames.PagesSystemSysHelp), AuditLog("系统帮助维护页面")]
  262. public ActionResult SysHelps()
  263. {
  264. ViewBag.Classification = StatesAppService.GetSelectLists("SysHelp", "Classification");
  265. return View();
  266. }
  267. public ActionResult SysHelpPreview()
  268. {
  269. var helps = _sysHelpRepository.GetAllList();
  270. return View(helps);
  271. }
  272. [DisableAbpAntiForgeryTokenValidation]
  273. [DontWrapResult]
  274. public ActionResult KindEditorUploadFile()
  275. {
  276. Hashtable hash;
  277. try
  278. {
  279. int maxSize = 1024*1024*10;
  280. HttpPostedFileBase file = Request.Files["imgFile"];
  281. if (file == null)
  282. {
  283. hash = new Hashtable
  284. {
  285. ["error"] = 1,
  286. ["url"] = "未上传文件!"
  287. };
  288. return Json(hash, "text/html;charset=UTF-8");
  289. }
  290. if (file.ContentLength > maxSize)
  291. {
  292. hash = new Hashtable
  293. {
  294. ["error"] = 1,
  295. ["url"] = "上传文件大于10M!"
  296. };
  297. return Json(hash, "text/html;charset=UTF-8");
  298. }
  299. var fileName = Clock.Now.ToString("yyyyMMddHHmmss") + file.FileName;
  300. string lcPath = SettingManager.GetValue(SettingNames.DownloadPath) + "/KindEditorUploadFile";
  301. string dir = Request["dir"];
  302. if (!dir.IsNullOrEmpty())
  303. {
  304. lcPath = Path.Combine(lcPath, dir);
  305. }
  306. var filePath = Server.MapPath($"~/{lcPath}");
  307. if (!Directory.Exists(filePath))
  308. {
  309. Directory.CreateDirectory(filePath);
  310. }
  311. file.SaveAs(Path.Combine(filePath, fileName));
  312. hash = new Hashtable
  313. {
  314. ["error"] = 0,
  315. ["url"] = Path.Combine(lcPath, fileName)
  316. };
  317. return Json(hash, "text/html;charset=UTF-8");
  318. }
  319. catch (Exception e)
  320. {
  321. this.LogError(e);
  322. hash = new Hashtable
  323. {
  324. ["error"] = 1,
  325. ["url"] = "附件上传失败!"
  326. };
  327. return Json(hash, "text/html;charset=UTF-8");
  328. }
  329. }
  330. }
  331. }