/******************************************************************************* * Copyright © 2020 HT.Cloud.Framework 版权所有 * Author: HT.Cloud * Description: WaterCloud快速开发平台 * Website: *********************************************************************************/ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HT.Cloud.Code; using HT.Cloud.Domain; using HT.Cloud.Domain.SystemManage; using HT.Cloud.Service.InfoManage; using HT.Cloud.Service.SystemManage; using HT.Cloud.Service.SystemOrganize; using HT.Cloud.Service.SystemSecurity; namespace HT.Cloud.Web.Controllers { [HandlerLogin] public class ClientsDataController : Controller { /// /// 缓存操作类 /// private string cacheKeyOperator = GlobalContext.SystemConfig.ProjectPrefix + "_operator_";// +登录者token public QuickModuleService _quickModuleService { get; set; } public NoticeService _noticeService { get; set; } public UserService _userService { get; set; } public ModuleService _moduleService { get; set; } public LogService _logService { get; set; } public RoleAuthorizeService _roleAuthorizeService { get; set; } public ItemsDataService _itemsDetailService { get; set; } public ItemsTypeService _itemsService { get; set; } public SystemSetService _setService { get; set; } public MessageService _msgService { get; set; } /// /// 初始数据加载请求方法 /// /// [HttpGet] [HandlerAjaxOnly] [AllowAnonymous] public async Task GetClientsDataJson() { var data = new { dataItems = await this.GetDataItemList(), authorizeButton = await this.GetMenuButtonListNew(), moduleFields = await this.GetMenuFields(), authorizeFields = await this.GetMenuFieldsListNew(), }; return Content(data.ToJson()); } /// /// 清空缓存请求方法 /// /// [HttpGet] public async Task ClearCache() { try { if (!_setService.currentuser.IsSuperAdmin) { return Content(new { code = 0, msg = "此功能需要管理员权限" }.ToJson()); } await CacheHelper.FlushAllAsync(); await OperatorProvider.Provider.EmptyCurrent("pc_"); return Content(new { code = 1, msg = "服务端清理缓存成功" }.ToJson()); } catch (Exception) { return Content(new { code = 0, msg = "此功能需要管理员权限" }.ToJson()); } } /// /// 模块字段权限 /// /// private async Task GetMenuFields() { var roleId = _userService.currentuser.RoleId; if (roleId == null && _userService.currentuser.IsAdmin) { roleId = "admin"; } else if (roleId == null && !_userService.currentuser.IsSuperAdmin) { roleId = "visitor"; } Dictionary dictionary = new Dictionary(); var list = await _roleAuthorizeService.GetMenuList(roleId); foreach (ModuleEntity item in list.Where(a => a.F_UrlAddress != null)) { dictionary.Add(item.F_UrlAddress, item.F_IsFields ?? false); } return dictionary; } /// /// 快捷菜单列表 /// /// private async Task GetQuickModuleList() { var currentuser = _userService.currentuser; if (currentuser.UserId == null) { return null; } var userId = currentuser.UserId; var data = await _quickModuleService.GetQuickModuleList(userId); return data; } /// /// 获取公告信息 /// /// private async Task GetNoticeList() { var data = (await _noticeService.GetList("")).Where(a => a.F_EnabledMark == true).OrderByDescending(a => a.F_CreatorTime).Take(6).ToList(); return data; } /// /// 初始菜单列表请求方法 /// /// [HttpGet] public async Task GetInitDataJson() { var currentuser = _userService.currentuser; if (currentuser.UserId == null) { return Content(""); } var data = await GetMenuListNew(); return Content(data); } /// /// 获取公告信息请求方法 /// /// [HttpGet] public async Task GetNoticeInfo() { var data = await this.GetNoticeList(); return Content(data.ToJson()); } /// /// 获取当前用户信息请求方法 /// /// [HttpGet] [HandlerAjaxOnly] [AllowAnonymous] public async Task GetUserCode() { var currentuser = _userService.currentuser; if (currentuser.UserId == null) { return Content(""); } var data = await _userService.GetFormExtend(currentuser.UserId); var msglist = await _msgService.GetUnReadListJson(); data.MsgCout = msglist.Count(); return Content(data.ToJson()); } /// /// 获取快捷菜单请求方法 /// /// [HttpGet] public async Task GetQuickModule() { try { var data = await this.GetQuickModuleList(); return Content(data.ToJson()); } catch (Exception) { return Content(""); } } /// /// 获取数据信息接口 /// /// [HttpGet] public async Task GetCoutData() { var currentuser = _userService.currentuser; if (currentuser.UserId == null) { return Content(""); } int usercout = (await _userService.GetUserList("")).Count(); var temp = await CacheHelper.GetAsync(cacheKeyOperator + "info_" + currentuser.UserId); int logincout = temp != null && temp.F_LogOnCount != null ? (int)temp.F_LogOnCount : 0; int modulecout = (await _moduleService.GetList()).Where(a => a.F_EnabledMark == true && a.F_UrlAddress != null).Count(); int logcout = (await _logService.GetList()).Count(); var data = new { usercout = usercout, logincout = logincout, modulecout = modulecout, logcout = logcout }; return Content(data.ToJson()); } /// /// 菜单按钮信息 /// /// private async Task GetMenuListNew() { var currentuser = _userService.currentuser; var roleId = currentuser.RoleId; StringBuilder sbJson = new StringBuilder(); InitEntity init = new InitEntity(); init.homeInfo = new HomeInfoEntity(); init.homeInfo.href = GlobalContext.SystemConfig.HomePage; init.logoInfo = new LogoInfoEntity(); var systemset = await _setService.GetForm(currentuser.CompanyId); //修改主页及logo参数 init.logoInfo.title = systemset.F_LogoCode; init.logoInfo.image = ".." + systemset.F_Logo; init.menuInfo = new List(); init.menuInfo = ToMenuJsonNew(await _roleAuthorizeService.GetMenuList(roleId), "0"); sbJson.Append(init.ToJson()); return sbJson.ToString(); } /// /// 菜单信息 /// /// /// /// private List ToMenuJsonNew(List data, string parentId) { List list = new List(); List entitys = data.FindAll(t => t.F_ParentId == parentId); if (entitys.Count > 0) { foreach (var item in entitys) { MenuInfoEntity munu = new MenuInfoEntity(); munu.title = item.F_FullName; munu.icon = item.F_Icon; munu.href = item.F_UrlAddress; switch (item.F_Target) { case "iframe": munu.target = "_self"; break; case "open": munu.target = "_open"; break; case "blank": munu.target = "_blank"; break; default: munu.target = "_self"; break; } if (data.FindAll(t => t.F_ParentId == item.F_Id).Count > 0) { munu.child = new List(); munu.child = ToMenuJsonNew(data, item.F_Id); } if (item.F_IsMenu == true) { list.Add(munu); } }; } return list; } /// /// 字段信息 /// /// private async Task GetDataItemList() { var itemdata = await _itemsDetailService.GetList(); Dictionary dictionaryItem = new Dictionary(); var itemlist = await _itemsService.GetList(); foreach (var item in itemlist.Where(a => a.F_EnabledMark == true).ToList()) { var dataItemList = itemdata.FindAll(t => t.F_ItemId == item.F_Id); Dictionary dictionaryItemList = new Dictionary(); foreach (var itemList in dataItemList) { dictionaryItemList.Add(itemList.F_ItemCode, itemList.F_ItemName); } dictionaryItem.Add(item.F_EnCode, dictionaryItemList); } return dictionaryItem; } /// /// 菜单按钮信息 /// /// private async Task GetMenuButtonListNew() { var currentuser = _userService.currentuser; var roleId = currentuser.RoleId; if (roleId == null && currentuser.IsAdmin) { roleId = "admin"; } else if (roleId == null && !currentuser.IsAdmin) { roleId = "visitor"; } var rolelist = roleId.Split(','); var dictionarylist = new Dictionary>(); if (currentuser.UserId == null) { return dictionarylist; } foreach (var roles in rolelist) { var dictionarytemp = new Dictionary>(); var data = await _roleAuthorizeService.GetButtonList(roles); var dataModuleId = data.Where(a => a.F_ModuleId != null && a.F_ModuleId != "" && a.F_EnabledMark == true).Distinct(new ExtList("F_ModuleId")); foreach (ModuleButtonEntity item in dataModuleId) { var buttonList = data.Where(t => t.F_ModuleId == item.F_ModuleId && t.F_EnabledMark == true).ToList(); dictionarytemp.Add(item.F_ModuleId, buttonList); if (dictionarylist.ContainsKey(item.F_ModuleId)) { dictionarylist[item.F_ModuleId].AddRange(buttonList); dictionarylist[item.F_ModuleId] = dictionarylist[item.F_ModuleId].GroupBy(p => p.F_Id).Select(q => q.First()).ToList(); } else { dictionarylist.Add(item.F_ModuleId, buttonList); } } } return dictionarylist; } /// /// 菜单字段信息 /// /// private async Task GetMenuFieldsListNew() { var currentuser = _userService.currentuser; var roleId = currentuser.RoleId; if (roleId == null && currentuser.IsAdmin) { roleId = "admin"; } else if (roleId == null && !currentuser.IsAdmin) { roleId = "visitor"; } var rolelist = roleId.Split(','); var dictionarylist = new Dictionary>(); if (currentuser.UserId == null) { return dictionarylist; } foreach (var roles in rolelist) { var dictionarytemp = new Dictionary>(); var data = await _roleAuthorizeService.GetFieldsList(roles); var dataModuleId = data.Where(a => a.F_ModuleId != null && a.F_ModuleId != "" && a.F_EnabledMark == true).Distinct(new ExtList("F_ModuleId")); foreach (ModuleFieldsEntity item in dataModuleId) { var buttonList = data.Where(t => t.F_ModuleId == item.F_ModuleId && t.F_EnabledMark == true).ToList(); dictionarytemp.Add(item.F_ModuleId, buttonList); if (dictionarylist.ContainsKey(item.F_ModuleId)) { dictionarylist[item.F_ModuleId].AddRange(buttonList); dictionarylist[item.F_ModuleId] = dictionarylist[item.F_ModuleId].GroupBy(p => p.F_Id).Select(q => q.First()).ToList(); } else { dictionarylist.Add(item.F_ModuleId, buttonList); } } } return dictionarylist; } } }