AliSmsUtil.java
6.7 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.skua.tool.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.skua.common.constant.CacheConstant;
import com.skua.core.exception.JeecgBootException;
import com.skua.modules.common.vo.AliAlarm;
import com.skua.modules.common.vo.AliCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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 boolean sendMsg(String phoneNumbers, String template, Map<String, Object> message){
RequestEntity<Map> requestEntity = null;
ResponseEntity<Map> responseEntity = null;
boolean ok = true;//发送结果标识
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");
ok = true;
if (code != 200) {
ok = false;
log.error(String.format("阿里短信发送失败 请求=%s 响应=%s", requestBody.toString(), responseEntity.toString()));
}
} catch (Exception e) {
ok = false;
log.error(String.format("阿里短信发送失败 请求=%s 响应=%s",
requestEntity == null ? "" : requestEntity.toString() ,
responseEntity == null ? "" : responseEntity.toString()), e);
}
return ok;
}
/**
* 发送阈值报警短信
* 在${time}时${factory}采集数据${alarmParamName}值:${actualAvgValue},标准要求${limit},当前${alarmParamUnit},请注意!
*/
public static boolean sendLimitAlarmMsg(AliAlarm aliAlarm) {
boolean ok = true;//发送结果标识
if(StringUtils.isNotEmpty(aliAlarm.getPhone())){
HashMap<String, Object> message = new HashMap<>();
message.put("time", aliAlarm.getTime());
message.put("factory", aliAlarm.getFactory());
message.put("alarmParamName", aliAlarm.getAlarmParamName());
message.put("actualAvgValue", aliAlarm.getAlarmParamValue() + aliAlarm.getAlarmParamUnit());
message.put("limit", aliAlarm.getLimitValue() + aliAlarm.getAlarmParamUnit());
message.put("alarmParamUnit", aliAlarm.getAlarmParamReason());
ok = AliSmsUtil.sendMsg(aliAlarm.getPhone(), CacheConstant.SMS_TEMPLATE_LIMIT, message);
}else{
log.info("=======>>>>>>>>>>【阈值报警】接收人手机号未设定!");
}
return ok;
}
/**
* 发送设备报警短信
* ${factory}于${date},${equip}信号为:${level},该设备位于${part},请及时处理。
*/
public static boolean sendEquipAlarmMsg(AliAlarm aliAlarm) {
boolean ok = true;//发送结果标识
if(StringUtils.isNotEmpty(aliAlarm.getPhone())){
HashMap<String, Object> message = new HashMap<>();
message.put("date", aliAlarm.getTime());
message.put("factory", aliAlarm.getFactory());
message.put("equip", aliAlarm.getAlarmParamName());
message.put("limit", aliAlarm.getLimitValue());
message.put("part", aliAlarm.getAlarmLoction());
ok = AliSmsUtil.sendMsg(aliAlarm.getPhone(), CacheConstant.SMS_TEMPLATE_EQUIP, message);
}else{
log.info("=======>>>>>>>>>>【设备报警】接收人手机号未设定!");
}
return ok;
}
/**
* 发送验证码短信
*/
public static boolean sendCodeMsg(AliCode aliCode) {
boolean ok = true;//发送结果标识
if(StringUtils.isNotEmpty(aliCode.getPhone())){
HashMap<String, Object> message = new HashMap<>();
message.put("code", aliCode.getCode());
message.put("product", aliCode.getProduct());
if("update".equals(aliCode.getType())){
ok = AliSmsUtil.sendMsg(aliCode.getPhone(), CacheConstant.SMS_TEMPLATE_UPDATE, message);
}else if("login".equals(aliCode.getType())){
ok = AliSmsUtil.sendMsg(aliCode.getPhone(), CacheConstant.SMS_TEMPLATE_LOGIN, message);
}else if("register".equals(aliCode.getType())){
ok = AliSmsUtil.sendMsg(aliCode.getPhone(), CacheConstant.SMS_TEMPLATE_REGISTER, message);
}else{
log.info("=======>>>>>>>>>>短信验证码缺少对应模版");
}
}else{
log.info("=======>>>>>>>>>>【验证码】接收人手机号未设定!");
}
return ok;
}
public static void main(String[] args) {
// AliAlarm aliAlarm = new AliAlarm();
// aliAlarm.setPhone("18519526218");
// aliAlarm.setTime("2024-11-01 11:25:29");
// aliAlarm.setFactory("天长市污水处理厂");
// aliAlarm.setAlarmParamName("进水COD");
// aliAlarm.setAlarmParamValue("130");
// aliAlarm.setAlarmParamUnit("mg/L");
// aliAlarm.setLimitValue("100");
// aliAlarm.setAlarmParamReason("大于上限");
// sendLimitAlarmMsg(aliAlarm);
AliCode aliCode = new AliCode();
aliCode.setPhone("18519526218");
aliCode.setCode("889955");
aliCode.setProduct("数字化企业大脑");
aliCode.setType("login");
sendCodeMsg(aliCode);
}
}