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