Exemplo n.º 1
0
        /// <summary>
        /// 短信发送
        /// </summary>
        /// <returns></returns>
        public async Task SendAsync(string tel, INotify notify)
        {
            if (string.IsNullOrEmpty(tel))
            {
                return;
            }
            var httpClient = _httpFactory.CreateClient();

            var msg     = notify.Execute();
            var request = new Cloud253SmsRequest()
            {
                Account  = _config.Account,
                Password = _config.Password,
                Phone    = tel,
                Msg      = msg,
                Report   = true
            };

            var log = new SmsLog
            {
                Tel      = tel,
                Message  = msg,
                Createat = DateTime.Now
            };

            try
            {
                var context = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
                using (var response = await httpClient.PostAsync(_config.Url, context))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        string jsonBody = await response.Content.ReadAsStringAsync();

                        var result = JsonConvert.DeserializeObject <Cloud243SmsResponse>(jsonBody);

                        log.Code     = result.Code;
                        log.ErrorMsg = result.ErrorMsg;
                        log.Time     = result.Time;
                        log.MsgId    = result.MsgId;
                    }

                    await _smsLogRepository.InsertAsync(log);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
            }
        }