package org.dromara.system.utils;




import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
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);
            code.append(codes[j]);
        }
        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;
    }

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

