PublicFunction.cs 8.8 KB

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