public HttpResponseMessage SendSms([FromBody] SmsRequest smsRequest) { logInfo.Info(string.Format("短信接口请求串:{0}", smsRequest.ToJson())); var viewModel = new SmsResultViewModel(); if (!ModelState.IsValid) { viewModel.BusinessStatus = -10000; string msg = ModelState.Values.Where(item => item.Errors.Count == 1).Aggregate(string.Empty, (current, item) => current + (item.Errors[0].ErrorMessage + "; ")); viewModel.StatusMessage = "输入参数错误," + msg; return(viewModel.ResponseToJson()); } var response = _smsService.SendSms(smsRequest, Request.GetQueryNameValuePairs()); logInfo.Info(string.Format("短信接口返回值:{0}", response.ToJson())); if (response.Status == HttpStatusCode.BadRequest || response.Status == HttpStatusCode.Forbidden) { viewModel.BusinessStatus = -10001; viewModel.StatusMessage = "参数校验错误,请检查您的校验码"; } else if (response.Status == HttpStatusCode.ExpectationFailed) { viewModel.BusinessStatus = -10003; viewModel.StatusMessage = "服务发生异常"; } else if (response.Status == HttpStatusCode.UnsupportedMediaType) { viewModel.BusinessStatus = -10004; viewModel.StatusMessage = "报价短信不允许车牌为空"; } else if (response.Status == HttpStatusCode.NoContent) { viewModel.BusinessStatus = 0; viewModel.MessagePayType = response.MessagePayType; viewModel.StatusMessage = "账号短信余额不足"; } else { viewModel = response; } return(viewModel.ResponseToJson()); }
public SmsResultViewModel SendSms(SmsRequest request, IEnumerable <KeyValuePair <string, string> > pairs) { var response = new SmsResultViewModel(); //参数校验 //获取顶级经纪人信息 bx_agent agentModel = _agentRepository.GetAgent(request.Agent); //获取当前经纪人信息 bx_agent curagentModel = _agentRepository.GetAgent(request.CurAgent); if (agentModel == null || curagentModel == null) { response.Status = HttpStatusCode.Forbidden; return(response); } if (!ValidateReqest(pairs, agentModel.SecretKey, request.SecCode)) { response.Status = HttpStatusCode.Forbidden; return(response); } if ((int)request.BusinessType == 3 && string.IsNullOrWhiteSpace(request.LicenseNo)) { response.Status = HttpStatusCode.UnsupportedMediaType; return(response); } bool isByTopAgent = IsPayByTopAgent(request.CurAgent); //true为走顶级代理人账户,否则走当前账户 int sendAgent = 0; //以哪个代理人id创建 int messagePayType = 0; //当前代理人是顶级代理人可以充值 if (isByTopAgent) {//顶级 sendAgent = request.Agent; messagePayType = agentModel.MessagePayType.HasValue?agentModel.MessagePayType.Value:0; } else {//当前代理 sendAgent = request.CurAgent; messagePayType = curagentModel.MessagePayType.HasValue ? curagentModel.MessagePayType.Value : 0; } //获取代理人的短信信息 var smsAcount = _smsContentRepository.Find(sendAgent); if (smsAcount == null) { response.MessagePayType = messagePayType; response.Status = HttpStatusCode.NoContent; return(response); } string posturl = string.Format("{0}/{1}", _smsCenter, _smsCenterSendSmsMethod); string postData = string.Format("account={0}&password={1}&mobile={2}&smscontent={3}&businessType={4}", smsAcount.sms_account, smsAcount.sms_password, request.Mobile, request.SmsContent, (int)request.BusinessType); string result; try { int ret = HttpWebAsk.Post(posturl, postData, out result); //result = "{\"ResponseType\":\"SubmitSms\",\"TradeTime\":\"2016-05-19 19:35:03\",\"ResultCode\":0,\"Message\":\"提交成功!\"}"; if (!string.IsNullOrEmpty(result)) { var smsResult = result.FromJson <SmsResultModel>(); if (smsResult.ResultCode == 0) { //调取后台接口,将短信发送内容保存到bx_sms_account_content string getData = string.Format("agent_id={0}&sent_mobile={1}&content={2}&agent_name={3}&sent_type={4}&&businessType={5}&license_no={6}", sendAgent, request.Mobile, request.SmsContent, curagentModel.AgentName, request.SentType, (int)request.BusinessType, request.LicenseNo); string getUrl = _smsSqlInterfaceUrl + getData; logInfo.Info(string.Format("短信内容入库请求串:{0}", getUrl)); //string getResult = HttpWebAsk.HttpGet(getUrl); string insertsms = string.Empty; int insertret = HttpWebAsk.Post(_smsSqlInterfaceUrl, getData, out insertsms); logInfo.Info(string.Format("短信内容入库返回结果:{0}", insertsms)); response.BusinessStatus = 1; response.MessagePayType = messagePayType; response.StatusMessage = "短信发送成功"; response.SmsResultModel = smsResult; } else { response.BusinessStatus = 0; response.MessagePayType = messagePayType; response.StatusMessage = smsResult.Message; } } else { response.BusinessStatus = 0; response.MessagePayType = messagePayType; response.StatusMessage = "短信发送失败"; } } catch (Exception ex) { response = new SmsResultViewModel(); response.MessagePayType = messagePayType; response.Status = HttpStatusCode.ExpectationFailed; logError.Info("短信请求发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException); } return(response); }