PublicFunction.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.MainModels;
  5. using Library;
  6. using Microsoft.IdentityModel.Tokens;
  7. using System.IdentityModel.Tokens.Jwt;
  8. using System.Security.Claims;
  9. using System.Text;
  10. namespace MySystem
  11. {
  12. public class PublicFunction
  13. {
  14. #region 根据ParentNav获取顶级创客Id
  15. public static int GetTopUserId(string ParentNav)
  16. {
  17. int TopUserId = 0;
  18. if (!string.IsNullOrEmpty(ParentNav))
  19. {
  20. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  21. }
  22. return TopUserId;
  23. }
  24. #endregion
  25. #region 隐藏身份证号
  26. public static string HideCertId(string str)
  27. {
  28. return str.Substring(0, 3) + "***********" + str.Substring(14);
  29. }
  30. #endregion
  31. #region 隐藏姓名
  32. public static string HideRealName(string str)
  33. {
  34. string result = "";
  35. for (int i = 0; i < str.Length; i++)
  36. {
  37. if (i == 0)
  38. {
  39. result += str.Substring(i, 1);
  40. }
  41. else
  42. {
  43. result += "*";
  44. }
  45. }
  46. return result;
  47. }
  48. #endregion
  49. #region 姓名脱敏
  50. /// <summary>
  51. /// 姓名敏感处理
  52. /// </summary>
  53. /// <param name="fullName">姓名</param>
  54. /// <returns>脱敏后的姓名</returns>
  55. public static string SetSensitiveName(string fullName)
  56. {
  57. if (string.IsNullOrEmpty(fullName)) return string.Empty;
  58. string familyName = fullName.Substring(0, 1);
  59. string end = fullName.Substring(fullName.Length - 1, 1);
  60. string name = string.Empty;
  61. //长度为2
  62. if (fullName.Length <= 2) name = familyName + "*";
  63. //长度⼤于2
  64. else if (fullName.Length >= 3)
  65. {
  66. name = familyName.PadRight(fullName.Length - 1, '*') + end;
  67. }
  68. return name;
  69. }
  70. #endregion
  71. #region 统计机具绑定数据
  72. public static void StatUserMachineData(int UserId, int BrandId, int Count)
  73. {
  74. using (WebCMSEntities db = new WebCMSEntities())
  75. {
  76. string IdBrand = UserId + "_" + BrandId;
  77. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  78. if (MachineData == null)
  79. {
  80. MachineData = db.UserMachineData.Add(new UserMachineData()
  81. {
  82. IdBrand = IdBrand,
  83. }).Entity;
  84. db.SaveChanges();
  85. }
  86. if(Count < 0)
  87. {
  88. MachineData.TotalMachineCount -= Math.Abs(Count);
  89. MachineData.UnBindCount -= Math.Abs(Count);
  90. }
  91. else
  92. {
  93. MachineData.TotalMachineCount += Count;
  94. MachineData.UnBindCount += Count;
  95. }
  96. db.SaveChanges();
  97. }
  98. }
  99. #endregion
  100. #region 设置押金添加记录公共方法
  101. public static void MerchantDepositSet(int BrandId, int UserId, int SnId, string SnNo, int BeforeDeposit, decimal DepositAmount, string ReturnNote)
  102. {
  103. try
  104. {
  105. WebCMSEntities maindb = new WebCMSEntities();
  106. MerchantDepositSet query = maindb.MerchantDepositSet.Add(new MerchantDepositSet()
  107. {
  108. CreateDate = DateTime.Now, //创建时间
  109. Sort = BrandId,//品牌
  110. SeoTitle = BeforeDeposit.ToString(),//变更前押金
  111. DepositAmount = DepositAmount,//押金
  112. ReturnNote = ReturnNote,//返回信息
  113. SnNo = SnNo,//机具Sn
  114. UserId = UserId,//创客Id
  115. }).Entity;
  116. maindb.SaveChanges();
  117. }
  118. catch (Exception ex)
  119. {
  120. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + ex.ToString(), "设置押金队列异常");
  121. }
  122. }
  123. #endregion
  124. #region 获取jwt的token
  125. public static string AppToken(int UserId, string JwtSecret, string JwtIss, string tag = "")
  126. {
  127. string Token = RedisDbconn.Instance.Get<string>("apptoken:" + UserId + tag);
  128. if(!string.IsNullOrEmpty(Token))
  129. {
  130. return Token;
  131. }
  132. string issuer = "new_" + UserId + tag;
  133. string test = function.get_Random(10);
  134. var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtSecret)), SecurityAlgorithms.HmacSha256);
  135. var claims = new Claim[] {
  136. new Claim(JwtRegisteredClaimNames.Iss,JwtIss),
  137. new Claim(JwtRegisteredClaimNames.Aud,test),
  138. new Claim("Guid", Guid.NewGuid().ToString("D")),
  139. new Claim(ClaimTypes.Role, "system"),
  140. new Claim(ClaimTypes.Role, "admin"),
  141. };
  142. SecurityToken securityToken = new JwtSecurityToken(
  143. signingCredentials: securityKey,
  144. expires: DateTime.Now.AddDays(10),//过期时间
  145. claims: claims,
  146. audience: test,
  147. issuer: issuer
  148. );
  149. RedisDbconn.Instance.Set("utoken:" + issuer, test);
  150. RedisDbconn.Instance.SetExpire("utoken:" + issuer, 3600 * 24 * 10);
  151. //生成jwt令牌
  152. Token = new JwtSecurityTokenHandler().WriteToken(securityToken);
  153. RedisDbconn.Instance.Set("apptoken:" + UserId + tag, Token);
  154. RedisDbconn.Instance.SetExpire("apptoken:" + UserId + tag, 3600 * 24 * 10 - 60);
  155. //生成jwt令牌
  156. return new JwtSecurityTokenHandler().WriteToken(securityToken);
  157. }
  158. #endregion
  159. //收支明细类别结合
  160. public static List<int> IncomeTypes()
  161. {
  162. List<int> list = new List<int>();
  163. list.Add(0);
  164. list.Add(1);
  165. list.Add(12);
  166. list.Add(31);
  167. list.Add(50);
  168. list.Add(60);
  169. list.Add(64);
  170. list.Add(66);
  171. list.Add(111);
  172. list.Add(112);
  173. list.Add(113);
  174. list.Add(114);
  175. list.Add(115);
  176. list.Add(116);
  177. list.Add(117);
  178. list.Add(118);
  179. list.Add(119);
  180. list.Add(120);
  181. list.Add(121);
  182. list.Add(122);
  183. list.Add(123);
  184. list.Add(126);
  185. return list;
  186. }
  187. public static List<int> ExpendTypes()
  188. {
  189. List<int> list = new List<int>();
  190. list.Add(3);
  191. list.Add(4);
  192. list.Add(5);
  193. list.Add(20);
  194. list.Add(63);
  195. list.Add(65);
  196. list.Add(124);
  197. list.Add(125);
  198. return list;
  199. }
  200. }
  201. }