GeneralMethod.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using OASystem.Domain.Entities.PersonnelModule;
  2. using OASystem.Domain.Entities.System;
  3. using OASystem.Domain.ViewModels.PersonnelModule;
  4. using OASystem.Infrastructure.Repositories.Groups;
  5. using System.IdentityModel.Tokens.Jwt;
  6. using System.Security.Claims;
  7. namespace OASystem.API.OAMethodLib
  8. {
  9. public static class GeneralMethod
  10. {
  11. #region 消息
  12. #endregion
  13. #region md5 加密
  14. /// <summary>
  15. /// MD5加密,和动网上的16/32位MD5加密结果相同,
  16. /// 使用的UTF8编码
  17. /// </summary>
  18. /// <param name="source">待加密字串</param>
  19. /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
  20. /// <returns>加密后的字串</returns>
  21. public static string Encrypt(string source, int length = 32)
  22. {
  23. if (string.IsNullOrWhiteSpace(source))
  24. return string.Empty;
  25. HashAlgorithm hashAlgorithm = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
  26. byte[] bytes = Encoding.UTF8.GetBytes(source);
  27. byte[] hashValue = hashAlgorithm.ComputeHash(bytes);
  28. StringBuilder sb = new StringBuilder();
  29. switch (length)
  30. {
  31. case 16://16位密文是32位密文的9到24位字符
  32. for (int i = 4; i < 12; i++)
  33. {
  34. sb.Append(hashValue[i].ToString("x2"));
  35. }
  36. break;
  37. case 32:
  38. for (int i = 0; i < 16; i++)
  39. {
  40. sb.Append(hashValue[i].ToString("x2"));
  41. }
  42. break;
  43. default:
  44. for (int i = 0; i < hashValue.Length; i++)
  45. {
  46. sb.Append(hashValue[i].ToString("x2"));
  47. }
  48. break;
  49. }
  50. return sb.ToString();
  51. }
  52. #endregion
  53. #region jwt
  54. /// <summary>
  55. /// 获取token
  56. /// </summary>
  57. /// <param name="_config"></param>
  58. /// <param name="Number"></param>
  59. /// <param name="exp"></param>
  60. /// <returns></returns>
  61. public static string GetToken(IConfiguration _config,string Number,DateTime exp)
  62. {
  63. var claims = new[] {
  64. new Claim(ClaimTypes.NameIdentifier, "Future"),
  65. new Claim("Number",Number)
  66. };
  67. var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"]));
  68. var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
  69. var token = new JwtSecurityToken(
  70. issuer: "OASystem.com",
  71. audience: "OASystem.com",
  72. claims: claims,
  73. expires: exp,
  74. signingCredentials: creds);
  75. return new JwtSecurityTokenHandler().WriteToken(token);
  76. }
  77. #endregion
  78. #region 数据类型转换
  79. /// <summary>
  80. /// object 转 Int
  81. /// </summary>
  82. /// <param name="obj"></param>
  83. /// <returns></returns>
  84. public static int GetInt(this object obj)
  85. {
  86. if (obj == null)
  87. return 0;
  88. int _number = 0;
  89. bool reslut = Int32.TryParse(obj.ToString(), out _number);
  90. return _number;
  91. }
  92. private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
  93. private static long longTime = 621355968000000000;
  94. private static int samllTime = 10000000;
  95. /// <summary>
  96. /// 时间戳 转 datetime
  97. /// </summary>
  98. /// <param name="timeStamp"></param>
  99. /// <returns></returns>
  100. public static DateTime GetTimeSpmpToDate(this object timeStamp)
  101. {
  102. if (timeStamp == null) return dateStart;
  103. DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
  104. return dateTime;
  105. }
  106. #endregion
  107. #region 用户页面操作功能
  108. //团组信息
  109. private readonly static DelegationInfoRepository _dirRep = AutofacIocManager.Instance.GetService<DelegationInfoRepository>();
  110. /// <summary>
  111. /// 用户页面操作功能(可使用)
  112. /// </summary>
  113. /// <param name="userId">用户Id</param>
  114. /// <param name="PageId">页面Id</param>
  115. /// <returns></returns>
  116. public static async Task<PageFunAuthViewBase> PostUserPageFuncDatas(int userId,int PageId)
  117. {
  118. PageFunAuthViewBase pageFunAuth = new PageFunAuthViewBase();
  119. List<UserPageFuncView> userPageFuncDatas = new List<UserPageFuncView>();
  120. string sql = string.Format(@"Select Distinct ua.UId As UserId, u.CnName As UserName, pa.ModuleId,pa.ModuleName,pa.PageId,pa.PageName,pa.PageIsEnable,
  121. pa.PagePhoneIsEnable,pa.FuncId,pa.FuncName,pa.FuncIsEnable
  122. From Sys_UserAuthority ua
  123. Left Join Sys_Users u On ua.UId = u.Id
  124. Left Join (
  125. Select sd.Id As ModuleId,sd.Name As ModuleName, smp.Id As PageId,smp.Name As PageName,smp.IsEnable As PageIsEnable,
  126. smp.phoneIsEnable As PagePhoneIsEnable,pfp.Id As FuncId,pfp.FunctionName As FuncName,pfp.IsEnable As FuncIsEnable
  127. From Sys_SystemMenuAndFunction smaf
  128. Left Join Sys_SystemMenuPermission smp On smaf.SmId = smp.Id
  129. Left Join Sys_SetData sd On sd.STid = 5 And smp.Mid = sd.Id
  130. Left Join Sys_PageFunctionPermission pfp On smaf.FId = pfp.Id
  131. Where smaf.IsDel = 0 And smp.IsDel = 0 And pfp.IsDel = 0 And sd.IsDel = 0
  132. ) As pa On ua.SmId = pa.PageId
  133. Where ua.IsDel = 0 And ua.UId = {0} And pa.PageId = {1}
  134. Order By ModuleId,PageId,FuncId Asc", userId, PageId);
  135. userPageFuncDatas = await _dirRep._sqlSugar.SqlQueryable<UserPageFuncView>(sql).ToListAsync();
  136. if (userPageFuncDatas.Count <= 0)
  137. {
  138. return pageFunAuth;
  139. }
  140. UserPageFuncView userPageFunc = new UserPageFuncView();
  141. //查询 1
  142. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 1).FirstOrDefault();
  143. if (userPageFunc != null) pageFunAuth.CheckAuth = 1;
  144. //删除 2
  145. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 2).FirstOrDefault();
  146. if (userPageFunc != null) pageFunAuth.DeleteAuth = 1;
  147. //编辑 3
  148. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 3).FirstOrDefault();
  149. if (userPageFunc != null) pageFunAuth.EditAuth = 1;
  150. //下载 4
  151. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 4).FirstOrDefault();
  152. if (userPageFunc != null) pageFunAuth.FilesDownloadAuth = 1;
  153. //上传 5
  154. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 5).FirstOrDefault();
  155. if (userPageFunc != null) pageFunAuth.FilesUploadAuth = 1;
  156. //添加 11
  157. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 11).FirstOrDefault();
  158. if (userPageFunc != null) pageFunAuth.AddAuth = 1;
  159. //审核 12
  160. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 12).FirstOrDefault();
  161. if (userPageFunc != null) pageFunAuth.AuditAuth = 1;
  162. return pageFunAuth;
  163. }
  164. #endregion
  165. }
  166. }