package com.xxl.job.executor.util;

import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.models.*;
import org.springframework.stereotype.Component;


/**
 * Created on 17/6/7.
 * 短信API产品的DEMO程序,工程中包含了一个SmsDemo类，直接通过
 * 执行main函数即可体验短信产品API功能(只需要将AK替换成开通了云通信-短信产品功能的AK即可)
 * 工程依赖了2个jar包(存放在工程的libs目录下)
 * 1:aliyun-java-sdk-core.jar
 * 2:aliyun-java-sdk-dysmsapi.jar
 *
 * 备注:Demo工程编码采用UTF-8
 * 国际短信发送请勿参照此DEMO
 */
@Component
public class SmsUtils {

    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId("bGRhazM4YjU2NzgzYTQ0N2QzNjIyMGFm")
                // 您的 AccessKey Secret
                .setAccessKeySecret("bGRzazg4ZGFlMWYwNWE4MDAyY2RlYWU2");
        // 访问的域名
        config.endpoint = "apisms.landui.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    public SendSmsResponse sendSms(String phone_num,String codetemp, int flag) throws Exception {
        String templateCode = "";
        String templateParam = "";
        switch (flag){
            case 2:
                templateCode = "1792";
                templateParam = "{ \"message\":\""+codetemp+"\"}";
                break;
            default:
                templateCode = "1792";

        }

        com.aliyun.dysmsapi20170525.Client client = SmsUtils.createClient("accessKeyId", "accessKeySecret");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone_num) // 手机号码
                .setSignName("留言板提醒") // 签名
                .setTemplateCode(templateCode); // 模板code
                //.setTemplateParam(templateParam); // 可选 有模板变量时请填写
        if(!"".equals(templateParam))sendSmsRequest.setTemplateParam(templateParam);
        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse sendSmsResponse = null;
        try {
            // 复制代码运行请自行打印 API 的返回值
            sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
//            System.out.println(sendSmsResponse.getBody().getCode());
//            System.out.println(sendSmsResponse.getBody().getMessage());
//            System.out.println(sendSmsResponse.getBody().getRequestId());
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            error.printStackTrace();
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            error.printStackTrace();
        }
        return sendSmsResponse;
    }

}
