GeneralMethod.cs 8.9 KB

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