LoginBLLCore.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using SqlSugar;
  5. using WMS.Util;
  6. using WMS.Info;
  7. namespace WMS.Core
  8. {
  9. public class LoginBLLCore
  10. {
  11. /// <summary>
  12. /// 检测登录用户有效性
  13. /// </summary>
  14. public static ACL_USERITEM CheckLoginUser(string UserNo, string UserPwd, EAppType AppType, string WarehouseNo, List<string> WAreaNoList, SqlSugarClient Ctx)
  15. {
  16. try
  17. {
  18. if (string.IsNullOrWhiteSpace(UserNo))
  19. throw SysExCore.ThrowFailException("登录用户名为空!!!");
  20. if (string.IsNullOrWhiteSpace(UserPwd))
  21. throw SysExCore.ThrowFailException("登录密码为空!!!");
  22. if (AppType == EAppType.TV || AppType == EAppType.Interface)
  23. throw SysExCore.ThrowFailException("登录APP类型不正确!!!");
  24. //离线仓库登录
  25. //if (AppType == EAppType.OffLineRF)
  26. //{
  27. // if (string.IsNullOrWhiteSpace(WarehouseNo))
  28. // throw SysExCore.ThrowFailException("仓库号为空!!!");
  29. // //判断仓库
  30. // var wh = Ctx.Queryable<BASE_WAREHOUSE>().Where(it => it.F_NO == WarehouseNo).First();
  31. // if (wh == null)
  32. // throw SysExCore.ThrowFailException("输入参数中仓库号在仓库列表中不存在!!!");
  33. // if (WAreaNoList == null || WAreaNoList.Count == 0)
  34. // throw SysExCore.ThrowFailException("仓库区域为空!!!");
  35. // if (wh.F_TYPENUM != (int)EWarehouseType.OfflineRFWarehouse)
  36. // throw SysExCore.ThrowFailException("输入参数中仓库号不是离线仓库类型!!!");
  37. // //判断区域与仓库有没有匹配
  38. // var walist = Ctx.Queryable<BASE_WAREA>().Where(it => it.F_WAREHOUSENO == WarehouseNo).ToList();
  39. // if (walist == null || walist.Count == 0)
  40. // throw SysExCore.ThrowFailException("输入参数中区域号在仓库区域列表中不存在!!!");
  41. // if (WAreaNoList.Any(it => !walist.Any(b => it == b.F_NO)))
  42. // throw SysExCore.ThrowFailException("输入参数仓库号与区域号不匹配!!!");
  43. //}
  44. //获取数据
  45. var user = Ctx.Queryable<ACL_USERITEM>().Where(it => it.F_NO.ToUpper() == UserNo.ToUpper()).First();
  46. if (user == null)
  47. throw SysExCore.ThrowFailException("无法找到指定用户");
  48. if (user.F_ISDELETE > 0)
  49. throw SysExCore.ThrowFailException("用户已删除!!!");
  50. if (user.F_ISSTOP > 0)
  51. throw SysExCore.ThrowFailException("用户已停用!!!");
  52. if (user.F_AUTOSTOPTIME < DateTime.Now)
  53. throw SysExCore.ThrowFailException("用户已过期!!!");
  54. if (user.F_PWDERRQTY >= SysSetCore.GetSysSet().UserPwdErrQty)
  55. throw SysExCore.ThrowFailException(string.Format("用户密码错误已超过{0}次,已被锁定。", SysSetCore.GetSysSet().UserPwdErrQty));
  56. //用户密码已过期
  57. if (DateTime.MaxValue!=user.F_EDITPWDTIME)
  58. {
  59. if (user.F_EDITPWDTIME.AddDays(SysSetCore.GetSysSet().UserPwdExpD) < DateTime.Now)
  60. throw SysExCore.ThrowPwdExpiration();
  61. }
  62. //用户密码错误
  63. if (user.F_PASSWORD != SysSecurityCore.Aes256Encrypt(UserPwd))
  64. {
  65. int qty = SysSetCore.GetSysSet().UserPwdErrQty - user.F_PWDERRQTY;
  66. if (user.F_PWDERRQTY > 0)
  67. throw new SysExCore(ESysExType.PwdError, string.Format("用户密码错误,您还有{0}次", qty > 0 ? qty : 0));
  68. else
  69. throw new SysExCore(ESysExType.PwdError, "用户密码错误。");
  70. }
  71. if (user.F_PASSWORD == SysSecurityCore.Aes256Encrypt(SysSetCore.GetSysSet().PwdInit))
  72. {
  73. throw new SysExCore(ESysExType.PwdInit, "请将初始化密码进行修改。");
  74. }
  75. return user;
  76. }
  77. catch (Exception ex)
  78. {
  79. throw ex;
  80. }
  81. }
  82. /// <summary>
  83. /// 是否是管理员
  84. /// </summary>
  85. public static bool CheckAdmin(string UserNo)
  86. {
  87. bool IsAdmin = false;
  88. EUserType UserType = GetUserType(UserNo);
  89. if (UserType == EUserType.SuperAdmin || UserType == EUserType.Super || UserType == EUserType.System)
  90. {
  91. IsAdmin = true;
  92. }
  93. return IsAdmin;
  94. }
  95. /// <summary>
  96. /// 获取用户类型
  97. /// </summary>
  98. public static EUserType GetUserType(string UserNo)
  99. {
  100. EUserType UserType = EUserType.User;
  101. try
  102. {
  103. if (UserNo.ToUpper() == EUserType.Super.ToString().ToUpper())
  104. {
  105. UserType = EUserType.Super;
  106. }
  107. else if (UserNo.ToUpper() == EUserType.System.ToString().ToUpper())
  108. {
  109. UserType = EUserType.System;
  110. }
  111. else if (UserNo.ToUpper() == EUserType.SuperAdmin.ToString().ToUpper())
  112. {
  113. UserType = EUserType.SuperAdmin;
  114. }
  115. else
  116. {
  117. UserType = EUserType.User;
  118. }
  119. //权限管理员
  120. }
  121. catch
  122. {
  123. }
  124. return UserType;
  125. }
  126. /// <summary>
  127. /// 更新用户密码错误次数
  128. /// </summary>
  129. public static void UpdatePwdErr(string UserNo)
  130. {
  131. var i = SysDbCore.GetDbCtx().Updateable<ACL_USERITEM>().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();
  132. if (i <= 0)
  133. throw SysExCore.ThrowFailException("更新用户密码登录错误次数失败!!!");
  134. }
  135. /// <summary>
  136. /// 检测登录状态
  137. /// </summary>
  138. /// <param name="EncryptTokenNo">加密TokenNo</param>
  139. public static void LoginTokenCheck(string EncryptTokenNo)
  140. {
  141. try
  142. {
  143. if (string.IsNullOrWhiteSpace(EncryptTokenNo))
  144. {
  145. throw SysExCore.ThrowToken();
  146. }
  147. SqlSugarClient Ctx = SysDbCore.GetDbCtx();
  148. string TokenNo = SysSecurityCore.Aes256Decrypt(EncryptTokenNo);
  149. ACL_USERTOKEN token = Ctx.Queryable<ACL_USERTOKEN>().Where(a => a.F_NO == TokenNo).First();
  150. if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
  151. {
  152. throw SysExCore.ThrowToken();
  153. }
  154. if (token.F_APPTYPENUM != (int)EAppType.OffLineRF)
  155. {
  156. if (token.F_ETIME != DateTime.MaxValue)
  157. {
  158. if (token.F_ETIME.AddMinutes(SysSetCore.GetSysSet().OnLoginTimeOutM) < DateTime.Now)
  159. throw SysExCore.ThrowLoginTimeout();
  160. }
  161. Ctx.Updateable<ACL_USERTOKEN>().SetColumns(it => it.F_ETIME == DateTime.Now).Where(a => a.F_NO == TokenNo).ExecuteCommand();
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. throw ex;
  167. }
  168. }
  169. // <summary>
  170. /// 检测登录状态
  171. /// </summary>
  172. public static void LoginTokenCheck()
  173. {
  174. LoginTokenCheck(SessionCookieCore.GetLoginTokenNo());
  175. }
  176. /// <summary>
  177. /// 检测登录状态
  178. /// </summary>
  179. /// <param name="EncryptTokenNo">加密TokenNo</param>
  180. public static void LoginTokenCheck(ReqInfo ReqData)
  181. {
  182. try
  183. {
  184. if (ReqData == null)
  185. {
  186. throw SysExCore.ThrowInEmpty();
  187. }
  188. LoginTokenCheck(ReqData.EncryptTokenNo);
  189. }
  190. catch (Exception ex)
  191. {
  192. throw ex;
  193. }
  194. }
  195. /// <summary>
  196. /// 获取用户登录数据
  197. /// </summary>
  198. public static LoginUserInfo GetLoginUser(string EncryptTokenNo)
  199. {
  200. try
  201. {
  202. LoginUserInfo LoginUser = new LoginUserInfo();
  203. void action(SqlSugarClient ctx)
  204. {
  205. if (string.IsNullOrWhiteSpace(EncryptTokenNo))
  206. {
  207. throw SysExCore.ThrowFailException("登录凭证为空。");
  208. }
  209. string TokenNo = SysSecurityCore.Aes256Decrypt(EncryptTokenNo);
  210. ACL_USERTOKEN token = ctx.Queryable<ACL_USERTOKEN>().Where(a => a.F_NO == TokenNo).First();
  211. if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
  212. {
  213. throw SysExCore.ThrowFailException("无法找到登录用户信息。");
  214. }
  215. if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
  216. {
  217. throw SysExCore.ThrowFailException("无法找到登录用户信息。");
  218. }
  219. LoginUser.UserNo = token.F_USERNO;
  220. LoginUser.UserName = token.F_USERNAME;
  221. LoginUser.UserType = GetUserType(token.F_USERNO);
  222. LoginUser.IPAddress = token.F_IPADDRESS;
  223. LoginUser.WAreaNoList = token.F_WAREANOLIST.ToObject<List<string>>();
  224. LoginUser.WarehouseNo = token.F_WAREHOUSENO;
  225. LoginUser.EncryptTokenNo = EncryptTokenNo;
  226. LoginUser.AppType = (EAppType)token.F_APPTYPENUM;
  227. LoginUser.LoginTime = token.F_BTIME;
  228. LoginUser.AppDeviceNo = token.F_APPDEVICENO;
  229. };
  230. SysDbCore.DbConnExec(action);
  231. return LoginUser;
  232. }
  233. catch (Exception ex)
  234. {
  235. throw ex;
  236. }
  237. }
  238. /// <summary>
  239. /// 获取用户登录数据
  240. /// </summary>
  241. public static LoginUserInfo GetLoginUser()
  242. {
  243. try
  244. {
  245. LoginUserInfo LoginUser = GetLoginUser(SessionCookieCore.GetLoginTokenNo());
  246. void action(SqlSugarClient ctx)
  247. {
  248. if (string.IsNullOrWhiteSpace(LoginUser.EncryptTokenNo))
  249. {
  250. throw SysExCore.ThrowFailException("登录凭证为空。");
  251. }
  252. string TokenNo = SysSecurityCore.Aes256Decrypt(LoginUser.EncryptTokenNo);
  253. ACL_USERTOKEN token = ctx.Queryable<ACL_USERTOKEN>().Where(a => a.F_NO == TokenNo).First();
  254. if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
  255. {
  256. throw SysExCore.ThrowFailException("无法找到登录用户信息。");
  257. }
  258. if (token == null || string.IsNullOrWhiteSpace(token.F_NO))
  259. {
  260. throw SysExCore.ThrowFailException("无法找到登录用户信息。");
  261. }
  262. LoginUser.UserNo = token.F_USERNO;
  263. LoginUser.UserName = token.F_USERNAME;
  264. LoginUser.UserType = GetUserType(token.F_USERNO);
  265. LoginUser.IPAddress = token.F_IPADDRESS;
  266. LoginUser.WAreaNoList = token.F_WAREANOLIST.ToObject<List<string>>();
  267. LoginUser.WarehouseNo = token.F_WAREHOUSENO;
  268. LoginUser.EncryptTokenNo = LoginUser.EncryptTokenNo;
  269. LoginUser.AppType = (EAppType)token.F_APPTYPENUM;
  270. LoginUser.LoginTime = token.F_BTIME;
  271. LoginUser.AppDeviceNo = token.F_APPDEVICENO;
  272. };
  273. SysDbCore.DbConnExec(action);
  274. return LoginUser;
  275. }
  276. catch (Exception ex)
  277. {
  278. throw ex;
  279. }
  280. }
  281. /// <summary>
  282. /// 获取用户登录数据
  283. /// </summary>
  284. public static LoginUserInfo GetLoginUser(ReqInfo ReqData)
  285. {
  286. try
  287. {
  288. if (ReqData == null)
  289. {
  290. throw SysExCore.ThrowInEmpty();
  291. }
  292. return GetLoginUser(ReqData.EncryptTokenNo);
  293. }
  294. catch (Exception ex)
  295. {
  296. throw ex;
  297. }
  298. }
  299. public static byte[] GetVerifyCode()
  300. {
  301. try
  302. {
  303. string code = "";
  304. var vcode = VerifyCodeUtil.GetVerifyCode(out code);
  305. SessionCookieCore.WriteSessionCookieValue(SessionCookieConst.LoginVerifyCodeKey, code);
  306. return vcode;
  307. }
  308. catch (Exception ex)
  309. {
  310. throw ex;
  311. }
  312. }
  313. }
  314. }