package com.ruoyi.common.utils;


import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * @author tjf
 * @Date: 2021/07/15/10:21
 */
public class SendSmsUtils {
    //短信参数
    static final String ACCESS_KEY_ID = "LTAI5tNA2fcBJH6EWRH6Pxr6";
    static final String ACCESS_KEY_SECRET = "5WdaPEOvC3u9LC7pwy2DQ9pgmJvgUr";


    //生成X位验证码
    public static String getCode(Integer num) {
        String[] codes = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < num; i++) {
            int j = (int) (Math.random() * 10);
            if (j <= 0) {
                j = 1;
            }
            code.append(codes[j - 1]);

        }
        return code.toString();
    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            // 必填，您的 AccessKey ID
            .setAccessKeyId(ACCESS_KEY_ID)
            // 必填，您的 AccessKey Secret
            .setAccessKeySecret(ACCESS_KEY_SECRET);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    /**
     * 发送短信消息
     *
     * @return
     */
    public static String sendSms(String phone, String templateCode, String smsCode) {
        String code = "";
        try {
            // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
            com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();

            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                //手机号码
                .setPhoneNumbers(phone)
                //短信签名名称。中新云
                .setSignName("中新云")
                //短信模板变量对应的实际值{"name": code}
                .setTemplateParam(smsCode)
                //短信模板CODE
                .setTemplateCode(templateCode);
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            code = sendSmsResponse.getBody().code;
        } catch (Exception _error) {
        }
        return code;
    }

    /**
     * 阿里云批量发送 短信接口,一次最多100个手机号码
     *
     * @return
     * @throws
     */
    public static SendBatchSmsResponse sendBatchSms(SendBatchSmsRequest sendBatchSmsRequest){
        try {
            com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
            RuntimeOptions runtime = new RuntimeOptions();
            SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
            return sendBatchSmsResponse;
            // 复制代码运行请自行打印 API 的返回值
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

    /**
     * 发送注册的随机密码
     *
     * @return
     */
    public static String sendPassword(String code, String phone) {
        try {
            // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
            com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
            String smsCode = "{\"code\":\"" + code + "\"}";
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                //手机号码
                .setPhoneNumbers(phone)
                //短信签名名称。潜山市数据资源局
                .setSignName("中新云")
                //短信模板CODE
                .setTemplateCode("SMS_219525380")
                //短信模板变量对应的实际值{"name": code}
                .setTemplateParam(smsCode);
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            code = sendSmsResponse.getBody().code;
        } catch (Exception _error) {
        }
        return code;
    }
}

