public override SetUserPasswordResponseBody ExecuteCore() { SetUserPasswordResponseBody res = new SetUserPasswordResponseBody(); using (HWLEntities db = new HWLEntities()) { IQueryable <t_user> query = db.t_user; IQueryable <t_user_code> codeQuery = db.t_user_code.OrderByDescending(c => c.id).Where(c => c.code_type == CodeType.Register); if (!string.IsNullOrEmpty(this.request.Mobile)) { query = query.Where(u => u.mobile == this.request.Mobile); codeQuery = codeQuery.Where(u => u.user_account == this.request.Mobile); } else { query = query.Where(u => u.email == this.request.Email); codeQuery = codeQuery.Where(u => u.user_account == this.request.Email); } t_user user = query.FirstOrDefault(); if (user == null) { throw new Exception("未注册的帐号不能找回密码!"); } t_user_code userCode = codeQuery.FirstOrDefault(); if (userCode == null) { throw new Exception("注册码不存在"); } if (userCode.expire_time <= DateTime.Now) { throw new Exception("注册码已过期"); } if (userCode.code != this.request.CheckCode) { throw new Exception("注册码验证错误"); } //添加用户成功后,设置注册码失效 userCode.expire_time = userCode.expire_time.AddDays(-1); //更新用户密码信息 user.password = this.request.PasswordOK; db.SaveChanges(); try { //清除用户之前登录用过的TOKEN new Redis.UserAction().RemoveUserToken(user.id); } catch (Exception) { } res.Status = ResultStatus.Success; } return(res); }
///// <summary> ///// 加密用户token模型信息,作为请求的唯一凭证 ///// </summary> ///// <returns></returns> //public static string BuildToken(UserTokenInfo tokenInfo) //{ // if (tokenInfo == null || tokenInfo.UserId <= 0) throw new Exception("用户凭证生成错误"); // if (tokenInfo.Expire == null || tokenInfo.Expire <= DateTime.Now) throw new Exception("用户凭证过期设置错误"); // return JsonConvert.SerializeObject(tokenInfo); //} ///// <summary> ///// 用户访问凭证解析处理 ///// </summary> ///// <param name="tokenInfoJsonStr"></param> ///// <returns></returns> //public static UserTokenInfo ParserToken(string tokenInfoJsonStr) //{ // if (string.IsNullOrEmpty(tokenInfoJsonStr)) throw new Exception("用户访问凭证是空的"); // try // { // return JsonConvert.DeserializeObject<UserTokenInfo>(tokenInfoJsonStr); // } // catch // { // throw new Exception("用户访问凭证解析错误"); // } //} /// <summary> /// 向数据库中添加验证码 /// </summary> public static int AddCode(HWLEntities db, CodeType codeType, string code, string remark = "", string userAccount = "", int userId = 0) { DateTime currTime = DateTime.Now; t_user_code model = new t_user_code() { //id = 0, code = code, code_type = codeType, create_date = currTime, expire_time = currTime.AddSeconds(AppConfigManager.UserCodeExpireSecond), remark = remark, user_id = userId, user_account = userAccount, }; db.t_user_code.Add(model); db.SaveChanges(); return(model.id); }
public override UserLoginAndRegisterResponseBody ExecuteCore() { UserLoginAndRegisterResponseBody res = new UserLoginAndRegisterResponseBody(); IQueryable <t_user> query = db.t_user; IQueryable <t_user_code> codeQuery = db.t_user_code; if (this.isMobile) { query = query.Where(u => u.mobile == this.request.Mobile); codeQuery = db.t_user_code.Where(u => u.user_account == this.request.Mobile); } else { query = query.Where(u => u.email == this.request.Email); codeQuery = db.t_user_code.Where(u => u.user_account == this.request.Email); } t_user_code oldCode = null; if (this.request.CheckCode != AppConfigManager.CheckCodeForDebug) { oldCode = codeQuery.OrderByDescending(u => u.id).FirstOrDefault(); if (oldCode == null || oldCode.code != this.request.CheckCode) { throw new Exception("验证码错误"); } if (oldCode.expire_time <= DateTime.Now) { throw new Exception("验证码已过期"); } } t_user user = query.FirstOrDefault(); if (user == null) { user = this.CreateUser(); } if (user.status != UserStatus.Normal) { throw new Exception("用户已经被禁用"); } string userToken = UserUtility.BuildToken(user.id); bool succ = UserStore.SaveUserToken(user.id, userToken); if (!succ) { throw new Exception("用户登录token生成失败"); } UserRegisterAreaInfo pos = null; if (user.register_country > 0 || user.register_province > 0 || user.register_city > 0 || user.register_district > 0) { pos = (from country in db.t_country join province in db.t_province on country.id equals province.country_id join city in db.t_city on province.id equals city.province_id join dist in db.t_district on city.id equals dist.city_id where country.id == user.register_country && province.id == user.register_province && city.id == user.register_city && dist.id == user.register_district select new UserRegisterAreaInfo { CountryId = country.id, Country = country.name, ProvinceId = province.id, Province = province.name, CityId = city.id, City = city.name, DistrictId = dist.id, District = dist.name, }).FirstOrDefault(); } res.UserInfo = new UserBaseInfo() { Id = user.id, Symbol = user.symbol, Email = user.email, Mobile = user.mobile, Name = user.name, Token = userToken, HeadImage = user.head_image, CircleBackImage = user.circle_back_image, UserSex = user.sex, LifeNotes = user.life_notes, RegAreaInfo = pos, FriendCount = db.t_user_friend.Where(f => f.user_id == user.id).Count(), GroupCount = db.t_group_user.Where(f => f.user_id == user.id).Count() }; //设置注册码过期 if (oldCode != null) { oldCode.expire_time = oldCode.expire_time.AddDays(-1); db.SaveChanges(); } return(res); }
public override UserRegisterResponseBody ExecuteCore() { UserRegisterResponseBody res = new UserRegisterResponseBody(); using (HWLEntities db = new HWLEntities()) { IQueryable <t_user> query = db.t_user; IQueryable <t_user_code> codeQuery = db.t_user_code.OrderByDescending(c => c.id).Where(c => c.code_type == CodeType.Register); if (!string.IsNullOrEmpty(this.request.Mobile)) { query = query.Where(u => u.mobile == this.request.Mobile); codeQuery = codeQuery.Where(u => u.user_account == this.request.Mobile); } else { query = query.Where(u => u.email == this.request.Email); codeQuery = codeQuery.Where(u => u.user_account == this.request.Email); } t_user user = query.FirstOrDefault(); if (user != null) { throw new Exception("该帐号已经被注册"); } if (this.request.CheckCode != "888888") { t_user_code userCode = codeQuery.FirstOrDefault(); if (userCode == null) { throw new Exception("注册码不存在"); } if (userCode.expire_time <= DateTime.Now) { throw new Exception("注册码已过期"); } if (userCode.code != this.request.CheckCode) { throw new Exception("注册码验证错误"); } //添加用户成功后,设置注册码失效 userCode.expire_time = userCode.expire_time.AddDays(-1); } //添加用户到数据库 t_user model = new t_user() { id = 0, email = this.request.Email ?? " ", mobile = this.request.Mobile ?? " ", password = this.request.PasswordOK, status = UserStatus.Normal, sex = UserSex.Unknow, register_date = DateTime.Now, update_date = DateTime.Now, name = "HWL-" + RandomText.GetNum(), head_image = ConfigManager.UserDefaultHeadImage, circle_back_image = ConfigManager.UserCircleBackImage, }; db.t_user.Add(model); db.SaveChanges(); res.Status = ResultStatus.Success; } return(res); }