/// <summary> /// 添加帐户 /// </summary> /// <param name="entity"></param> /// <returns></returns> public Account AddEntity(Account entity) { try { using (var connection = ConnectionFactory.GetMasterSql()) { AccountSingleParam singleParam = new AccountSingleParam() { OpenId=entity.OpenId, LoginName=entity.LoginName }; string sql = @"Insert into B_Account values(@OpenId,@LoginName,@EncryptKey,@Password,@SafePassword,@SafeBinding,@Mobile,@MobileBinding,@Email,@EmailBinding,@DelFlag,@ReMark,@CreateDate,@SubmitDomainId);select @@identity;"; var id = connection.ExecuteScalar<int>(sql, new { OpenId = entity.OpenId, LoginName = entity.LoginName, EncryptKey = entity.EncryptKey, Password = entity.Password, SafePassword = entity.SafePassword, SafeBinding = entity.SafeBinding, Mobile = entity.Mobile, MobileBinding = entity.MobileBinding, Email = entity.Email, EmailBinding = entity.EmailBinding, DelFlag = entity.DelFlag, ReMark = entity.ReMark, CreateDate = entity.CreateDate, SubmitDomainId = entity.SubmitDomainId }); var account = connection.Query<Account>(@"Select * from B_Account where AccountId=@AccountId", new { AccountId = id }).FirstOrDefault(); return account; } } catch { return null; } }
/// <summary> /// 添加单点登录的帐号 /// </summary> /// <param name="model"></param> /// <returns></returns> public JsonModel<Account> AddAccount(AccountAddModel model) { JsonModel<Account> jsonModel = new JsonModel<Account>() { Success = false, ErrMsg = "添加失败", SuccessMsg = "添加成功" }; try { //对实体进行验证 var validate = DotNet.Utils.DataValidate.ValidateHelper<AccountAddModel>.ValidateModel(model); if (!validate.Pass) { jsonModel.ErrMsg = validate.ResultList.FirstOrDefault().ErrorMessage; return jsonModel; } //过滤 model.LoginName = DotNet.Utils.Untility.StringHelper.FilterHtml(model.LoginName); model.Mobile = DotNet.Utils.Untility.StringHelper.FilterHtml(model.Mobile); model.LoginName = DotNet.Utils.Untility.StringHelper.FilterHtml(model.LoginName); #region 验证 if (!BllUtility.AccountHandler.VerifyOnly(new AccountSingleParam() { LoginName = model.LoginName })) { jsonModel.ErrMsg = "用户名已经存在"; return jsonModel; }; //验证Mobile int mobileBinding = (int)BindingEnum.NotBinded; if (!string.IsNullOrEmpty(model.Mobile)) { if (!DotNet.Utils.Untility.RegexValidate.IsMobileNumber(model.Mobile)) { jsonModel.ErrMsg = "手机号码格式不正确"; return jsonModel; } mobileBinding=(int)BindingEnum.Binded; if (!BllUtility.AccountHandler.VerifyOnly(new AccountSingleParam() { Mobile = model.Mobile })) { jsonModel.ErrMsg = "手机号码已经存在"; return jsonModel; }; } //验证Email int emailBinding = (int)BindingEnum.NotBinded; if (!string.IsNullOrEmpty(model.Email)) { if (!DotNet.Utils.Untility.RegexValidate.IsEmailAddress(model.Email)) { jsonModel.ErrMsg = "Email格式不正确"; return jsonModel; } emailBinding=(int)BindingEnum.Binded; if (!BllUtility.AccountHandler.VerifyOnly(new AccountSingleParam() { Email = model.Email })) { jsonModel.ErrMsg = "邮箱已经存在"; return jsonModel; }; } //验证安全密码 int safeBinding = (int)BindingEnum.NotBinded; if (!string.IsNullOrEmpty(model.SafePassword)) { if (!DotNet.Utils.Untility.RegexValidate.IsPasswordOne(model.SafePassword, 6, 25)) { jsonModel.ErrMsg = "安全密码格式不正确"; return jsonModel; } model.SafePassword = BllUtility.AccountHandler.EncryptSafePassword(model.SafePassword); safeBinding = (int)BindingEnum.Binded; } //验证提交的域是否存在 IDomainDal domainDal = new DomainDal(); var domain = domainDal.GetEntity(new DomainSingleParam() { DomainCode=model.SubmitDomainCode }); if (domain == null || domain.DomainId <= 0) { jsonModel.ErrMsg = "域不存在"; return jsonModel; } #endregion string openId = BllUtility.AccountHandler.CreateOpenId(); string encryptKey = BllUtility.AccountHandler.CreateEncryptKey(); string encryptPassword = BllUtility.AccountHandler.EncryptPassword(openId, model.Password, encryptKey); string mobile = string.IsNullOrEmpty(model.Mobile) ? "" : model.Mobile; string email = string.IsNullOrEmpty(model.Email) ? "" : model.Email; string safePassword = string.IsNullOrEmpty(model.SafePassword) ? "" : model.SafePassword; Account account = new Account() { OpenId = openId, LoginName = model.LoginName, EncryptKey = encryptKey, Password = encryptPassword, Mobile = mobile, MobileBinding = mobileBinding, Email = email, EmailBinding = emailBinding, SafePassword = safePassword, SafeBinding = safeBinding, CreateDate = DateTime.Now, DelFlag = (int)DelFlagEnum.Noraml, ReMark = model.ReMark, SubmitDomainId = domain.DomainId }; IAccountDal accountDal = new AccountDal(); var r = accountDal.AddEntity(account); if (r != null && r.AccountId > 0) { jsonModel.Success = true; jsonModel.Data = r; } else { jsonModel.ErrMsg = "数据插入失败"; } } catch { jsonModel.ErrMsg = "系统内部错误"; } return jsonModel; }
/// <summary> /// 修改帐户 /// </summary> /// <param name="entity"></param> /// <returns></returns> public Account UpdateEntity(Account entity) { try { using (var connection = ConnectionFactory.GetMasterSql()) { var dbAccount = connection.Query<Account>(@"Select * from B_Account where AccountId=@AccountId", new { AccountId = @entity.AccountId }).FirstOrDefault(); if (dbAccount == null) { return null; } string sql = @"Update B_Account set EncryptKey=@EncryptKey, [Password]=@Password,SafePassword=@SafePassword,SafeBinding=@SafeBinding,Mobile=@Mobile,MobileBinding=@MobileBinding,Email=@Email,EmailBinding=@EmailBinding,DelFlag=@DelFlag,ReMark=@ReMark where AccountId=@AccountId"; var r = connection.Execute(sql, new { EncryptKey = entity.EncryptKey, Password = entity.Password,SafePassword=entity.SafePassword,SafeBinding=entity.SafeBinding,Mobile=entity.Mobile,MobileBinding=entity.MobileBinding,Email=entity.Email,EmailBinding=entity.EmailBinding, DelFlag = entity.DelFlag, ReMark = entity.ReMark, AccountId = entity.AccountId }); if (r > 0) { var model = connection.Query<Account>(@"Select * from B_Account where AccountId=@AccountId", new { AccountId = entity.AccountId }).FirstOrDefault(); return model; } else { return null; } } } catch { return null; } }