Exemple #1
0
        public static CustomJsonResult Send(string template, string smsparam, string mobile, out string token, string validCode = null, int?expireSecond = null)
        {
            CustomJsonResult result  = new CustomJsonResult();
            IClientProfile   profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAInW2wvf70MRTU", "wCceTF0BOmMPgctSPPLfmMNMfyFRXS");
            IAcsClient       client  = new DefaultAcsClient(profile);

            SingleSendSmsRequest request = new SingleSendSmsRequest();

            LumosDbContext currentDb = new LumosDbContext();

            SysSmsSendHistory sendHistory = new SysSmsSendHistory();

            token                      = Guid.NewGuid().ToString();
            sendHistory.Token          = token;
            sendHistory.ApiName        = "AliyunSingleSendSmsUtils";
            sendHistory.TemplateParams = smsparam;
            sendHistory.TemplateCode   = template;
            sendHistory.Phone          = mobile;
            sendHistory.CreateTime     = DateTime.Now;
            sendHistory.Creator        = 0;
            sendHistory.ValidCode      = validCode;
            if (expireSecond != null)
            {
                sendHistory.ExpireTime = sendHistory.CreateTime.AddSeconds(expireSecond.Value);
            }

            try
            {
                request.SignName     = "全线通信息";  //"管理控制台中配置的短信签名(状态必须是验证通过)"
                request.TemplateCode = template; //管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)"
                request.RecNum       = mobile;   //"接收号码,多个号码可以逗号分隔"
                request.ParamString  = smsparam; //短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。"
                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);


                sendHistory.Result = Enumeration.SysSmsSendResult.Success;

                result = new CustomJsonResult(ResultType.Success, "发送成功");
            }
            catch (ServerException ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.ErrorCode;

                result = new CustomJsonResult(ResultType.Exception, "发送失败");
            }
            catch (ClientException ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.ErrorCode;

                result = new CustomJsonResult(ResultType.Exception, "发送失败");
            }
            catch (Exception ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.Message;

                result = new CustomJsonResult(ResultType.Exception, "发送失败");
            }

            currentDb.SysSmsSendHistory.Add(sendHistory);
            currentDb.SaveChanges();

            return(result);
        }
        public static CustomJsonResult Send(string template, string smsparam, string mobile)
        {
            ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            LumosDbContext currentDb = new LumosDbContext();

            SysSmsSendHistory sendHistory = new SysSmsSendHistory();

            sendHistory.ApiName        = "sms.market.alicloudapi.com";
            sendHistory.TemplateParams = smsparam;
            sendHistory.TemplateCode   = template;
            sendHistory.Phone          = mobile;
            sendHistory.Creator        = 0;
            sendHistory.CreateTime     = DateTime.Now;

            CustomJsonResult result = new CustomJsonResult();


            try
            {
                String querys = string.Format("ParamString={1}&RecNum={2}&SignName={3}&TemplateCode={0}", template, UrlEncode(smsparam), mobile, "收款易");

                String          bodys        = "";
                String          url          = host + path;
                HttpWebRequest  httpRequest  = null;
                HttpWebResponse httpResponse = null;

                if (0 < querys.Length)
                {
                    url = url + "?" + querys;
                }

                if (host.Contains("https://"))
                {
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(url);
                }
                httpRequest.Method = method;
                httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
                if (0 < bodys.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }

                httpResponse = (HttpWebResponse)httpRequest.GetResponse();

                Stream       st     = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));

                string r = reader.ReadToEnd();


                AliCloudApiResult apiResult = Newtonsoft.Json.JsonConvert.DeserializeObject <AliCloudApiResult>(r);

                if (apiResult.Success)
                {
                    sendHistory.Result = Enumeration.SysSmsSendResult.Success;
                    currentDb.SysSmsSendHistory.Add(sendHistory);
                    currentDb.SaveChanges();


                    result = new CustomJsonResult(ResultType.Success, "发送成功");
                }
                else
                {
                    sendHistory.Result        = Enumeration.SysSmsSendResult.Failure;
                    sendHistory.FailureReason = string.Format("描述:{0}", apiResult.Message);
                    currentDb.SysSmsSendHistory.Add(sendHistory);
                    currentDb.SaveChanges();

                    log.ErrorFormat("调用短信{0}接口-错误信息:{1}", sendHistory.ApiName, apiResult.Message);

                    result = new CustomJsonResult(ResultType.Failure, "发送失败");
                }


                return(result);
            }
            catch (Exception ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.Message;

                currentDb.SysSmsSendHistory.Add(sendHistory);
                currentDb.SaveChanges();

                log.ErrorFormat("调用短信{0}接口-错误信息:{1},描述:{2}", sendHistory.ApiName, ex.Message, ex.StackTrace);

                return(new CustomJsonResult(ResultType.Failure, "发送失败"));
            }

            return(result);
        }
        public static CustomJsonResult Send(string template, string smsparam, string mobile, out string token, string validCode = null, int?expireSecond = null)
        {
            String           product = "Dysmsapi";              //短信API产品名称
            String           domain  = "dysmsapi.aliyuncs.com"; //短信API产品域名
            CustomJsonResult result  = new CustomJsonResult();
            IClientProfile   profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAI1kaGcK7uE9Hf", "95x0VXSdph8lMvjLRvsv8sscCpTvWL");

            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient     acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request   = new SendSmsRequest();

            LumosDbContext currentDb = new LumosDbContext();

            SysSmsSendHistory sendHistory = new SysSmsSendHistory();

            token                      = Guid.NewGuid().ToString();
            sendHistory.Token          = token;
            sendHistory.ApiName        = "AliyunSingleSendSmsUtils";
            sendHistory.TemplateParams = smsparam;
            sendHistory.TemplateCode   = template;
            sendHistory.Phone          = mobile;
            sendHistory.CreateTime     = DateTime.Now;
            sendHistory.Creator        = 0;
            sendHistory.ValidCode      = validCode;
            if (expireSecond != null)
            {
                sendHistory.ExpireTime = sendHistory.CreateTime.AddSeconds(expireSecond.Value);
            }

            try
            {
                request.SignName      = "好易联";    //"管理控制台中配置的短信签名(状态必须是验证通过)"
                request.PhoneNumbers  = mobile;   //"接收号码,多个号码可以逗号分隔"
                request.TemplateCode  = template; //管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)"
                request.TemplateParam = smsparam; //短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。"
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                if (sendSmsResponse.Code == "OK")
                {
                    sendHistory.Result = Enumeration.SysSmsSendResult.Success;
                    result             = new CustomJsonResult(ResultType.Success, "发送成功.");
                }
                else
                {
                    sendHistory.Result        = Enumeration.SysSmsSendResult.Failure;
                    sendHistory.FailureReason = sendSmsResponse.Message;
                    result = new CustomJsonResult(ResultType.Failure, "发送失败.");
                }
            }
            catch (ServerException ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.ErrorCode;

                result = new CustomJsonResult(ResultType.Exception, "发送失败..");
            }
            catch (ClientException ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.ErrorCode;

                result = new CustomJsonResult(ResultType.Exception, "发送失败...");
            }
            catch (Exception ex)
            {
                sendHistory.Result = Enumeration.SysSmsSendResult.Exception;

                sendHistory.FailureReason = ex.Message;

                result = new CustomJsonResult(ResultType.Exception, "发送失败....");
            }

            currentDb.SysSmsSendHistory.Add(sendHistory);
            currentDb.SaveChanges();

            return(result);
        }