using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugar;
using WMS.Util;
using WMS.Info;
namespace WMS.Core
{
public class LoginBLLCore
{
///
/// 检测登录用户有效性
///
public static ACL_USERITEM CheckLoginUser(string UserNo, string UserPwd, EAppType AppType, string WarehouseNo, List WAreaNoList, SqlSugarClient Ctx)
{
try
{
if (string.IsNullOrWhiteSpace(UserNo))
throw SysExCore.ThrowFailException("登录用户名为空!!!");
if (string.IsNullOrWhiteSpace(UserPwd))
throw SysExCore.ThrowFailException("登录密码为空!!!");
if (AppType == EAppType.TV || AppType == EAppType.Interface)
throw SysExCore.ThrowFailException("登录APP类型不正确!!!");
//离线仓库登录
//if (AppType == EAppType.OffLineRF)
//{
// if (string.IsNullOrWhiteSpace(WarehouseNo))
// throw SysExCore.ThrowFailException("仓库号为空!!!");
// //判断仓库
// var wh = Ctx.Queryable().Where(it => it.F_NO == WarehouseNo).First();
// if (wh == null)
// throw SysExCore.ThrowFailException("输入参数中仓库号在仓库列表中不存在!!!");
// if (WAreaNoList == null || WAreaNoList.Count == 0)
// throw SysExCore.ThrowFailException("仓库区域为空!!!");
// if (wh.F_TYPENUM != (int)EWarehouseType.OfflineRFWarehouse)
// throw SysExCore.ThrowFailException("输入参数中仓库号不是离线仓库类型!!!");
// //判断区域与仓库有没有匹配
// var walist = Ctx.Queryable().Where(it => it.F_WAREHOUSENO == WarehouseNo).ToList();
// if (walist == null || walist.Count == 0)
// throw SysExCore.ThrowFailException("输入参数中区域号在仓库区域列表中不存在!!!");
// if (WAreaNoList.Any(it => !walist.Any(b => it == b.F_NO)))
// throw SysExCore.ThrowFailException("输入参数仓库号与区域号不匹配!!!");
//}
//获取数据
var user = Ctx.Queryable().Where(it => it.F_NO.ToUpper() == UserNo.ToUpper()).First();
if (user == null)
throw SysExCore.ThrowFailException("无法找到指定用户");
if (user.F_ISDELETE > 0)
throw SysExCore.ThrowFailException("用户已删除!!!");
if (user.F_ISSTOP > 0)
throw SysExCore.ThrowFailException("用户已停用!!!");
if (user.F_AUTOSTOPTIME < DateTime.Now)
throw SysExCore.ThrowFailException("用户已过期!!!");
if (user.F_PWDERRQTY >= SysSetCore.GetSysSet().UserPwdErrQty)
throw SysExCore.ThrowFailException(string.Format("用户密码错误已超过{0}次,已被锁定。", SysSetCore.GetSysSet().UserPwdErrQty));
//用户密码已过期
if (DateTime.MaxValue!=user.F_EDITPWDTIME)
{
if (user.F_EDITPWDTIME.AddDays(SysSetCore.GetSysSet().UserPwdExpD) < DateTime.Now)
throw SysExCore.ThrowPwdExpiration();
}
//用户密码错误
if (user.F_PASSWORD != SysSecurityCore.Aes256Encrypt(UserPwd))
{
int qty = SysSetCore.GetSysSet().UserPwdErrQty - user.F_PWDERRQTY;
if (user.F_PWDERRQTY > 0)
throw new SysExCore(ESysExType.PwdError, string.Format("用户密码错误,您还有{0}次", qty > 0 ? qty : 0));
else
throw new SysExCore(ESysExType.PwdError, "用户密码错误。");
}
if (user.F_PASSWORD == SysSecurityCore.Aes256Encrypt(SysSetCore.GetSysSet().PwdInit))
{
throw new SysExCore(ESysExType.PwdInit, "请将初始化密码进行修改。");
}
return user;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 是否是管理员
///
public static bool CheckAdmin(string UserNo)
{
bool IsAdmin = false;
EUserType UserType = GetUserType(UserNo);
if (UserType == EUserType.SuperAdmin || UserType == EUserType.Super || UserType == EUserType.System)
{
IsAdmin = true;
}
return IsAdmin;
}
///
/// 获取用户类型
///
public static EUserType GetUserType(string UserNo)
{
EUserType UserType = EUserType.User;
try
{
if (UserNo.ToUpper() == EUserType.Super.ToString().ToUpper())
{
UserType = EUserType.Super;
}
else if (UserNo.ToUpper() == EUserType.System.ToString().ToUpper())
{
UserType = EUserType.System;
}
else if (UserNo.ToUpper() == EUserType.SuperAdmin.ToString().ToUpper())
{
UserType = EUserType.SuperAdmin;
}
else
{
UserType = EUserType.User;
}
//权限管理员
}
catch
{
}
return UserType;
}
///
/// 更新用户密码错误次数
///
public static void UpdatePwdErr(string UserNo)
{
var i = SysDbCore.GetDbCtx().Updateable().SetColumns(it => new ACL_USERITEM() { F_PWDERRQTY = it.F_PWDERRQTY + 1, F_EDITTIME = DateTime.Now, F_EDITUSERNO = UserNo }).Where(it => it.F_NO.ToUpper() == UserNo.ToUpper()).ExecuteCommand();
if (i <= 0)
throw SysExCore.ThrowFailException("更新用户密码登录错误次数失败!!!");
}
///
/// 检测登录状态
///
/// 加密TokenNo
public static void LoginTokenCheck(string EncryptTokenNo)
{
try
{
if (string.IsNullOrWhiteSpace(EncryptTokenNo))
{
throw SysExCore.ThrowToken();
}
SqlSugarClient Ctx = SysDbCore.GetDbCtx();
string TokenNo = SysSecurityCore.Aes256Decrypt(EncryptTokenNo);
ACL_USERTOKEN token = Ctx.Queryable().Where(a => a.F_NO == TokenNo).First();
if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
{
throw SysExCore.ThrowToken();
}
if (token.F_APPTYPENUM != (int)EAppType.OffLineRF)
{
if (token.F_ETIME != DateTime.MaxValue)
{
if (token.F_ETIME.AddMinutes(SysSetCore.GetSysSet().OnLoginTimeOutM) < DateTime.Now)
throw SysExCore.ThrowLoginTimeout();
}
Ctx.Updateable().SetColumns(it => it.F_ETIME == DateTime.Now).Where(a => a.F_NO == TokenNo).ExecuteCommand();
}
}
catch (Exception ex)
{
throw ex;
}
}
//
/// 检测登录状态
///
public static void LoginTokenCheck()
{
LoginTokenCheck(SessionCookieCore.GetLoginTokenNo());
}
///
/// 检测登录状态
///
/// 加密TokenNo
public static void LoginTokenCheck(ReqInfo ReqData)
{
try
{
if (ReqData == null)
{
throw SysExCore.ThrowInEmpty();
}
LoginTokenCheck(ReqData.EncryptTokenNo);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取用户登录数据
///
public static LoginUserInfo GetLoginUser(string EncryptTokenNo)
{
try
{
LoginUserInfo LoginUser = new LoginUserInfo();
void action(SqlSugarClient ctx)
{
if (string.IsNullOrWhiteSpace(EncryptTokenNo))
{
throw SysExCore.ThrowFailException("登录凭证为空。");
}
string TokenNo = SysSecurityCore.Aes256Decrypt(EncryptTokenNo);
ACL_USERTOKEN token = ctx.Queryable().Where(a => a.F_NO == TokenNo).First();
if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
{
throw SysExCore.ThrowFailException("无法找到登录用户信息。");
}
if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
{
throw SysExCore.ThrowFailException("无法找到登录用户信息。");
}
LoginUser.UserNo = token.F_USERNO;
LoginUser.UserName = token.F_USERNAME;
LoginUser.UserType = GetUserType(token.F_USERNO);
LoginUser.IPAddress = token.F_IPADDRESS;
LoginUser.WAreaNoList = token.F_WAREANOLIST.ToObject>();
LoginUser.WarehouseNo = token.F_WAREHOUSENO;
LoginUser.EncryptTokenNo = EncryptTokenNo;
LoginUser.AppType = (EAppType)token.F_APPTYPENUM;
LoginUser.LoginTime = token.F_BTIME;
LoginUser.AppDeviceNo = token.F_APPDEVICENO;
};
SysDbCore.DbConnExec(action);
return LoginUser;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取用户登录数据
///
public static LoginUserInfo GetLoginUser()
{
try
{
LoginUserInfo LoginUser = GetLoginUser(SessionCookieCore.GetLoginTokenNo());
void action(SqlSugarClient ctx)
{
if (string.IsNullOrWhiteSpace(LoginUser.EncryptTokenNo))
{
throw SysExCore.ThrowFailException("登录凭证为空。");
}
string TokenNo = SysSecurityCore.Aes256Decrypt(LoginUser.EncryptTokenNo);
ACL_USERTOKEN token = ctx.Queryable().Where(a => a.F_NO == TokenNo).First();
if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
{
throw SysExCore.ThrowFailException("无法找到登录用户信息。");
}
if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
{
throw SysExCore.ThrowFailException("无法找到登录用户信息。");
}
LoginUser.UserNo = token.F_USERNO;
LoginUser.UserName = token.F_USERNAME;
LoginUser.UserType = GetUserType(token.F_USERNO);
LoginUser.IPAddress = token.F_IPADDRESS;
LoginUser.WAreaNoList = token.F_WAREANOLIST.ToObject>();
LoginUser.WarehouseNo = token.F_WAREHOUSENO;
LoginUser.EncryptTokenNo = LoginUser.EncryptTokenNo;
LoginUser.AppType = (EAppType)token.F_APPTYPENUM;
LoginUser.LoginTime = token.F_BTIME;
LoginUser.AppDeviceNo = token.F_APPDEVICENO;
};
SysDbCore.DbConnExec(action);
return LoginUser;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取用户登录数据
///
public static LoginUserInfo GetLoginUser(ReqInfo ReqData)
{
try
{
if (ReqData == null)
{
throw SysExCore.ThrowInEmpty();
}
return GetLoginUser(ReqData.EncryptTokenNo);
}
catch (Exception ex)
{
throw ex;
}
}
public static byte[] GetVerifyCode()
{
try
{
string code = "";
var vcode = VerifyCodeUtil.GetVerifyCode(out code);
SessionCookieCore.WriteSessionCookieValue(SessionCookieConst.LoginVerifyCodeKey, code);
return vcode;
}
catch (Exception ex)
{
throw ex;
}
}
}
}