Token.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Common.Config;
  2. using Common.Wechat;
  3. using CoreEntity.DAL;
  4. using CoreEntity.Entity;
  5. using Microsoft.Extensions.Caching.Memory;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using ZcPeng.PublicLibrary;
  11. using ZcPeng.weixin.PublicAccount;
  12. namespace SupplierWeb.Codes.Auth
  13. {
  14. public class Token
  15. {
  16. private Guid _StaffId;
  17. private string _SignToken;
  18. private DateTime _ExpireTime;
  19. public Guid StaffId
  20. {
  21. get { return _StaffId; }
  22. set { _StaffId = value; }
  23. }
  24. public string SignToken
  25. {
  26. get { return _SignToken; }
  27. set { _SignToken = value; }
  28. }
  29. public DateTime ExpireTime
  30. {
  31. get { return _ExpireTime; }
  32. set { _ExpireTime = value; }
  33. }
  34. public static Token genAndSaveToken(IList<Account> users,IMemoryCache _cache,out string RoleName, AccessToken restoken = null)
  35. {
  36. var roleid = users[0].RoleId;
  37. var username = users[0].AccountRealName;
  38. //取供应商id
  39. var sups = ContactDAL.getContactorByAccountId(users[0].Id.ToString());
  40. string suppliersids = "";
  41. foreach (ContactDocExt cd in sups)
  42. {
  43. suppliersids = suppliersids + cd.BusinessId + ",";
  44. }
  45. suppliersids = suppliersids.TrimEnd(',');
  46. //生成token
  47. #region 生成token
  48. if (roleid == 3)
  49. {
  50. //List<ContactDocExt> list = ContactDAL.getContactorByAccountId(users[0].Id.ToString());
  51. //if (list == null|| list.Count == 0) {
  52. // return Json(new { success = false, msg = "登陆错误,账户没有绑定公众号", user = new { id = "", username = username, password = password } });
  53. //}
  54. }
  55. Dictionary<string, Permission> permission = PermissionHelper.Permissions(users[0].RoleId);
  56. string results;
  57. object roleName = "";
  58. bool success = DataAccess.GetOneValue("select RoleRemark from " + Config.TablePrefix + "Role where id=" + roleid, out roleName, out results);
  59. RoleName = roleName ==null?"":roleName.ToString();
  60. var payload = new Dictionary<string, object>()
  61. {
  62. { "roleid", roleid },
  63. { "rolename", roleName },
  64. { "username", username },
  65. { "userid", users[0].Id },
  66. { "CurrentEmployeeID", users[0].PurStaffId },
  67. { "CurrentDeptID", "" },
  68. { "CurrentSupplierID", suppliersids },
  69. { "CurrentUserLastLoginTime", users[0].LastLoginTime },
  70. { "permission", permission },
  71. { "wxresponse", restoken }
  72. };
  73. var secretKey = TokenConfig.SecretKey;
  74. string SignToken = Jwt.JsonWebToken.Encode(payload, secretKey, Jwt.JwtHashAlgorithm.HS256);
  75. Token token = new Token();
  76. token.StaffId = Guid.NewGuid();
  77. token.SignToken = SignToken;
  78. token.ExpireTime = DateTime.Now.AddSeconds(TokenConfig.ExpireTime);
  79. _cache.GetOrCreate(token.StaffId.ToString(), entry =>
  80. {
  81. entry.SetAbsoluteExpiration(token.ExpireTime);
  82. return token.SignToken;
  83. });
  84. #endregion
  85. return token;
  86. }
  87. }
  88. }