package com.casic.util; // This file is auto-generated, don't edit it. Thanks. import com.alibaba.fastjson.JSONObject; import com.aliyun.auth.credentials.Credential; import com.aliyun.auth.credentials.provider.StaticCredentialProvider; import com.aliyun.sdk.service.dysmsapi20170525.models.*; import com.aliyun.sdk.service.dysmsapi20170525.*; import com.casic.config.AliYunConfig; import com.google.gson.Gson; import darabonba.core.client.ClientOverrideConfiguration; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.time.Duration; import java.util.*; import java.util.concurrent.CompletableFuture; @AllArgsConstructor @Component @Slf4j public class SendBatchSmsUtil { private AliYunConfig aliYunConfig; public void sendMsg(String phoneJson, String msgJson,String aliSignNameJson) { StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId(aliYunConfig.getKey()) .accessKeySecret(aliYunConfig.getScrect()) .build()); AsyncClient client = AsyncClient.builder() .region("cn-shanghai") // Region ID .credentialsProvider(provider) .overrideConfiguration( ClientOverrideConfiguration.create() .setEndpointOverride(aliYunConfig.getUrl()) .setConnectTimeout(Duration.ofSeconds(30)) ) .build(); SendBatchSmsRequest sendBatchSmsRequest = SendBatchSmsRequest.builder() .phoneNumberJson(phoneJson) .signNameJson(aliSignNameJson) .templateCode(aliYunConfig.getTemplateCode()) .templateParamJson(msgJson) .build(); CompletableFuture<SendBatchSmsResponse> response = client.sendBatchSms(sendBatchSmsRequest); try { SendBatchSmsResponse resp = response.get(); System.out.println(new Gson().toJson(resp)); } catch (Exception ex) { log.error("发送消息出现异常,手机号内容为{},信息内容为{}",phoneJson,msgJson,ex.getMessage()); }finally { client.close(); } } // public static void main(String[] args) throws Exception { // StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() // .accessKeyId("LTAI5tDFNucQF2Bdy4fHgzrN") // .accessKeySecret("hZ22QQPxQue1G0R8Ty2bo6GimrLdoB") // .build()); // // AsyncClient client = AsyncClient.builder() // .region("cn-shanghai") // Region ID // .credentialsProvider(provider) // .overrideConfiguration( // ClientOverrideConfiguration.create() // .setEndpointOverride("dysmsapi.aliyuncs.com") // .setConnectTimeout(Duration.ofSeconds(30)) // ) // .build(); // List<Map<String, String>> msgList = new ArrayList<>(); // Map<String, String> msgMap = new HashMap<>(); // msgMap.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); // msgMap.put("devcode", "123456"); // msgMap.put("value", "24Vol"); // msgMap.put("content", "浓度超限"); // msgList.add(msgMap); // Map<String, String> msgMapCoye=(Map)((HashMap<String, String>) msgMap).clone(); // msgList.add(msgMapCoye); // System.out.println(JSONObject.toJSONString(msgList)); // SendBatchSmsRequest sendBatchSmsRequest = SendBatchSmsRequest.builder() // .phoneNumberJson("[\"13651065090\",\"13652031267\"]") // .signNameJson("[\"柴壮告警提醒\",\"柴壮告警提醒\"]") // .templateCode("SMS_268475055") // .templateParamJson(JSONObject.toJSONString(msgList)) // .build(); // // CompletableFuture<SendBatchSmsResponse> response = client.sendBatchSms(sendBatchSmsRequest); // SendBatchSmsResponse resp = response.get(); // System.out.println(new Gson().toJson(resp)); // client.close(); // } }