package com.limap.common.utils.ali;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.*;

import javax.annotation.Resource;

/**
 * 阿里云短信发送
 * @author limap
 */
public class SendSmsUtil {

    @Resource
    private ALiConfigEntity aLiConfigEntity;


    /**
     * 发送短信验证码
     * @param param 验证码
     * @param tel 发送手机号
     * @throws Exception
     */
    public static boolean sendCodeSms(JSONObject param, String tel) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5tKv3CygUJwCd8W9p6aR", "KjOSdAMh4Tgdyj0he1mM5xk6JBYiY5");
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(tel)
                .setSignName("鱼测云")
                .setTemplateCode("SMS_465720430")
                .setTemplateParam(param.toJSONString());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String statusCode = sendSmsResponse.getBody().getCode();
            if("OK".equals(statusCode)){
                return true;
            }
        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return false;
    }



    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

}
