package com.ruoyi.common.utils;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
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;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description: 阿里短信发送
 * @author: zzl
 * @date: Created in 2021/8/9 9:55
 * @version: v1.0
 * @modified By:
 */
@Slf4j
public class SmsUtils {

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static boolean sendMessage_ali(String accessKeyId, String accessKeySecret, String phone, int smsCode) {
        try {
            Config config = new Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret);
            config.endpoint = "dysmsapi.aliyuncs.com";
            Client client = new Client(config);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", smsCode);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName("农燊高科")
                    .setTemplateCode("SMS_218160192")
                    .setTemplateParam(JSONObject.toJSONString(jsonObject));
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            log.info("SendSms:{}", response);
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }
}
