PublicFunction.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. return AppToken(UserId.ToString(), JwtSecret, JwtIss, tag);
  157. }
  158. public static string AppToken(string UserId, string JwtSecret, string JwtIss, string tag = "")
  159. {
  160. string Token = RedisDbconn.Instance.Get<string>("apptoken:" + UserId + tag);
  161. if(!string.IsNullOrEmpty(Token))
  162. {
  163. return Token;
  164. }
  165. string issuer = "new_" + UserId + tag;
  166. string test = function.get_Random(10);
  167. var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtSecret)), SecurityAlgorithms.HmacSha256);
  168. var claims = new Claim[] {
  169. new Claim(JwtRegisteredClaimNames.Iss,JwtIss),
  170. new Claim(JwtRegisteredClaimNames.Aud,test),
  171. new Claim("Guid", Guid.NewGuid().ToString("D")),
  172. new Claim(ClaimTypes.Role, "system"),
  173. new Claim(ClaimTypes.Role, "admin"),
  174. };
  175. SecurityToken securityToken = new JwtSecurityToken(
  176. signingCredentials: securityKey,
  177. expires: DateTime.Now.AddDays(10),//过期时间
  178. claims: claims,
  179. audience: test,
  180. issuer: issuer
  181. );
  182. RedisDbconn.Instance.Set("utoken:" + issuer, test);
  183. RedisDbconn.Instance.SetExpire("utoken:" + issuer, 3600 * 24 * 10);
  184. //生成jwt令牌
  185. Token = new JwtSecurityTokenHandler().WriteToken(securityToken);
  186. RedisDbconn.Instance.Set("apptoken:" + UserId + tag, Token);
  187. RedisDbconn.Instance.SetExpire("apptoken:" + UserId + tag, 3600 * 24 * 10 - 60);
  188. //生成jwt令牌
  189. return new JwtSecurityTokenHandler().WriteToken(securityToken);
  190. }
  191. #endregion
  192. //收支明细类别结合
  193. public static List<int> IncomeTypes()
  194. {
  195. List<int> list = new List<int>();
  196. list.Add(0);
  197. list.Add(1);
  198. list.Add(12);
  199. list.Add(31);
  200. list.Add(50);
  201. list.Add(60);
  202. list.Add(64);
  203. list.Add(66);
  204. list.Add(111);
  205. list.Add(112);
  206. list.Add(113);
  207. list.Add(114);
  208. list.Add(115);
  209. list.Add(116);
  210. list.Add(117);
  211. list.Add(118);
  212. list.Add(119);
  213. list.Add(120);
  214. list.Add(121);
  215. list.Add(122);
  216. list.Add(123);
  217. list.Add(126);
  218. return list;
  219. }
  220. public static List<int> ExpendTypes()
  221. {
  222. List<int> list = new List<int>();
  223. list.Add(3);
  224. list.Add(4);
  225. list.Add(5);
  226. list.Add(20);
  227. list.Add(63);
  228. list.Add(65);
  229. list.Add(124);
  230. list.Add(125);
  231. return list;
  232. }
  233. }
  234. }