using Common.Config; using Common.Wechat; using CoreEntity.DAL; using CoreEntity.Entity; using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using ZcPeng.PublicLibrary; using ZcPeng.weixin.PublicAccount; namespace SupplierWeb.Codes.Auth { public class Token { private Guid _StaffId; private string _SignToken; private DateTime _ExpireTime; public Guid StaffId { get { return _StaffId; } set { _StaffId = value; } } public string SignToken { get { return _SignToken; } set { _SignToken = value; } } public DateTime ExpireTime { get { return _ExpireTime; } set { _ExpireTime = value; } } public static Token genAndSaveToken(IList users,IMemoryCache _cache,out string RoleName, AccessToken restoken = null) { var roleid = users[0].RoleId; var username = users[0].AccountRealName; //生成token #region 生成token if (roleid == 3) { //List list = ContactDAL.getContactorByAccountId(users[0].Id.ToString()); //if (list == null|| list.Count == 0) { // return Json(new { success = false, msg = "登陆错误,账户没有绑定公众号", user = new { id = "", username = username, password = password } }); //} } Dictionary permission = PermissionHelper.Permissions(users[0].RoleId); string results; object roleName = ""; bool success = DataAccess.GetOneValue("select RoleRemark from " + Config.TablePrefix + "Role where id=" + roleid, out roleName, out results); RoleName = roleName ==null?"":roleName.ToString(); var payload = new Dictionary() { { "roleid", roleid }, { "rolename", roleName }, { "username", username }, { "userid", users[0].Id }, { "CurrentEmployeeID", users[0].PurStaffId }, { "CurrentDeptID", "" }, //{ "CurrentSupplierID", suppliersids }, { "CurrentUserLastLoginTime", users[0].LastLoginTime }, { "permission", permission }, { "wxresponse", restoken } }; var secretKey = TokenConfig.SecretKey; string SignToken = Jwt.JsonWebToken.Encode(payload, secretKey, Jwt.JwtHashAlgorithm.HS256); Token token = new Token(); token.StaffId = Guid.NewGuid(); token.SignToken = SignToken; token.ExpireTime = DateTime.Now.AddSeconds(TokenConfig.ExpireTime); _cache.GetOrCreate(token.StaffId.ToString(), entry => { entry.SetAbsoluteExpiration(token.ExpireTime); return token.SignToken; }); #endregion return token; } } }