123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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<Account> 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<ContactDocExt> 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<string, Permission> 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<string, object>()
- {
- { "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;
- }
- }
- }
|