package com.ff.weijinxiao.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class SmsUtil {

    /**
     * AccessKey ID: LTAI5tLfu7nYMrkiid7hhqN8
     * AccessKey Secret:  GYxltm6oKpTpbU1mKWeXfZEgXqyXcT
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    private static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static void sendSms(String phone, String smsCode) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5tLfu7nYMrkiid7hhqN8", "GYxltm6oKpTpbU1mKWeXfZEgXqyXcT");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setTemplateCode("SMS_217890490")
                .setSignName("铂原联动")
                .setTemplateParam("{\"code\":" + smsCode + "}");

        SendSmsResponse response = client.sendSms(sendSmsRequest);
        log.info("sendSms result: {},{}, result code: {},msg:{}", phone, smsCode, response.getBody().getCode(), response.getBody().getMessage());
    }
}

