public BaseResponse <bool> AddUser(AddUserParameter parameter) { BaseResponse <bool> response = new BaseResponse <bool>(); try { #region 输入合法性验证 if (string.IsNullOrEmpty(parameter.UserToken)) { response.IsSuccessful = false; response.Reason = "账户ID不能为空"; return(response); } if (string.IsNullOrEmpty(parameter.PWD)) { response.IsSuccessful = false; response.Reason = "账户密码不能为空"; return(response); } if (parameter.PWD.Length < 6) { response.IsSuccessful = false; response.Reason = "密码长度至少是6位"; return(response); } var isExisted = hbUserReosiory.GetDatas <HBUser>(t => !t.IsDeleted && !string.IsNullOrEmpty(t.UserToken) && t.UserToken.Equals(parameter.UserToken), true).Any(); if (isExisted) { response.IsSuccessful = false; response.Reason = "账户已存在"; return(response); } #endregion HBUser user = new HBUser(); user.UserToken = parameter.UserToken; user.PWD = MD5Helper.MD5Encrypt64(Utilitys.DecodeBase64("UTF-8", parameter.PWD));//密码MD5加密 user.RoleID = parameter.RoleID; user.OrganizationID = parameter.OrganizationID; user.AddUserID = parameter.AddUserID; user.LastUpdateDate = DateTime.Now; user.LastUpdateUserID = parameter.AddUserID; user.UseStatus = true; OperationResult operationResult = hbUserReosiory.AddNew <HBUser>(user); if (operationResult.ResultType != EnumOperationResultType.Success) { throw new Exception("添加用户异常"); } return(response); } catch (Exception e) { response.IsSuccessful = false; return(response); } }
/// <summary> /// 登陆后返回Token 和角色ID /// </summary> /// <param name="parameter"></param> /// <returns></returns> public BaseResponse <LoginResult> Login(LoginParameter parameter) { BaseResponse <LoginResult> response = new BaseResponse <LoginResult>(); LoginResult result = new LoginResult(); try { //密码Base64解密 var encodePWD = MD5Helper.MD5Encrypt64(Utilitys.DecodeBase64("UTF-8", parameter.PWD)); var user = hbUserReosiory.GetDatas <HBUser>(t => t.UserToken.Equals(parameter.UserName) && t.PWD.Equals(encodePWD) && t.UseStatus && !t.IsDeleted, true).FirstOrDefault(); if (user != null) { result.RoleID = user.RoleID; result.UserID = user.UserID; result.UserToken = user.UserToken; result.OrganID = user.OrganizationID; var organ = organRepository.GetDatas <Organization>(t => !t.IsDeleted && t.OrganID == result.OrganID, true).FirstOrDefault(); if (organ != null) { result.OrganTypeID = organ.OrganTypeID; var organType = organTypeRepository.GetDatas <OrganType>(t => !t.IsDeleted && t.OrganTypeID == result.OrganTypeID, true).FirstOrDefault(); if (organType != null) { result.OrganCategoryID = organType.CategoryID; } } var payload = new Dictionary <string, object> { { "name", user.UserToken }, { "exp", 1000 * 60 * 240 }, //超时时间 4个小时 { "role", user.RoleID }, { "date", DateTime.Now.ToString() } //{"date",DateTime.Now } }; var privateKey = Utilitys.GetAppConfig("PrivateKey"); //var privateKey = AppConfigHelper.GetConfigValue("PrivateKey"); result.Token = JsonWebToken.Encode(payload, privateKey, JwtHashAlgorithm.HS512); response.Result = result; return(response); } else { response.IsSuccessful = false; response.Reason = "用户名或者密码错误"; return(response); } } catch (global::System.Exception e) { LogHelper.WriteLog(e); response.Reason = "用户名或者密码错误"; response.IsSuccessful = false; return(response); } }
public BaseResponse <bool> UpdatePWD(UpdatePWDParameter parameter) { BaseResponse <bool> response = new BaseResponse <bool>(); try { var curUser = hbUserReosiory.GetDatas <HBUser>(t => !t.IsDeleted && t.UserID == parameter.CurrentUserID, true).FirstOrDefault(); if (curUser == null) { response.IsSuccessful = false; response.Reason = "当前用户数据异常"; return(response); } if (string.IsNullOrEmpty(parameter.OriginPWD)) { response.IsSuccessful = false; response.Reason = "请输入原密码"; return(response); } var pwd = Utilitys.DecodeBase64("UTF-8", parameter.OriginPWD); if (curUser.PWD != MD5Helper.MD5Encrypt64(pwd)) { response.IsSuccessful = false; response.Reason = "原密码不正确"; return(response); } if (string.IsNullOrEmpty(parameter.NewPWD)) { response.IsSuccessful = false; response.Reason = "请输入新密码"; return(response); } var newPWD = Utilitys.DecodeBase64("UTF-8", parameter.NewPWD); if (string.IsNullOrEmpty(newPWD)) { response.IsSuccessful = false; response.Reason = "请输入新密码"; return(response); } if (newPWD.Length < 6) { response.IsSuccessful = false; response.Reason = "新密码位数至少是6位"; return(response); } curUser.PWD = MD5Helper.MD5Encrypt64(newPWD); var operRes = hbUserReosiory.Update <HBUser>(curUser); if (operRes.ResultType != EnumOperationResultType.Success) { throw new Exception("修改密码时,数据库操作异常"); } return(response); } catch (Exception e) { LogHelper.WriteLog(e); response.IsSuccessful = false; response.Reason = e.Message; return(response); } }