报警不同等级措施
parent
b51d8fad5d
commit
9c86bbf900
|
@ -172,8 +172,32 @@
|
|||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--短信依赖 5条依赖-->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-util</artifactId>
|
||||
<version>9.3.7.v20160115</version>
|
||||
</dependency>
|
||||
<!--邮箱-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
package com.muyu.common.core.utils.sms;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
|
||||
|
||||
/**
|
||||
* 邮箱工具类
|
||||
*/
|
||||
@Component
|
||||
public class EmailUtils {
|
||||
|
||||
@Autowired
|
||||
private JavaMailSender javaMailSender;
|
||||
|
||||
//邮件发送
|
||||
public void sendCodeByEmail(String email,String code){
|
||||
|
||||
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||
|
||||
try {
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
helper.setSubject("验证码通知");
|
||||
//helper.setText("<a href='http://192.168.183.1:8080/student/confirmRead?id="+student.getId()+"'>点击</a>",true);
|
||||
helper.setText("您的验证码是:"+code);
|
||||
helper.setFrom("2108429300@qq.com");
|
||||
helper.setTo(email);
|
||||
|
||||
javaMailSender.send(mimeMessage);
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 图片
|
||||
// @Test
|
||||
// public void test(){
|
||||
//
|
||||
// MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||
// try {
|
||||
// MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
// //标题
|
||||
// helper.setSubject("通知");
|
||||
// //内容
|
||||
// helper.setText("你好");
|
||||
// //发件人
|
||||
// helper.setFrom("1074359652@qq.com");
|
||||
// //收件人
|
||||
// helper.setTo("1074359652@qq.com");
|
||||
// //文件
|
||||
// helper.addAttachment("李雪仁.txt",new File("E:\\桌面\\a.txt"));
|
||||
//
|
||||
// javaMailSender.send(mimeMessage);
|
||||
// } catch (MessagingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// 文件
|
||||
// @Test
|
||||
// public void test1(){
|
||||
//
|
||||
// MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||
// try {
|
||||
// MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
// //标题
|
||||
// helper.setSubject("通知");
|
||||
// //内容
|
||||
// helper.setText("你好,给你看涨图片<br>" + "<img src='cid:opp'>",true);
|
||||
// //发件人
|
||||
// helper.setFrom("1074359652@qq.com");
|
||||
// //收件人
|
||||
// helper.setTo("1074359652@qq.com");
|
||||
// //图片
|
||||
// helper.addInline("opp",new FileSystemResource(new File("E:\\桌面\\11.gif")));
|
||||
//
|
||||
// javaMailSender.send(mimeMessage);
|
||||
// } catch (MessagingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,339 @@
|
|||
package com.muyu.common.core.utils.sms;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 短信网络请求工具类
|
||||
* @author Li YongJie
|
||||
* @createTime 2022年04月27日 13:53:00
|
||||
*/
|
||||
public class HttpUtils {
|
||||
|
||||
private static final String SCHEME = "https://";
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doGet(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* post form
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param bodys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
Map<String, String> bodys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (bodys != null) {
|
||||
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
|
||||
|
||||
for (String key : bodys.keySet()) {
|
||||
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
|
||||
}
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
|
||||
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
|
||||
request.setEntity(formEntity);
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post String
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post stream
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put String
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put stream
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doDelete(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
||||
StringBuilder sbUrl = new StringBuilder();
|
||||
sbUrl.append(host);
|
||||
if (!StringUtils.isBlank(path)) {
|
||||
sbUrl.append(path);
|
||||
}
|
||||
if (null != querys) {
|
||||
StringBuilder sbQuery = new StringBuilder();
|
||||
for (Map.Entry<String, String> query : querys.entrySet()) {
|
||||
if (0 < sbQuery.length()) {
|
||||
sbQuery.append("&");
|
||||
}
|
||||
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append(query.getValue());
|
||||
}
|
||||
if (!StringUtils.isBlank(query.getKey())) {
|
||||
sbQuery.append(query.getKey());
|
||||
if (!StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append("=");
|
||||
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 < sbQuery.length()) {
|
||||
sbUrl.append("?").append(sbQuery);
|
||||
}
|
||||
}
|
||||
|
||||
return sbUrl.toString();
|
||||
}
|
||||
|
||||
private static HttpClient wrapClient(String host) {
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
if (host.startsWith(SCHEME)) {
|
||||
sslClient(httpClient);
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* sslClient
|
||||
* @param httpClient
|
||||
*/
|
||||
private static void sslClient(HttpClient httpClient) {
|
||||
try {
|
||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
X509TrustManager tm = new X509TrustManager() {
|
||||
/**
|
||||
* getAcceptedIssuers
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* checkClientTrusted
|
||||
* @param xcs
|
||||
* @param str
|
||||
*/
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
/**
|
||||
* checkServerTrusted
|
||||
* @param xcs
|
||||
* @param str
|
||||
*/
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
};
|
||||
ctx.init(null, new TrustManager[] { tm }, null);
|
||||
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
||||
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
||||
SchemeRegistry registry = ccm.getSchemeRegistry();
|
||||
registry.register(new Scheme("https", 443, ssf));
|
||||
} catch (KeyManagementException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.muyu.common.core.utils.sms;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 短信发送工具类
|
||||
* @author Li YongJie
|
||||
* @createTime 2022年04月27日 13:53:00
|
||||
*/
|
||||
public class MsgUtil {
|
||||
|
||||
|
||||
public static void sendMsg(String phone,String code){
|
||||
|
||||
String host = "http://dingxin.market.alicloudapi.com";
|
||||
String path = "/dx/sendSms";
|
||||
String method = "POST";
|
||||
String appcode = "fe89da4e62954ebda78bd257785e6a53";
|
||||
Map<String, String> headers = new HashMap<String, String>(200,200);
|
||||
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 86873be959334103b775996a22409375
|
||||
headers.put("Authorization", "APPCODE " + appcode);
|
||||
Map<String, String> querys = new HashMap<String, String>(200,200);
|
||||
querys.put("mobile", phone);
|
||||
querys.put("param", "code:"+code);
|
||||
//模板不允许改动。 如果要改动,联系客服
|
||||
querys.put("tpl_id", "TP1711063");
|
||||
Map<String, String> bodys = new HashMap<String, String>(200,200);
|
||||
|
||||
|
||||
try {
|
||||
/**
|
||||
* 重要提示如下:
|
||||
* HttpUtils请从
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
|
||||
* 下载
|
||||
*
|
||||
* 相应的依赖请参照
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
|
||||
*
|
||||
*
|
||||
* 获取response的body
|
||||
* EntityUtils.toString(response.getEntity())
|
||||
*/
|
||||
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
||||
System.out.println(response.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ import com.muyu.business.domain.res.*;
|
|||
import com.muyu.business.mapper.*;
|
||||
import com.muyu.business.service.*;
|
||||
import com.muyu.common.core.utils.*;
|
||||
import com.muyu.common.core.utils.sms.*;
|
||||
import com.muyu.common.security.utils.*;
|
||||
import org.springframework.beans.factory.annotation.*;
|
||||
import org.springframework.stereotype.*;
|
||||
|
@ -31,6 +32,12 @@ public class AlarmLogsServiceImpl extends ServiceImpl<AlarmLogsMapper, AlarmLogs
|
|||
@Autowired
|
||||
private AlarmLogsMapper alarmLogsMapper;
|
||||
|
||||
/**
|
||||
* 注入邮件工具类
|
||||
*/
|
||||
@Autowired
|
||||
private EmailUtils emailUtils;
|
||||
|
||||
@Override
|
||||
public List<AlarmLogs> selectAlarmLogsList(AlarmLogsReq alarmLogsReq) {
|
||||
LambdaQueryWrapper<AlarmLogs> wrapper = new LambdaQueryWrapper<>();
|
||||
|
@ -46,6 +53,18 @@ public class AlarmLogsServiceImpl extends ServiceImpl<AlarmLogsMapper, AlarmLogs
|
|||
|
||||
@Override
|
||||
public int insertAlarmLogs(AlarmLogs alarmLogs) {
|
||||
switch (alarmLogs.getFaultLevel()) {
|
||||
case 1:
|
||||
MsgUtil.sendMsg(SecurityUtils.getLoginUser().getPhonenumber(), "1");
|
||||
alarmLogsMapper.insert(alarmLogs);
|
||||
break;
|
||||
case 2:
|
||||
emailUtils.sendCodeByEmail(SecurityUtils.getLoginUser().getEmail(), "2");
|
||||
alarmLogsMapper.insert(alarmLogs);
|
||||
break;
|
||||
default:
|
||||
alarmLogsMapper.insert(alarmLogs);
|
||||
}
|
||||
return alarmLogsMapper.insert(alarmLogs);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.system.common.model;
|
||||
|
||||
|
||||
import com.muyu.common.core.annotation.*;
|
||||
import com.muyu.system.common.domain.SysUser;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
@ -30,6 +31,13 @@ public class LoginUser implements Serializable
|
|||
*/
|
||||
private String username;
|
||||
|
||||
|
||||
/** 用户邮箱 */
|
||||
private String email;
|
||||
|
||||
/** 手机号码 */
|
||||
private String phonenumber;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
|
@ -149,4 +157,24 @@ public class LoginUser implements Serializable
|
|||
{
|
||||
this.sysUser = sysUser;
|
||||
}
|
||||
|
||||
public String getEmail()
|
||||
{
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email)
|
||||
{
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPhonenumber()
|
||||
{
|
||||
return phonenumber;
|
||||
}
|
||||
|
||||
public void setPhonenumber(String phonenumber)
|
||||
{
|
||||
this.phonenumber = phonenumber;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue