123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MySystem.MainModels;
- using Library;
- using Microsoft.IdentityModel.Tokens;
- using System.IdentityModel.Tokens.Jwt;
- using System.Security.Claims;
- using System.Text;
- namespace MySystem
- {
- public class PublicFunction
- {
- #region 根据ParentNav获取顶级创客Id
- public static int GetTopUserId(string ParentNav)
- {
- int TopUserId = 0;
- if (!string.IsNullOrEmpty(ParentNav))
- {
- TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
- }
- return TopUserId;
- }
- #endregion
- #region 隐藏身份证号
- public static string HideCertId(string str)
- {
- return str.Substring(0, 3) + "***********" + str.Substring(14);
- }
- #endregion
- #region 隐藏姓名
- public static string HideRealName(string str)
- {
- string result = "";
- for (int i = 0; i < str.Length; i++)
- {
- if (i == 0)
- {
- result += str.Substring(i, 1);
- }
- else
- {
- result += "*";
- }
- }
- return result;
- }
- #endregion
- #region 姓名脱敏
- /// <summary>
- /// 姓名敏感处理
- /// </summary>
- /// <param name="fullName">姓名</param>
- /// <returns>脱敏后的姓名</returns>
- public static string SetSensitiveName(string fullName)
- {
- if (string.IsNullOrEmpty(fullName)) return string.Empty;
- string familyName = fullName.Substring(0, 1);
- string end = fullName.Substring(fullName.Length - 1, 1);
- string name = string.Empty;
- //长度为2
- if (fullName.Length <= 2) name = familyName + "*";
- //长度⼤于2
- else if (fullName.Length >= 3)
- {
- name = familyName.PadRight(fullName.Length - 1, '*') + end;
- }
- return name;
- }
- #endregion
- #region 统计机具绑定数据
- public static void StatUserMachineData(int UserId, int BrandId, int Count)
- {
- WebCMSEntities db = new WebCMSEntities();
- string IdBrand = UserId + "_" + BrandId;
- UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
- if (MachineData == null)
- {
- MachineData = db.UserMachineData.Add(new UserMachineData()
- {
- IdBrand = IdBrand,
- }).Entity;
- db.SaveChanges();
- }
- if(Count < 0)
- {
- MachineData.TotalMachineCount -= Math.Abs(Count);
- MachineData.UnBindCount -= Math.Abs(Count);
- }
- else
- {
- MachineData.TotalMachineCount += Count;
- MachineData.UnBindCount += Count;
- }
- db.SaveChanges();
- db.Dispose();
- }
- #endregion
- #region 绑定
- public static void BindUserMachineData(WebCMSEntities db, int UserId, int BrandId, int Count, string PosSn)
- {
- PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PosMachinesTwo();
- pos.BindingState = 1;
- pos.BindingTime = DateTime.Now;
- string IdBrand = UserId + "_" + BrandId;
- UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
- if (MachineData == null)
- {
- MachineData = db.UserMachineData.Add(new UserMachineData()
- {
- IdBrand = IdBrand,
- }).Entity;
- db.SaveChanges();
- }
- MachineData.UnBindCount -= Count;
- MachineData.BindCount += Count;
- db.SaveChanges();
- }
- #endregion
- #region 划拨记录
- public static void SendRecord(WebCMSEntities db, int UserId, int ToUserId, int StoreId, string[] PosSnList)
- {
- string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
- for (int i = 0; i < PosSnList.Length; i++)
- {
- string PosSn = PosSnList[i];
- PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PosMachinesTwo();
- pos.StoreId = StoreId;
- pos.TransferTime = DateTime.Now;
- pos.BuyUserId = ToUserId;
- pos.UserId = ToUserId;
- db.PosCouponRecord.Add(new PosCouponRecord()
- {
- CreateDate = DateTime.Now,
- OrderNo = ChangeRecordNo,
- ToUserId = ToUserId,
- FromUserId = UserId,
- PosCouponId = pos.Id,
- });
- }
- int SnCount = PosSnList.Length;
- db.PosCouponOrders.Add(new PosCouponOrders()
- {
- CreateDate = DateTime.Now,
- ChangeCount = SnCount,
- OrderNo = ChangeRecordNo,
- ToUserId = ToUserId,
- FromUserId = UserId,
- });
- db.SaveChanges();
- }
- #endregion
- #region 获取jwt的token
- public static string AppToken(int UserId, string JwtSecret, string JwtIss, string tag = "")
- {
- string Token = RedisDbconn.Instance.Get<string>("apptoken:" + UserId + tag);
- if(!string.IsNullOrEmpty(Token))
- {
- return Token;
- }
- string issuer = "new_" + UserId + tag;
- string test = function.get_Random(10);
- var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtSecret)), SecurityAlgorithms.HmacSha256);
- var claims = new Claim[] {
- new Claim(JwtRegisteredClaimNames.Iss,JwtIss),
- new Claim(JwtRegisteredClaimNames.Aud,test),
- new Claim("Guid", Guid.NewGuid().ToString("D")),
- new Claim(ClaimTypes.Role, "system"),
- new Claim(ClaimTypes.Role, "admin"),
- };
- SecurityToken securityToken = new JwtSecurityToken(
- signingCredentials: securityKey,
- expires: DateTime.Now.AddDays(10),//过期时间
- claims: claims,
- audience: test,
- issuer: issuer
- );
- RedisDbconn.Instance.Set("utoken:" + issuer, test);
- RedisDbconn.Instance.SetExpire("utoken:" + issuer, 3600 * 24 * 10);
- //生成jwt令牌
- Token = new JwtSecurityTokenHandler().WriteToken(securityToken);
- RedisDbconn.Instance.Set("apptoken:" + UserId + tag, Token);
- RedisDbconn.Instance.SetExpire("apptoken:" + UserId + tag, 3600 * 24 * 10 - 60);
- //生成jwt令牌
- return new JwtSecurityTokenHandler().WriteToken(securityToken);
- }
- #endregion
- //收支明细类别结合
- public static List<int> IncomeTypes()
- {
- List<int> list = new List<int>();
- list.Add(0);
- list.Add(1);
- list.Add(12);
- list.Add(31);
- list.Add(50);
- list.Add(60);
- list.Add(64);
- list.Add(66);
- list.Add(111);
- list.Add(112);
- list.Add(113);
- list.Add(114);
- list.Add(115);
- list.Add(116);
- list.Add(117);
- list.Add(118);
- list.Add(119);
- list.Add(120);
- list.Add(121);
- list.Add(122);
- list.Add(123);
- list.Add(126);
- return list;
- }
- public static List<int> ExpendTypes()
- {
- List<int> list = new List<int>();
- list.Add(3);
- list.Add(4);
- list.Add(5);
- list.Add(20);
- list.Add(63);
- list.Add(65);
- list.Add(124);
- list.Add(125);
- return list;
- }
- }
- }
|