123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using LeaRun.Cache.Factory;
- using LeaRun.Util;
- using System;
- namespace LeaRun.Application.Code
- {
- /// <summary>
- /// 版 本 6.1
- /// Copyright (c) 2013-2016 上海力软信息技术有限公司
- /// 创建人:佘赐雄
- /// 日 期:2015.10.10
- /// 描 述:当前操作者回话
- /// </summary>
- public class OperatorProvider : OperatorIProvider
- {
- #region 静态实例
- /// <summary>
- /// 当前提供者
- /// </summary>
- public static OperatorIProvider Provider
- {
- get { return new OperatorProvider(); }
- }
- /// <summary>
- /// 给app调用
- /// </summary>
- public static string AppUserId
- {
- set;
- get;
- }
- #endregion
- /// <summary>
- /// 秘钥
- /// </summary>
- private string LoginUserKey = "Learun_LoginUserKey_2016_V6.1";
- /// <summary>
- /// 登陆提供者模式:Session、Cookie
- /// </summary>
- private string LoginProvider = Config.GetValue("LoginProvider");
- /// <summary>
- /// 写入登录信息
- /// </summary>
- /// <param name="user">成员信息</param>
- public virtual void AddCurrent(Operator user)
- {
- try
- {
- if (LoginProvider == "Cookie")
- {
- #region 解决cookie时,设置数据权限较多时无法登陆的bug
- CacheFactory.Cache().WriteCache(user.DataAuthorize, LoginUserKey, user.LogTime.AddHours(12));
- user.DataAuthorize = null;
- #endregion
- WebHelper.WriteCookie(LoginUserKey, DESEncrypt.Encrypt(user.ToJson()));
- }
- else
- {
- WebHelper.WriteSession(LoginUserKey, DESEncrypt.Encrypt(user.ToJson()));
- }
- CacheFactory.Cache().WriteCache(user.Token, user.UserId, user.LogTime.AddHours(12));
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- /// <summary>
- /// 当前用户
- /// </summary>
- /// <returns></returns>
- public virtual Operator Current()
- {
- try
- {
- Operator user = new Operator();
- if (LoginProvider == "Cookie")
- {
- user = DESEncrypt.Decrypt(WebHelper.GetCookie(LoginUserKey).ToString()).ToObject<Operator>();
- #region 解决cookie时,设置数据权限较多时无法登陆的bug
- AuthorizeDataModel dataAuthorize = CacheFactory.Cache().GetCache<AuthorizeDataModel>(LoginUserKey);
- user.DataAuthorize = dataAuthorize;
- #endregion
- }
- else if (LoginProvider == "AppClient")
- {
- user = CacheFactory.Cache().GetCache<Operator>(AppUserId);
- }
- else
- {
- user = DESEncrypt.Decrypt(WebHelper.GetSession(LoginUserKey).ToString()).ToObject<Operator>();
- }
- return user;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- /// <summary>
- /// 删除登录信息
- /// </summary>
- public virtual void EmptyCurrent()
- {
- if (LoginProvider == "Cookie")
- {
- WebHelper.RemoveCookie(LoginUserKey.Trim());
- #region 解决cookie时,设置数据权限较多时无法登陆的bug
- CacheFactory.Cache().RemoveCache(LoginUserKey);
- #endregion
- }
- else
- {
- WebHelper.RemoveSession(LoginUserKey.Trim());
- }
- }
- /// <summary>
- /// 是否过期
- /// </summary>
- /// <returns></returns>
- public virtual bool IsOverdue()
- {
- try
- {
- object str = "";
- AuthorizeDataModel dataAuthorize = null;
- if (LoginProvider == "Cookie")
- {
- str = WebHelper.GetCookie(LoginUserKey);
- #region 解决cookie时,设置数据权限较多时无法登陆的bug
- dataAuthorize = CacheFactory.Cache().GetCache<AuthorizeDataModel>(LoginUserKey);
- if (dataAuthorize == null)
- {
- return true;
- }
- #endregion
- }
- else
- {
- str = WebHelper.GetSession(LoginUserKey);
- }
- if (str != null && str.ToString() != "")
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- catch (Exception)
- {
- return true;
- }
- }
- /// <summary>
- /// 是否已登录
- /// </summary>
- /// <returns></returns>
- public virtual int IsOnLine()
- {
- Operator user = new Operator();
- if (LoginProvider == "Cookie")
- {
- user = DESEncrypt.Decrypt(WebHelper.GetCookie(LoginUserKey).ToString()).ToObject<Operator>();
- #region 解决cookie时,设置数据权限较多时无法登陆的bug
- AuthorizeDataModel dataAuthorize = CacheFactory.Cache().GetCache<AuthorizeDataModel>(LoginUserKey);
- user.DataAuthorize = dataAuthorize;
- #endregion
- }
- else
- {
- user = DESEncrypt.Decrypt(WebHelper.GetSession(LoginUserKey).ToString()).ToObject<Operator>();
- }
- object token = CacheFactory.Cache().GetCache<string>(user.UserId);
- if (token == null)
- {
- return -1;//过期
- }
- if (user.Token == token.ToString())
- {
- return 1;//正常
- }
- else
- {
- return 0;//已登录
- }
- }
- }
- }
|