LoginBLLCore.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using Mapster;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WMS.BZModels.Dto.UserCenterManager.UserInfoDtos;
  9. using WMS.BZModels.Models.UserCenterManager;
  10. using WMS.Info;
  11. using WMS.BZSqlSugar;
  12. using WMS.Util;
  13. namespace WMS.BZServices
  14. {
  15. public class BZLoginBLLCore
  16. {
  17. private static Repository<AclUserInfo> _acluserInfoRepository => new Repository<AclUserInfo>();
  18. private static Repository<AclUsertoken> _aclUsertokenRepository => new Repository<AclUsertoken>();
  19. /// <summary>
  20. /// 检测登录用户有效性
  21. /// </summary>
  22. public static AclUserInfo CheckLoginUser(string UserNo, string UserPwd, EAppType AppType, string WarehouseNo, List<string> WAreaNoList)
  23. {
  24. try
  25. {
  26. if (string.IsNullOrWhiteSpace(UserNo))
  27. throw BZSysExCore.ThrowFailException("登录用户名为空!!!");
  28. if (string.IsNullOrWhiteSpace(UserPwd))
  29. throw BZSysExCore.ThrowFailException("登录密码为空!!!");
  30. if (AppType == EAppType.TV || AppType == EAppType.Interface)
  31. throw BZSysExCore.ThrowFailException("登录APP类型不正确!!!");
  32. //离线仓库登录
  33. //if (AppType == EAppType.OffLineRF)
  34. //{
  35. // if (string.IsNullOrWhiteSpace(WarehouseNo))
  36. // throw BZSysExCore.ThrowFailException("仓库号为空!!!");
  37. // //判断仓库
  38. // var wh = Ctx.Queryable<BASE_WAREHOUSE>().Where(it => it.F_NO == WarehouseNo).First();
  39. // if (wh == null)
  40. // throw BZSysExCore.ThrowFailException("输入参数中仓库号在仓库列表中不存在!!!");
  41. // if (WAreaNoList == null || WAreaNoList.Count == 0)
  42. // throw BZSysExCore.ThrowFailException("仓库区域为空!!!");
  43. // if (wh.F_TYPENUM != (int)EWarehouseType.OfflineRFWarehouse)
  44. // throw BZSysExCore.ThrowFailException("输入参数中仓库号不是离线仓库类型!!!");
  45. // //判断区域与仓库有没有匹配
  46. // var walist = Ctx.Queryable<BASE_WAREA>().Where(it => it.F_WAREHOUSENO == WarehouseNo).ToList();
  47. // if (walist == null || walist.Count == 0)
  48. // throw BZSysExCore.ThrowFailException("输入参数中区域号在仓库区域列表中不存在!!!");
  49. // if (WAreaNoList.Any(it => !walist.Any(b => it == b.F_NO)))
  50. // throw BZSysExCore.ThrowFailException("输入参数仓库号与区域号不匹配!!!");
  51. //}
  52. //获取数据
  53. var user = _acluserInfoRepository.Queryable().First(it => it.Code.ToUpper() == UserNo.ToUpper());
  54. //var user = Ctx.Queryable<ACL_USERITEM>().Where(it => it.F_NO.ToUpper() == UserNo.ToUpper()).First();
  55. if (user == null)
  56. throw BZSysExCore.ThrowFailException("无法找到指定用户");
  57. if (user.IsStop > 0)
  58. throw BZSysExCore.ThrowFailException("用户已停用!!!");
  59. //if (user.PwdErrQty >= SysSetCore.GetSysSet().UserPwdErrQty)
  60. // throw BZSysExCore.ThrowFailException(string.Format("用户密码错误已超过{0}次,已被锁定。", SysSetCore.GetSysSet().UserPwdErrQty));
  61. ////用户密码已过期
  62. //if (DateTime.MaxValue != user.EditTime)
  63. //{
  64. // if (user.EditTime.AddDays(SysSetCore.GetSysSet().UserPwdExpD) < DateTime.Now)
  65. // throw BZSysExCore.ThrowPwdExpiration();
  66. //}
  67. //用户密码错误
  68. if (user.Pwd != SysSecurityHelp.Aes256Encrypt(UserPwd, user.Secretkey))
  69. {
  70. int qty = 5 - user.PwdErrQty;
  71. if (user.PwdErrQty > 0)
  72. throw new BZSysExCore(ESysExType.PwdError, string.Format("用户密码错误,您还有{0}次", qty > 0 ? qty : 0));
  73. else
  74. throw new BZSysExCore(ESysExType.PwdError, "用户密码错误。");
  75. }
  76. if (user.Pwd == SysSecurityHelp.Aes256Encrypt("000000", user.Secretkey))
  77. {
  78. throw new BZSysExCore(ESysExType.PwdInit, "请将初始化密码进行修改。");
  79. }
  80. return user;
  81. }
  82. catch (Exception ex)
  83. {
  84. throw ex;
  85. }
  86. }
  87. /// <summary>
  88. /// 是否是管理员
  89. /// </summary>
  90. public static bool CheckAdmin(string UserNo)
  91. {
  92. bool IsAdmin = false;
  93. EUserType UserType = GetUserType(UserNo);
  94. if (UserType == EUserType.SuperAdmin || UserType == EUserType.Super || UserType == EUserType.System)
  95. {
  96. IsAdmin = true;
  97. }
  98. return IsAdmin;
  99. }
  100. /// <summary>
  101. /// 获取用户类型
  102. /// </summary>
  103. public static EUserType GetUserType(string UserNo)
  104. {
  105. EUserType UserType = EUserType.User;
  106. try
  107. {
  108. if (UserNo.ToUpper() == EUserType.Super.ToString().ToUpper())
  109. {
  110. UserType = EUserType.Super;
  111. }
  112. else if (UserNo.ToUpper() == EUserType.System.ToString().ToUpper())
  113. {
  114. UserType = EUserType.System;
  115. }
  116. else if (UserNo.ToUpper() == EUserType.SuperAdmin.ToString().ToUpper())
  117. {
  118. UserType = EUserType.SuperAdmin;
  119. }
  120. else
  121. {
  122. UserType = EUserType.User;
  123. }
  124. //权限管理员
  125. }
  126. catch
  127. {
  128. }
  129. return UserType;
  130. }
  131. /// <summary>
  132. /// 更新用户密码错误次数
  133. /// </summary>
  134. public static void UpdatePwdErr(string UserNo)
  135. {
  136. var result = _acluserInfoRepository.UpdateModelColumns(p => new AclUserInfo
  137. {
  138. EditWho = UserNo,
  139. EditTime = DateTime.Now,
  140. PwdErrQty = p.PwdErrQty + 1
  141. }, it => it.Code.ToUpper() == UserNo.ToUpper());
  142. //if (result)
  143. // throw BZSysExCore.ThrowFailException("更新用户密码登录错误次数失败!!!");
  144. }
  145. /// <summary>
  146. /// 获取用户登录数据
  147. /// </summary>
  148. public static LoginUserInfo GetLoginUser(string EncryptTokenNo)
  149. {
  150. try
  151. {
  152. LoginUserInfo LoginUser = new LoginUserInfo();
  153. if (ConfigHelper.GetConfig().IsSSO)
  154. {
  155. if (string.IsNullOrWhiteSpace(EncryptTokenNo))
  156. {
  157. throw BZSysExCore.ThrowFailException("登录凭证为空。");
  158. }
  159. string TokenNo = SysSecurityHelp.Aes256Decrypt(EncryptTokenNo);
  160. var token = _aclUsertokenRepository.Queryable().Where(a => a.Code == TokenNo).First();
  161. // ACL_USERTOKEN token = ctx.Queryable<ACL_USERTOKEN>().Where(a => a.F_NO == TokenNo).First();
  162. if (token == null || string.IsNullOrWhiteSpace(token.Code))
  163. {
  164. throw BZSysExCore.ThrowFailException("无法找到登录用户信息。");
  165. }
  166. LoginUser.UserNo = token.UserNo;
  167. LoginUser.UserName = token.UserName;
  168. LoginUser.UserType = GetUserType(token.UserNo);
  169. LoginUser.IPAddress = token.IPAddress;
  170. LoginUser.WAreaNoList = token.WareaNoList.ToObject<List<string>>();
  171. LoginUser.WarehouseNo = token.WarehouseNo;
  172. LoginUser.EncryptTokenNo = EncryptTokenNo;
  173. LoginUser.AppType = (EAppType)token.AppTypeNum;
  174. LoginUser.LoginTime = token.BTime;
  175. LoginUser.AppDeviceNo = token.AppDeviceNo;
  176. }
  177. else
  178. {
  179. var user = _acluserInfoRepository.Queryable().First(it => it.Code.ToUpper() == EncryptTokenNo.ToUpper());
  180. LoginUser.UserNo = user.Code;
  181. LoginUser.UserName = user.Name;
  182. LoginUser.UserType = GetUserType(user.Code);
  183. //LoginUser.IPAddress = user.IPAddress;
  184. //LoginUser.WAreaNoList = token.WareaNoList.ToObject<List<string>>();
  185. LoginUser.WarehouseNo = user.WarehouseNo;
  186. //LoginUser.EncryptTokenNo = EncryptTokenNo;
  187. //LoginUser.AppType = (EAppType)user.AppTypeNum;
  188. LoginUser.LoginTime = user.EditTime;
  189. //LoginUser.AppDeviceNo = token.AppDeviceNo;
  190. }
  191. return LoginUser;
  192. }
  193. catch (Exception ex)
  194. {
  195. throw ex;
  196. }
  197. }
  198. /// <summary>
  199. /// 获取用户登录数据
  200. /// </summary>
  201. public static LoginUserInfo GetLoginUser()
  202. {
  203. try
  204. {
  205. LoginUserInfo LoginUser = GetLoginUser(BZSessionCookieCore.GetLoginTokenNo());
  206. //if (string.IsNullOrWhiteSpace(LoginUser.EncryptTokenNo))
  207. //{
  208. // throw BZSysExCore.ThrowFailException("登录凭证为空。");
  209. //}
  210. //string TokenNo = SysSecurityHelp.Aes256Decrypt(LoginUser.EncryptTokenNo);
  211. //var token = _aclUsertokenRepository.Queryable().Where(a => a.Code == TokenNo).First();
  212. //if (token == null || string.IsNullOrWhiteSpace(token.Code))
  213. //{
  214. // throw BZSysExCore.ThrowFailException("无法找到登录用户信息。");
  215. //}
  216. //LoginUser.UserNo = token.UserNo;
  217. //LoginUser.UserName = token.UserName;
  218. //LoginUser.UserType = GetUserType(token.UserNo);
  219. //LoginUser.IPAddress = token.IPAddress;
  220. //LoginUser.WAreaNoList = token.WareaNoList.ToObject<List<string>>();
  221. //LoginUser.WarehouseNo = token.WarehouseNo;
  222. //LoginUser.EncryptTokenNo = LoginUser.EncryptTokenNo;
  223. //LoginUser.AppType = (EAppType)token.AppTypeNum;
  224. //LoginUser.LoginTime = token.BTime;
  225. //LoginUser.AppDeviceNo = token.AppDeviceNo;
  226. return LoginUser;
  227. }
  228. catch (Exception ex)
  229. {
  230. throw ex;
  231. }
  232. }
  233. /// <summary>
  234. /// 获取用户登录数据
  235. /// </summary>
  236. public static LoginUserInfo GetLoginUser(ReqInfo ReqData)
  237. {
  238. try
  239. {
  240. if (ReqData == null)
  241. {
  242. throw BZSysExCore.ThrowInEmpty();
  243. }
  244. return GetLoginUser(ReqData.EncryptTokenNo);
  245. }
  246. catch (Exception ex)
  247. {
  248. throw ex;
  249. }
  250. }
  251. public static byte[] GetVerifyCode()
  252. {
  253. try
  254. {
  255. string code = "";
  256. var vcode = VerifyCodeUtil.GetVerifyCode(out code);
  257. BZSessionCookieCore.WriteSessionCookieValue(BZSessionCookieConst.LoginVerifyCodeKey, code);
  258. return vcode;
  259. }
  260. catch (Exception ex)
  261. {
  262. throw ex;
  263. }
  264. }
  265. }
  266. }