package com.casic.missiles.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 阿里云短信工具类
 * @Author: wangpeng
 * @Date: 2023/3/6 16:07
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "sms")
public class SMSUtil {
    private String accessKeyId;

    private String secret;
    // 短信签名
    private String signName;
    // 短信模板
    private String templateCode;
    // 短信服务器区域
    private String regionId;


    public void sendMsg(String phone, String code) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        //下面两个不能动
        request.setSysProduct("Dysmsapi");
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //自定义参数(手机号,验证码,签名,模板)
        request.putQueryParameter("RegoinId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName); //填自己申请的名称
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}"); //验证码

        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("阿里云短信响应信息：" + response.getData());
            boolean success = response.getHttpResponse().isSuccess();
            log.info("短信发送是否成功：" + success);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
