PublicFunction.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. WebCMSEntities db = new WebCMSEntities();
  75. string IdBrand = UserId + "_" + BrandId;
  76. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  77. if (MachineData == null)
  78. {
  79. MachineData = db.UserMachineData.Add(new UserMachineData()
  80. {
  81. IdBrand = IdBrand,
  82. }).Entity;
  83. db.SaveChanges();
  84. }
  85. if(Count < 0)
  86. {
  87. MachineData.TotalMachineCount -= Math.Abs(Count);
  88. MachineData.UnBindCount -= Math.Abs(Count);
  89. }
  90. else
  91. {
  92. MachineData.TotalMachineCount += Count;
  93. MachineData.UnBindCount += Count;
  94. }
  95. db.SaveChanges();
  96. db.Dispose();
  97. }
  98. #endregion
  99. #region 绑定
  100. public static void BindUserMachineData(WebCMSEntities db, int UserId, int BrandId, int Count, string PosSn)
  101. {
  102. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PosMachinesTwo();
  103. pos.BindingState = 1;
  104. pos.BindingTime = DateTime.Now;
  105. string IdBrand = UserId + "_" + BrandId;
  106. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  107. if (MachineData == null)
  108. {
  109. MachineData = db.UserMachineData.Add(new UserMachineData()
  110. {
  111. IdBrand = IdBrand,
  112. }).Entity;
  113. db.SaveChanges();
  114. }
  115. MachineData.UnBindCount -= Count;
  116. MachineData.BindCount += Count;
  117. db.SaveChanges();
  118. }
  119. #endregion
  120. #region 划拨记录
  121. public static void SendRecord(WebCMSEntities db, int UserId, int ToUserId, int StoreId, string[] PosSnList)
  122. {
  123. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  124. for (int i = 0; i < PosSnList.Length; i++)
  125. {
  126. string PosSn = PosSnList[i];
  127. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PosMachinesTwo();
  128. pos.StoreId = StoreId;
  129. pos.TransferTime = DateTime.Now;
  130. pos.BuyUserId = ToUserId;
  131. pos.UserId = ToUserId;
  132. db.PosCouponRecord.Add(new PosCouponRecord()
  133. {
  134. CreateDate = DateTime.Now,
  135. OrderNo = ChangeRecordNo,
  136. ToUserId = ToUserId,
  137. FromUserId = UserId,
  138. PosCouponId = pos.Id,
  139. });
  140. }
  141. int SnCount = PosSnList.Length;
  142. db.PosCouponOrders.Add(new PosCouponOrders()
  143. {
  144. CreateDate = DateTime.Now,
  145. ChangeCount = SnCount,
  146. OrderNo = ChangeRecordNo,
  147. ToUserId = ToUserId,
  148. FromUserId = UserId,
  149. });
  150. db.SaveChanges();
  151. }
  152. #endregion
  153. #region 获取jwt的token
  154. public static string AppToken(int UserId, string JwtSecret, string JwtIss, string tag = "")
  155. {
  156. string Token = RedisDbconn.Instance.Get<string>("apptoken:" + UserId + tag);
  157. if(!string.IsNullOrEmpty(Token))
  158. {
  159. return Token;
  160. }
  161. string issuer = "new_" + UserId + tag;
  162. string test = function.get_Random(10);
  163. var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtSecret)), SecurityAlgorithms.HmacSha256);
  164. var claims = new Claim[] {
  165. new Claim(JwtRegisteredClaimNames.Iss,JwtIss),
  166. new Claim(JwtRegisteredClaimNames.Aud,test),
  167. new Claim("Guid", Guid.NewGuid().ToString("D")),
  168. new Claim(ClaimTypes.Role, "system"),
  169. new Claim(ClaimTypes.Role, "admin"),
  170. };
  171. SecurityToken securityToken = new JwtSecurityToken(
  172. signingCredentials: securityKey,
  173. expires: DateTime.Now.AddDays(10),//过期时间
  174. claims: claims,
  175. audience: test,
  176. issuer: issuer
  177. );
  178. RedisDbconn.Instance.Set("utoken:" + issuer, test);
  179. RedisDbconn.Instance.SetExpire("utoken:" + issuer, 3600 * 24 * 10);
  180. //生成jwt令牌
  181. Token = new JwtSecurityTokenHandler().WriteToken(securityToken);
  182. RedisDbconn.Instance.Set("apptoken:" + UserId + tag, Token);
  183. RedisDbconn.Instance.SetExpire("apptoken:" + UserId + tag, 3600 * 24 * 10 - 60);
  184. //生成jwt令牌
  185. return new JwtSecurityTokenHandler().WriteToken(securityToken);
  186. }
  187. #endregion
  188. //收支明细类别结合
  189. public static List<int> IncomeTypes()
  190. {
  191. List<int> list = new List<int>();
  192. list.Add(0);
  193. list.Add(1);
  194. list.Add(12);
  195. list.Add(31);
  196. list.Add(50);
  197. list.Add(60);
  198. list.Add(64);
  199. list.Add(66);
  200. list.Add(111);
  201. list.Add(112);
  202. list.Add(113);
  203. list.Add(114);
  204. list.Add(115);
  205. list.Add(116);
  206. list.Add(117);
  207. list.Add(118);
  208. list.Add(119);
  209. list.Add(120);
  210. list.Add(121);
  211. list.Add(122);
  212. list.Add(123);
  213. list.Add(126);
  214. return list;
  215. }
  216. public static List<int> ExpendTypes()
  217. {
  218. List<int> list = new List<int>();
  219. list.Add(3);
  220. list.Add(4);
  221. list.Add(5);
  222. list.Add(20);
  223. list.Add(63);
  224. list.Add(65);
  225. list.Add(124);
  226. list.Add(125);
  227. return list;
  228. }
  229. }
  230. }