コード例 #1
0
        public Return Sms(User userModel)
        {
            var resultReturn = new Return();

            try
            {
                var vCode       = CommonHelper.CreateCode();
                var sms         = new Sms(vCode);
                var smsResponse = sms.SendSmsToPhone(userModel.Phone);

                if (smsResponse?.Code != null && smsResponse.Code == "OK")
                {
                    var smsModel = new Data.Domain.Sms
                    {
                        Code       = vCode,
                        Phone      = userModel.Phone,
                        UpdateTime = DateTime.Now
                    };

                    var flag = SmsService.InsertOrUpdate(smsModel);

                    if (!flag)
                    {
                        resultReturn.ReturnCode.Code = -1;
                    }
                }
                else
                {
                    if (smsResponse != null)
                    {
                        //短信验证码 :使用同一个签名,对同一个手机号码发送短信验证码,支持1条/分钟,5条/小时 ,累计10条/天。
                        if (smsResponse.Message.IndexOf("触发天级流控", StringComparison.Ordinal) != -1)
                        {
                            resultReturn.ReturnCode.Code = 1887;
                        }
                        else
                        {
                            resultReturn.ReturnCode.Code = 1992;
                        }

                        BpService.Use(smsResponse.Message);
                    }

                    BpService.Use("发送短信验证码返回的response为空");
                }

                return(resultReturn);
            }
            catch (Exception ex)
            {
                LogService.InsertLog(ex);
                resultReturn.SetWrong();
                return(resultReturn);
            }
        }
コード例 #2
0
        public Return Register(User userModel)
        {
            var resultReturn = new Return();

            try
            {
                #region 检查必须字段是否为空

                //安卓ID为空
                if (string.IsNullOrEmpty(userModel.Android))
                {
                    resultReturn.ReturnCode.Code = 1885;
                    return(resultReturn);
                }

                //密码为空
                if (string.IsNullOrEmpty(userModel.Password))
                {
                    resultReturn.ReturnCode.Code = 1997;
                    return(resultReturn);
                }

                //验证码为空
                if (string.IsNullOrEmpty(userModel.VCode))
                {
                    resultReturn.ReturnCode.Code = 1994;
                    return(resultReturn);
                }

                #endregion

                #region 验证该安卓ID是否被记录

                var androidInfo = AndroidService.GetByAndroidId(userModel.Android);

                //该安卓ID不存在Android表中
                if (androidInfo == null)
                {
                    resultReturn.ReturnCode.Code = 1884;
                    return(resultReturn);
                }

                #endregion

                #region 短信验证

                var sms = SmsService.GetSmsByPhone(userModel.Phone);

                //还没发送短信
                if (sms == null)
                {
                    resultReturn.ReturnCode.Code = 1991;
                    return(resultReturn);
                }

                //验证码已过期
                if (Convert.ToDateTime(sms.UpdateTime).AddMinutes(5) < DateTime.Now)
                {
                    resultReturn.ReturnCode.Code = 1990;
                    return(resultReturn);
                }

                //已使用
                if (sms.IsUse)
                {
                    resultReturn.ReturnCode.Code = 1888;
                    return(resultReturn);
                }

                //验证码不正确
                if (sms.Code != userModel.VCode)
                {
                    resultReturn.ReturnCode.Code = 1889;
                    return(resultReturn);
                }

                //避免不同数据库上下文操作出现的错误
                var newSms = new Data.Domain.Sms
                {
                    Code       = sms.Code,
                    IsUse      = true,
                    Phone      = sms.Phone,
                    UpdateTime = DateTime.Now
                };

                //更新Sms IsUse 为 true  //若出错,不抛出错误,只做错误记录
                SmsService.InsertOrUpdate(newSms);

                #endregion

                #region 验证手机号码是否已注册

                var userInfo = UserService.GetUserByPhone(userModel.Phone);

                //该手机号码已注册
                if (userInfo != null)
                {
                    resultReturn.ReturnCode.Code = 1998;
                    return(resultReturn);
                }

                #endregion

                resultReturn = UserService.Register(androidInfo.UserId.ToString(), userModel.Phone, userModel.Password);

                //将User数据库模型类转换为页面模型类
                var user = ModelTransfer.Mapper(resultReturn.Content, new User());
                user.Password = "";

                resultReturn.Content = user;
                return(resultReturn);
            }
            catch (Exception ex)
            {
                LogService.InsertLog(ex);
                resultReturn.SetWrong();
                return(resultReturn);
            }
        }