using System;
using System.IO;
using System.Net;
using System.Text;
using GxPress.Common.Encrypt;
using Newtonsoft.Json;
namespace GxPress.Common.Sms
{
    public static class MasSms
    {
        /// <summary>
        /// 广西短信
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool SendSms(string phone, string code)
        {
            var url = "http://112.35.1.155:1992/sms/norsubmit";
            try
            {
                var content = $"打死也不能告诉别人，短信验证码：{code}";
                var md5 = EncryptProvider.Md5($"接口联调账号impactpasswd@1342{phone}{content}bM16CfN2B").ToLower();
                var json = "{\"ecName\":\"接口联调账号\", \"apId\":\"impact\", \"mobiles\":\"" + phone + "\", \"content\":\"" + content + "\", \"sign\":\"bM16CfN2B\", \"addSerial\":\"\", \"mac\":\""+md5+"\"}";
                var base64Encode = EncryptProvider.Base64Encode(json);
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(base64Encode); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                //webReq.ContentType = "application/json";
                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                var ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
                var success = JsonConvert.DeserializeObject<SmsSuccess>(ret);
                return success.Success;
            }
            catch (Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }
        }
    }

    public class SmsSuccess
    {
        public string MsgGroup { get; set; }

        public string Rspcod { get; set; }

        public bool Success { get; set; }
    }
}