using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace Utils
{
    /// <summary>
    /// alibaba发送手机短信配置
    /// </summary>
    public class AlibabaSMSSdk
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        /// <summary>
        /// AccessKeyId
        /// </summary>
        public static readonly string AccessKeyId = "LTAI5tSKxiy9v3xndqPb5W2K";
        /// <summary>
        /// AccessKeySecret
        /// </summary>
        public static readonly string AccessKeySecret = "EJnDqnrxMIJ64NwgyNGhbqpWN6FLxd";
        /**
        * 使用AK&SK初始化账号Client
        * @param accessKeyId
        * @param accessKeySecret
        * @return Client
        * @throws Exception
        */
        public static AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = accessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = accessKeySecret,
            };
            // 访问的域名
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phone">发送到的手机号</param>
        /// <param name="signName">签名</param>
        /// <param name="TemplateCode">发送的模板id</param>
        /// <param name="TemplateParam">短信模板变量</param>
        public static void SendSms(string phone,string signName,string TemplateCode,string TemplateParam)
        {
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient(AccessKeyId, AccessKeySecret);
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest();
            //发送到的手机号
            sendSmsRequest.PhoneNumbers = phone;
            //签名
            sendSmsRequest.SignName = signName;
            //发送的模板id
            sendSmsRequest.TemplateCode = TemplateCode;
            //短信模板变量
            sendSmsRequest.TemplateParam=TemplateParam;
            // 发送操作
            var res=client.SendSms(sendSmsRequest);
            logger.Info($"res:{JsonConvert.SerializeObject(res)}");
        }
    }
}
