using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Utility
{
    public static class SmsNewHelper
    {
        public static string Send(string mobile,string content)
        {
            string result = string.Empty;
           
            var param = new
            {
                ecName = "安阳市行政便民服务中心",
                apId = "ayrx",
                mobiles = mobile,
                content = content,
                sign = "q4dGVsCk2",
                addSerial = "",
                mac = MD5("安阳市行政便民服务中心" + "ayrx" + "Ay+12345-Rx" + mobile + content + "q4dGVsCk2").ToLower(),
            };
           
            string sendresult = HttpMethods.HttpPost("http://112.35.1.155:1992/sms/norsubmit", Convert.ToBase64String(Encoding.UTF8.GetBytes(param.ToJson())), "application/json");
          
            var rt = sendresult.ToJObject();
            //success
            if (rt["success"].ToString() == "false")
            {
                result = rt["rspcod"].ToString();
            }
            return result;
        }

        /// <summary>
        /// MD5函数
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');

            return ret;
        }
    }
}
