using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.Core.Helpers; using DS.Module.UserModule; using DS.WMS.Core.System.Dtos; using DS.WMS.Core.System.Entity; using DS.WMS.Core.System.Interface; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using System.Diagnostics; using System.Runtime.InteropServices; namespace DS.WMS.Core.System.Method; /// /// 公共服务 /// public class CommonService : ICommonService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly IHttpContextAccessor IhttpContext; private readonly IWebHostEnvironment _environment; /// /// /// /// public CommonService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); IhttpContext = _serviceProvider.GetRequiredService(); _environment = _serviceProvider.GetRequiredService(); } /// /// 用户登录 /// /// /// public DataResult UserLogin(UserLoginModel model) { var userInfo = db.Queryable().Filter(null, true) .First(x => x.UserCode == model.UserName); if (userInfo == null) { return DataResult.Failed("账号不存在,请检查!"); } if (userInfo.MD5Password != model.Password) { return DataResult.Failed("密码错误!"); } var tokenModel = new JwtHelper.JwtTokenModel { Uid = userInfo.Id.ToString(), CompanyId = userInfo.CompanyId, GID = userInfo.GID, TenantId = userInfo.TenantId, }; var token = JwtHelper.Encrypt(tokenModel); return DataResult.Success(token); } /// /// /// /// /// public DataResult GetUserInfo() { var userId = user.UserId; var tenantId = user.GetTenantId(); var data = db.Queryable().Filter(null, true).Where(x => x.Id == userId) .Select(a => new CurrentUserViewModel { UserId = a.Id, UserCode = a.UserCode, UserName = a.NickName, TenantId = tenantId, UserType = a.UserType, CompanyId = a.CompanyId, GID = a.GID, ClientId = a.ClientId, IsLimitClient = a.IsLimitClient // CompanyId = a.CompanyId.ToString(), CompanyName = a.CustomerName }) .Mapper(it => { //只能写在Select后面 it.HomePath = "/analysis"; }) .First(); return DataResult.Success(data); } /// /// 查询用户拥有的菜单权限 /// /// public DataResult> GetUserPermissionByToken() { List list = new List(); var userId = user.UserId; var userInfo = db.Queryable().Filter(null, true).Where(x => x.Id == userId).First(); //超级管理员 if (userInfo.UserType == 0) { list = db.Queryable().Filter(null, true).Where(x => x.MenuType == 1 && x.IsHidden == false) .OrderBy(x => x.SortCode) .Select(a => new RouteItem { Id = a.Id, Path = a.Url, Name = a.PermissionName, Component = a.Component, Redirect = a.Redirect, Meta = new RouteMeta() { Title = a.Title, Icon = a.Icon, } }) .ToList(); foreach (var item in list) { var childs = db.Queryable().Filter(null, true) .Where(x => x.MenuType == 2 && x.ParentId == item.Id && x.IsHidden == false) .OrderBy(x => x.SortCode) .Select(a => new RouteItem { Path = a.Url, Name = a.PermissionName, Component = a.Component, Meta = new RouteMeta() { Title = a.Title, Icon = a.Icon, } }).ToList(); item.Children = childs; } } else { var roleIds = db.Queryable().Where(x => x.UserId == userId).Select(x => x.RoleId).ToList(); var permissions = db.Queryable().Where(x => roleIds.Contains(x.RoleId)) .Select(x => x.PermissionId).Distinct().ToList(); var perlist = db.Queryable() .Where(x => x.MenuType == 1 && x.IsHidden == false && permissions.Contains(x.Id)) .OrderBy(x => x.SortCode) .Select(a => new RouteItem { Id = a.Id, Path = a.Url, Name = a.PermissionName, Component = a.Component, Redirect = a.Redirect, SortCode = a.SortCode, Meta = new RouteMeta() { Title = a.Title, Icon = a.Icon, } }) .ToList(); // if (perlist.Count == 0) // { // //公共菜单权限 // var publicFirstMenu = db.Queryable().Filter(null, true).Where(x => // x.MenuType == 1 && x.IsHidden == false && x.PermissionType == (int)PermissionType.Public) // .OrderBy(x=>x.SortCode) // .Select(a => new RouteItem // { // Id = a.Id, // Path = a.Url, // Name = a.PermissionName, // Component = a.Component, // Redirect = a.Redirect, // SortCode = a.SortCode, // Meta = new RouteMeta() // { // Title = a.Title, // Icon = a.Icon, // } // }) // .ToList(); // list.AddRange(publicFirstMenu); // // // 权限身份菜单 // var identityFirstMenu = db.Queryable().Filter(null, true).Where(x => // x.MenuType == 1 && x.IsHidden == false && x.PermissionType == userInfo.PermissionIdentity) // .OrderBy(x=>x.SortCode) // .Select(a => new RouteItem // { // Id = a.Id, // Path = a.Url, // Name = a.PermissionName, // Component = a.Component, // Redirect = a.Redirect, // SortCode = a.SortCode, // Meta = new RouteMeta() // { // Title = a.Title, // Icon = a.Icon, // } // }) // .ToList(); // list.AddRange(identityFirstMenu); // } list.AddRange(perlist); //去重 list.Distinct(); //排序 list = list.OrderBy(x => x.SortCode).ToList(); foreach (var item in list) { var childs = db.Queryable().Where(x => x.MenuType == 2 && x.ParentId == item.Id && x.IsHidden == false && permissions.Contains(x.Id)) .OrderBy(x => x.SortCode) .Select(a => new RouteItem { Path = a.Url, Name = a.PermissionName, Component = a.Component, Meta = new RouteMeta() { Title = a.Title, Icon = a.Icon, } }).ToList(); // if (childs.Count == 0) // { // var publicSecondMenu = db.Queryable().Filter(null, true).Where(x => // x.MenuType == 2 && x.ParentId == item.Id && x.IsHidden == false && x.PermissionType == (int)PermissionType.Public) // .OrderBy(x=>x.SortCode) // .Select(a => new RouteItem // { // Path = a.Url, // Name = a.PermissionName, // Component = a.Component, // Meta = new RouteMeta() // { // Title = a.Title, // Icon = a.Icon, // } // }).ToList(); // childs.AddRange(publicSecondMenu); // // // 权限身份菜单 // var identityecondMenu = db.Queryable().Filter(null, true).Where(x => // x.MenuType == 2 && x.ParentId == item.Id && x.IsHidden == false && x.PermissionType == userInfo.PermissionIdentity) // .OrderBy(x=>x.SortCode) // .Select(a => new RouteItem // { // Id = a.Id, // Path = a.Url, // Name = a.PermissionName, // Component = a.Component, // Redirect = a.Redirect, // Meta = new RouteMeta() // { // Title = a.Title, // Icon = a.Icon, // } // }) // .ToList(); // childs.AddRange(identityecondMenu); // } //去重 childs.Distinct(); //排序 childs = childs.OrderBy(x => x.SortCode).ToList(); item.Children = childs; } } // list = list.OrderBy(x => x.Id).ToList(); return DataResult>.Success("获取数据成功!", list); } #region 获取服务器信息 /// /// 获取服务器信息 /// /// public DataResult GetServerInfo() { //核心数 int cpuNum = Environment.ProcessorCount; string computerName = Environment.MachineName; string osName = RuntimeInformation.OSDescription; string osArch = RuntimeInformation.OSArchitecture.ToString(); string version = RuntimeInformation.FrameworkDescription; string appRAM = ((double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB"; string startTime = Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss"); string sysRunTime = ComputerHelper.GetRunTime(); string serverIP = IhttpContext.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + IhttpContext.HttpContext.Connection.LocalPort; //获取服务器IP var programStartTime = Process.GetCurrentProcess().StartTime; string programRunTime = DateTimeHelper.FormatTime((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0] .ParseToLong()); var data = new { cpu = ComputerHelper.GetComputerInfo(), disk = ComputerHelper.GetDiskInfos(), sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime }, app = new { name = _environment.EnvironmentName, rootPath = _environment.ContentRootPath, webRootPath = _environment.WebRootPath, version, appRAM, startTime, runTime = programRunTime, host = serverIP }, }; return DataResult.Success(data); } #endregion 获取服务器信息 #region 获取角色下拉列表 /// /// 获取角色下拉列表 /// /// public DataResult> GetRoleList() { var list = db.Queryable() .Select(a => new ApiSelectViewModel { Label = a.RoleName, Value = a.Id, }) .ToList(); return DataResult>.Success("获取数据成功!", list); } #endregion 获取角色下拉列表 #region 修改密码 /// /// 修改密码 /// /// /// public DataResult ChangePassword(ChangePasswordInput model) { var userId = user.UserId; var userInfo = db.Queryable().First(x => x.Id == userId); if (model.OldPassword == model.NewPassword) { return DataResult.Failed("旧密码与新密码一致!"); } if (model.NewPassword != model.ConfirmPassword) { return DataResult.Failed("新密码与确认密码不一致!"); } if (userInfo.Password == model.NewPassword) { return DataResult.Failed("新密码与用户密码一致!"); } userInfo.Password = model.ConfirmPassword; userInfo.MD5Password = MD5Helper.MD5Encrypt(model.ConfirmPassword); db.Updateable(userInfo).ExecuteCommand(); return DataResult.Successed("密码修改成功!"); } #endregion 修改密码 public DataResult> GetDataRuleEntityFields(string permissionId) { // SysUser us = new SysUser(); // var temp = us.GetEntityFieldProperty(); var role = new EntityFieldProperty() { FieldName = ComonConst.DataRule_LoginRole, FieldComment = "当前登录用户的角色", FieldType = "String" }; var permission = db.Queryable().First(x => x.Id == permissionId); if (permission.IsNull()) { return DataResult>.Failed("权限模块不存在!"); } var list = permission.PermissionEntity.GetEntityFieldByName(); // var entity = db.EntityMaintenance.GetEntityInfo(permission.PermissionEntity.GetEntity()); // var list = entity.GetEntityFieldByName(); if (list.Count == 0) { return DataResult>.Failed("请配置实体属性!"); } list.Add(role); return DataResult>.Success(list); } // /// // /// 获取当前登录用户的数据访问权限 // /// // /// linq表达式参数的名称,如u=>u.name中的"u" // /// // protected ISugarQueryable GetDataRuleFilter(string parametername) // { // var userInfo = db.Queryable().First(x => x.Id == user.UserId); // if (user.User.Account == Define.SYSTEM_USERNAME) return db.Queryable(); //超级管理员特权 // // var moduleName = typeof(T).Name; // var rule = db.Queryable().First(u => u.SourceCode == moduleName); // if (rule == null) return SugarClient.Queryable(); //没有设置数据规则,那么视为该资源允许被任何主体查看 // if (rule.DataRules.Contains(ComonConst.DataRule_LoginRole)) // { // var roles = db.Queryable().Where(x=>x.UserId == user.UserId).Select(u => u.RoleId).ToList(); // roles.Sort(); //按字母排序,这样可以进行like操作 // rule.DataRules = rule.DataRules.Replace(ComonConst.DataRule_LoginRole, // string.Join(',',roles)); // } // // if (rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINUSER) || // // rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINROLE)|| // // rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINORG)) // // { // // // // //即把{loginUser} =='xxxxxxx'换为 loginUser.User.Id =='xxxxxxx',从而把当前登录的用户名与当时设计规则时选定的用户id对比 // // rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINUSER, loginUser.User.Id); // // // // var roles = loginUser.Roles.Select(u => u.Id).ToList(); // // roles.Sort(); //按字母排序,这样可以进行like操作 // // rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINROLE, // // string.Join(',',roles)); // // // // var orgs = loginUser.Orgs.Select(u => u.Id).ToList(); // // orgs.Sort(); // // rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINORG, // // string.Join(',',orgs)); // // } // return db.Queryable().GenerateFilter(parametername, // JsonHelper.Instance.Deserialize(rule.PrivilegeRules)); // } }