package com.ebts.framework.config;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;

import java.util.Random;

/**
 * @author: Clay
 * @date: 2021/8/8 22:54
 * @description：SMSConfig
 * @email: clay@hchyun.com
 */
public class SMSConfig {

    private static String accessKeyId;
    private static String accessKeySecret;
    private static String signName;

    public void setAccessKeyId(String accessKeyId) {
        SMSConfig.accessKeyId = accessKeyId;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        SMSConfig.accessKeySecret = accessKeySecret;
    }

    public void setSignName(String signName) {
        SMSConfig.signName = signName;
    }

    public static Object string(){
        System.out.println(accessKeyId+accessKeySecret+signName);
        return accessKeyId+accessKeySecret+signName;
    }

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

    public static String code() {
        Random rand = new Random();
        return String.valueOf(rand.nextInt(1000000) + 1);
    }

    /**
     * 验证码
     * @param phone
     * @param code
     * @return
     */
    public static boolean smsCode(String phone, String code) {
        Client client = null;
        try {
            client = SMSConfig.createClient();
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(signName)
                    .setTemplateCode("SMS_176925470")
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            // 复制代码运行请自行打印 API 的返回值
            client.sendSms(sendSmsRequest);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    /**
     * 报警
     * @param phone
     * @param code
     * @return
     */
    public static boolean alarm(String phone, String code) {
        Client client = null;
        try {
            client = SMSConfig.createClient();
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(signName)
                    .setTemplateCode("SMS_176925470")
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            // 复制代码运行请自行打印 API 的返回值
            client.sendSms(sendSmsRequest);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


}
