using System.Text;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Datory.Utils;

namespace GxPress.Common.Sms
{
    public static class AliySms
    {
        public static bool SendSms(string phone, string code)
        {
            //发送验证码阿里云
            IClientProfile profile =
                DefaultProfile.GetProfile("cn-hangzhou", "LTAI4GKu41MJT8HBxdGAUSWU", "huEFjWImOBDmzyJ8jdive83BXpQm1V");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest
            {
                Method = MethodType.POST,
                Domain = "dysmsapi.aliyuncs.com",
                Version = "2017-05-25",
                Action = "SendSms"
            };
            request.AddQueryParameters("PhoneNumbers", $"{phone}");
            request.AddQueryParameters("SignName", "同岑");
            request.AddQueryParameters("TemplateCode", "SMS_190273884");
            request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                if (response.HttpStatus == 200)
                {
                    var codeByte = Encoding.UTF8.GetBytes(Utilities.JsonSerialize(code));
                    return true;
                }
            }
            catch (ServerException e)
            {
                return false;
            }
            return false;
        }
    }
}