BaseSysInfoController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. [AuditLog("功能管理页面"),HttpPost]
  86. public ActionResult GetUserSession()
  87. {
  88. return AbpJson(AbpSession);
  89. }
  90. #region Auth
  91. /// <summary>
  92. /// 用户权限
  93. /// </summary>
  94. /// <param name="userId"></param>
  95. /// <returns></returns>
  96. [HttpPost, DisableAuditing, AbpAuthorize(PermissionNames.PagesSystemUsersAuth)]
  97. public async Task<ActionResult> GetUserPermission(long userId)
  98. {
  99. var permissions = (await _usersAppService.GetAllPermissions()).Items;
  100. List<PermissionDto> currentPerms = new List<PermissionDto>();
  101. if (AbpSession.UserName == UserBase.AdminUserName)
  102. {
  103. currentPerms.AddRange(permissions);
  104. }
  105. else
  106. {
  107. foreach (var perm in permissions)
  108. {
  109. if (await PermissionChecker.IsGrantedAsync(perm.Name))
  110. currentPerms.Add(perm);
  111. }
  112. }
  113. var permission = permissions.FirstOrDefault(a => a.Name == PermissionNames.Pages);
  114. var model = new PermissionViewModel();
  115. if (permission != null)
  116. {
  117. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  118. .GetAsync(permission.Name, () => _funsAppService.GetFunByPermissionName(permission.Name));
  119. model.Name = permission.Name;
  120. model.IsAuth = await _usersAppService.IsGrantedOnlyUserAsync(userId, permission.Name);
  121. model.PermDisplayName = fun.FunctionName;
  122. model.Sort = fun.Sort;
  123. model.Icon = fun.Icon;
  124. model.IsOpen = fun.Depth < 2;
  125. model.Children = await GetPermissionTree(permission.Name, currentPerms, userId);
  126. }
  127. return AbpJson(model);
  128. }
  129. /// <summary>
  130. /// 获取用户权限树
  131. /// </summary>
  132. /// <param name="parentName"></param>
  133. /// <param name="permissions"></param>
  134. /// <param name="userId"></param>
  135. /// <returns></returns>
  136. private async Task<List<PermissionViewModel>> GetPermissionTree(string parentName, List<PermissionDto> permissions, long userId)
  137. {
  138. var parentPerms = permissions.Where(a => a.Parent?.Name == parentName).OrderBy(a => a.Sort).ToList();
  139. var list = new List<PermissionViewModel>();
  140. if (parentPerms.Any())
  141. {
  142. foreach (var p in parentPerms)
  143. {
  144. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  145. .GetAsync(p.Name, () => _funsAppService.GetFunByPermissionName(p.Name));
  146. var model = new PermissionViewModel
  147. {
  148. Name = p.Name,
  149. IsAuth = await _usersAppService.IsGrantedOnlyUserAsync(userId, p.Name),
  150. PermDisplayName = fun.FunctionName,
  151. Sort = fun.Sort,
  152. Icon = fun.Icon,
  153. IsOpen = fun.Depth < 2,
  154. Children = await GetPermissionTree(p.Name, permissions, userId)
  155. };
  156. list.Add(model);
  157. }
  158. }
  159. return list;
  160. }
  161. /// <summary>
  162. /// 角色权限
  163. /// </summary>
  164. /// <param name="roleId"></param>
  165. /// <returns></returns>
  166. [HttpPost, DisableAuditing, AbpAuthorize(PermissionNames.PagesSystemUsersAuth)]
  167. public async Task<ActionResult> GetRolePermission(int roleId)
  168. {
  169. var permissions = (await _rolesAppService.GetAllPermissions()).Items;
  170. List<PermissionDto> currentPerms = new List<PermissionDto>();
  171. if (AbpSession.UserName == UserBase.AdminUserName)
  172. {
  173. currentPerms.AddRange(permissions);
  174. }
  175. else
  176. {
  177. foreach (var perm in permissions)
  178. {
  179. if (await PermissionChecker.IsGrantedAsync(perm.Name))
  180. currentPerms.Add(perm);
  181. }
  182. }
  183. var permission = permissions.FirstOrDefault(a => a.Name == PermissionNames.Pages);
  184. var model = new PermissionViewModel();
  185. if (permission != null)
  186. {
  187. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  188. .GetAsync(permission.Name, () => _funsAppService.GetFunByPermissionName(permission.Name));
  189. model.Name = permission.Name;
  190. model.IsAuth = await _rolesAppService.IsGrantedAsync(roleId, permission.Name);
  191. model.PermDisplayName = fun.FunctionName;
  192. model.Sort = fun.Sort;
  193. model.Icon = fun.Icon;
  194. model.IsOpen = fun.Depth < 2;
  195. model.Children = await GetPermissionTree(permission.Name, currentPerms, roleId);
  196. }
  197. return AbpJson(model);
  198. }
  199. /// <summary>
  200. /// 获取角色权限树
  201. /// </summary>
  202. /// <param name="parentName"></param>
  203. /// <param name="permissions"></param>
  204. /// <param name="userId"></param>
  205. /// <returns></returns>
  206. private async Task<List<PermissionViewModel>> GetPermissionTree(string parentName, List<PermissionDto> permissions, int userId)
  207. {
  208. var parentPerms = permissions.Where(a => a.Parent?.Name == parentName).OrderBy(a => a.Sort).ToList();
  209. var list = new List<PermissionViewModel>();
  210. if (parentPerms.Any())
  211. {
  212. foreach (var p in parentPerms)
  213. {
  214. var fun = await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache)
  215. .GetAsync(p.Name, () => _funsAppService.GetFunByPermissionName(p.Name));
  216. var model = new PermissionViewModel
  217. {
  218. Name = p.Name,
  219. IsAuth = await _rolesAppService.IsGrantedAsync(userId, p.Name),
  220. PermDisplayName = fun.FunctionName,
  221. Sort = fun.Sort,
  222. Icon = fun.Icon,
  223. IsOpen = fun.Depth < 2,
  224. Children = await GetPermissionTree(p.Name, permissions, userId)
  225. };
  226. list.Add(model);
  227. }
  228. }
  229. return list;
  230. }
  231. public class PermissionViewModel
  232. {
  233. public string Name { get; set; }
  234. public string PermDisplayName { get; set; }
  235. public int Sort { get; set; }
  236. public string Icon { get; set; }
  237. public bool IsOpen { get; set; }
  238. public bool IsAuth { get; set; }
  239. public List<PermissionViewModel> Children { get; set; }
  240. }
  241. #endregion
  242. [AbpMvcAuthorize(PermissionNames.PagesSystemSysFunction), AuditLog("功能菜单页面")]
  243. public ActionResult SysFunctions()
  244. {
  245. ViewBag.FunctionType = StatesAppService.GetSelectLists("SysFunction", "FunctionType");
  246. ViewBag.CurrentUser = GetCurrentUser();
  247. return View();
  248. }
  249. [AbpMvcAuthorize(PermissionNames.PagesSystemSysState), AuditLog("系统字典页面")]
  250. public ActionResult SysStates()
  251. {
  252. return View();
  253. }
  254. [AbpMvcAuthorize(PermissionNames.PagesSystemSysSetting), AuditLog("系统配置页面")]
  255. public ActionResult SysSettings()
  256. {
  257. return View();
  258. }
  259. [AbpMvcAuthorize(PermissionNames.PagesSystemSysLog), AuditLog("操作日志页面")]
  260. public async Task<ActionResult> SysLogs()
  261. {
  262. ViewBag.ServiceNames = await _logsAppService.GetLogServiceSelectListStrs();
  263. ViewBag.MethodNames = await _logsAppService.GetLogMethodSelectListStrs(new QueryMethodName());
  264. return View();
  265. }
  266. [AbpMvcAuthorize(PermissionNames.PagesSystemSysHelp), AuditLog("系统帮助维护页面")]
  267. public ActionResult SysHelps()
  268. {
  269. ViewBag.Classification = StatesAppService.GetSelectLists("SysHelp", "Classification");
  270. return View();
  271. }
  272. public ActionResult SysHelpPreview()
  273. {
  274. var helps = _sysHelpRepository.GetAllList();
  275. return View(helps);
  276. }
  277. [DisableAbpAntiForgeryTokenValidation]
  278. [DontWrapResult]
  279. public ActionResult KindEditorUploadFile()
  280. {
  281. Hashtable hash;
  282. try
  283. {
  284. int maxSize = 1024*1024*10;
  285. HttpPostedFileBase file = Request.Files["imgFile"];
  286. if (file == null)
  287. {
  288. hash = new Hashtable
  289. {
  290. ["error"] = 1,
  291. ["url"] = "未上传文件!"
  292. };
  293. return Json(hash, "text/html;charset=UTF-8");
  294. }
  295. if (file.ContentLength > maxSize)
  296. {
  297. hash = new Hashtable
  298. {
  299. ["error"] = 1,
  300. ["url"] = "上传文件大于10M!"
  301. };
  302. return Json(hash, "text/html;charset=UTF-8");
  303. }
  304. var fileName = Clock.Now.ToString("yyyyMMddHHmmss") + file.FileName;
  305. string lcPath = SettingManager.GetValue(SettingNames.DownloadPath) + "/KindEditorUploadFile";
  306. string dir = Request["dir"];
  307. if (!dir.IsNullOrEmpty())
  308. {
  309. lcPath = Path.Combine(lcPath, dir);
  310. }
  311. var filePath = Server.MapPath($"~/{lcPath}");
  312. if (!Directory.Exists(filePath))
  313. {
  314. Directory.CreateDirectory(filePath);
  315. }
  316. file.SaveAs(Path.Combine(filePath, fileName));
  317. hash = new Hashtable
  318. {
  319. ["error"] = 0,
  320. ["url"] = Path.Combine(lcPath, fileName)
  321. };
  322. return Json(hash, "text/html;charset=UTF-8");
  323. }
  324. catch (Exception e)
  325. {
  326. this.LogError(e);
  327. hash = new Hashtable
  328. {
  329. ["error"] = 1,
  330. ["url"] = "附件上传失败!"
  331. };
  332. return Json(hash, "text/html;charset=UTF-8");
  333. }
  334. }
  335. }
  336. }