AliSmsUtil.java
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.skua.tool.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.skua.core.exception.JeecgBootException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
/**
* 阿里短信发送
*/
@Slf4j
@Service
public class AliSmsUtil {
static String url = "http://47.104.23.94:8088/";
static String signName = "金控数矿";
// 项目id(每个项目独立使用)
static String projectId = "556a0a9d-5a78-400e-bf10-24fa14a1a925";
// 项目accessKey(每个项目独立使用)
static String accessKey = "9a1832e31135dc5bd72590580dc89943b68554dd8d26af0021af84aae7259773";
static RestTemplate restTemplate = new RestTemplate();
/**
*
* @param phoneNumbers 多个号码,以,隔开
* @param template 短信模板id
* @param message 短信内容 模板内容与模板参数必须完全匹配
*/
public static void sendMsg(String phoneNumbers, String template, Map<String, Object> message){
RequestEntity<Map> requestEntity = null;
ResponseEntity<Map> responseEntity = null;
try {
Map requestBody = new HashMap();
requestBody.put("phone_numbers", phoneNumbers);
requestBody.put("template", template);
requestBody.put("message", new ObjectMapper().writeValueAsString(message));
requestBody.put("operator", "aliyun");
requestBody.put("sign_name", signName);
requestBody.put("project_id", projectId);
requestBody.put("access_key", accessKey);
requestEntity = RequestEntity.post(new URI(url + "sms/send"))
.header("content-type","application/json")
.body(requestBody, Map.class);
responseEntity = restTemplate.exchange(requestEntity, Map.class);
Integer code = (Integer) responseEntity.getBody().get("code");
if (code != 200) {
log.error(String.format("阿里短信发送失败 请求=%s 响应=%s", requestBody.toString(), responseEntity.toString()));
}
} catch (Exception e) {
log.error(String.format("阿里短信发送失败 请求=%s 响应=%s",
requestEntity == null ? "" : requestEntity.toString() ,
responseEntity == null ? "" : responseEntity.toString()), e);
throw new JeecgBootException("阿里短信发送失败");
}
}
/**
* 发送阈值报警短信
*/
public static void sendLimitAlarmMsg() {
HashMap<String, Object> message = new HashMap<>();
message.put("time","2024-10-12");
message.put("factory","天长市污水处理厂");
message.put("alarmParamName","进水COD");
message.put("actualAvgValue","130"+"mg/L");
message.put("limit","105"+"mg/L");
message.put("alarmParamUnit","大于上限");
AliSmsUtil.sendMsg("18519526218", "SMS_474510018",message);
}
public static void main(String[] args) {
sendLimitAlarmMsg();
}
}