public JsonResult PhoneLogin([FromForm] string phone, [FromForm] string code) { DataResult dr = new DataResult(); try { DataResult dataResult = ExaminePhoneAndCode(phone, code); if (dataResult.code != "200") { return(Json(dataResult)); } UserEntity userEntity = userBLL.GetByPhone(phone); if (userEntity == null) { int rows = userBLL.CreateToPhone(phone); } PhoneCodeBLL phoneCodeBLL = new PhoneCodeBLL(); phoneCodeBLL.Delete(phone); UserEntity user = userBLL.GetByPhone(phone); UserTokenBLL userTokenBLL = new UserTokenBLL(); UserTokenEntity userTokenEntity = userTokenBLL.GetByUserId(user.userId); UserTokenEntity userToken = new UserTokenEntity(); if (userTokenEntity == null) { userToken = userTokenBLL.Create(user.userId); } else { userToken = userTokenBLL.Update(userTokenEntity); } LoginResult loginResult = new LoginResult(); loginResult.token = userToken.token; loginResult.userEntity = user; dr.code = "200"; dr.data = loginResult; } catch (Exception ex) { dr.code = "999"; dr.msg = ex.Message; } return(Json(dr)); }
/// <summary> /// 检查手机号码 /// </summary> /// <param name="phone"></param> /// <param name="code"></param> /// <returns></returns> private DataResult ExaminePhoneAndCode(string phone, string code) { DataResult dr = new DataResult(); try { if (string.IsNullOrWhiteSpace(phone) || phone.Length != 11)// || Regex.IsMatch(phone, Helper.RegexHelper.PATTERN_PHONE) { dr.code = "201"; dr.msg = "手机号码错误"; return(dr); } if (string.IsNullOrWhiteSpace(code) || code.Length != 6) { dr.code = "201"; dr.msg = "验证码错误"; return(dr); } PhoneCodeBLL phoneCodeBLL = new PhoneCodeBLL(); PhoneCodeEntity phoneCodeEntity = phoneCodeBLL.GetByPhoneAndCode(phone, code); if (phoneCodeEntity == null) { dr.code = "201"; dr.msg = "验证码错误"; return(dr); } TimeSpan ts = DateTime.Now.Subtract(phoneCodeEntity.createDate).Duration(); double dateDiff = ts.TotalMinutes; if (dateDiff > 15) { phoneCodeBLL.Delete(phone); dr.code = "201"; dr.msg = "验证码过期"; return(dr); } dr.code = "200"; } catch (Exception ex) { dr.code = "999"; dr.msg = ex.Message; } return(dr); }
public JsonResult SendCode([FromForm] string phone) { DataResult dr = new DataResult(); try { if (string.IsNullOrWhiteSpace(phone) || phone.Length != 11) //|| !Regex.IsMatch(phone, Helper.RegexHelper.PATTERN_PHONE) { dr.code = "201"; dr.msg = "手机号码错误"; return(Json(dr)); } Random rd = new Random(); string code = rd.Next(100000, 999999).ToString(); bool rs = Aliyun.MessageHeiper.SendCode(phone, code); if (!rs) { dr.code = "201"; dr.msg = "验证码发送失败"; return(Json(dr)); } PhoneCodeBLL phoneCodeBLL = new PhoneCodeBLL(); int rows = phoneCodeBLL.Delete(phone); if (phoneCodeBLL.Create(phone, code) > 0) { dr.code = "200"; dr.msg = "验证码发送成功"; } else { dr.code = "201"; dr.msg = "验证码发送成功,但保存失败"; } } catch (Exception ex) { dr.code = "999"; dr.msg = ex.Message; } return(Json(dr)); }