diff --git a/alarm/pom.xml b/alarm/pom.xml
index 7aac696..123dcc1 100644
--- a/alarm/pom.xml
+++ b/alarm/pom.xml
@@ -17,15 +17,6 @@
UTF-8
-
-
-
- com.yingji
- utils
- 0.0.1-SNAPSHOT
-
-
-
alarm
diff --git a/alarm/src/main/java/com/yingji/quartz/AlarmQuartz.java b/alarm/src/main/java/com/yingji/quartz/AlarmQuartz.java
index d556e22..5dc6fd8 100644
--- a/alarm/src/main/java/com/yingji/quartz/AlarmQuartz.java
+++ b/alarm/src/main/java/com/yingji/quartz/AlarmQuartz.java
@@ -6,12 +6,12 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
-import com.utils.SmsUtil;
import com.yingji.entity.Alarm;
import com.yingji.entity.QuartzLog;
import com.yingji.entity.dto.request.AlarmRequest;
import com.yingji.service.AlarmService;
import com.yingji.service.QuartzLogService;
+import com.yingji.utils.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
@@ -45,6 +45,29 @@ public class AlarmQuartz {
@Resource
private QuartzLogService quartzLogService;
+ @Resource
+ private SmsUtil smsUtil;
+
+
+ /**
+ * 每天早上9点判断数据
+ */
+ @Async
+ @Scheduled(cron = "0 0 9 * * ? ")
+ public void sendMes() {
+ // 获取数据库中最新的创建时间
+ LocalDateTime newTime = alarmService.findNewTime();
+ // 获取当前时间
+ LocalDateTime currentTime = LocalDateTime.now();
+ // 计算两者之间的持续时间
+ Duration duration = Duration.between(newTime, currentTime);
+ // 判断时间差是否大于一小时
+ if (duration.toHours() > 1) {
+ // 调用短信接口
+ smsUtil.sendSms("110原始接口超过1小时无数据", null, null);
+ }
+ }
+
/**
* 保存所有数据
*/
@@ -68,21 +91,20 @@ public class AlarmQuartz {
emergencyAlgorithm();
} catch (Exception e) {
// 调用短信接口
- SmsUtil.sendSms("110调用算法接口异常", null);
+ smsUtil.sendSms("110调用算法接口异常", null, null);
}
}
quartzLog.setStatus(1);
} else {
quartzLog.setStatus(2);
// 调用短信接口
- SmsUtil.sendSms("110原始接口获取token失败", null);
+ smsUtil.sendSms("110原始接口获取token失败", null, null);
log.error("============110原始接口获取token失败=============");
}
} catch (Exception e) {
// 调用短信接口
- SmsUtil.sendSms("110原始接口调用失败", null);
+ smsUtil.sendSms("110原始接口调用失败", null, "110ErrorMes");
quartzLog.setStatus(2);
- e.printStackTrace();
} finally {
quartzLogService.save(quartzLog);
}
@@ -159,17 +181,6 @@ public class AlarmQuartz {
if (StrUtil.isEmpty(token)) {
return idList;
}
- // 获取数据库中最新的创建时间
- LocalDateTime newTime = alarmService.findNewTime();
- // 获取当前时间
- LocalDateTime currentTime = LocalDateTime.now();
- // 计算两者之间的持续时间
- Duration duration = Duration.between(newTime, currentTime);
- // 判断时间差是否大于一小时
- if (duration.toHours() > 1) {
- // 调用短信接口
- SmsUtil.sendSms("110原始接口超过1小时无数据", null);
- }
// 获取数据库中最新的时间
LocalDateTime nowTime = alarmService.findNowTime();
log.info("==========================获取数据库中最新的时间{}", nowTime);
diff --git a/utils/src/main/java/com/utils/SmsUtil.java b/alarm/src/main/java/com/yingji/utils/SmsUtil.java
similarity index 57%
rename from utils/src/main/java/com/utils/SmsUtil.java
rename to alarm/src/main/java/com/yingji/utils/SmsUtil.java
index c484fee..d857965 100644
--- a/utils/src/main/java/com/utils/SmsUtil.java
+++ b/alarm/src/main/java/com/yingji/utils/SmsUtil.java
@@ -1,14 +1,20 @@
-package com.utils;
+package com.yingji.utils;
+import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
+import com.yingji.redis.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import javax.annotation.Resource;
+import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
/**
* 短信通知工具类
@@ -16,16 +22,19 @@ import java.util.Map;
* @author wu
* @since 2024/9/24 10:18
*/
+@Component
public class SmsUtil {
public static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
+ @Resource
+ private RedisCache redisCache;
/**
* 获取短信接口token
*
* @return token
*/
- public static String getSmsToken() {
+ public String getSmsToken() {
Map bodyMap = new HashMap<>();
bodyMap.put("uid", "yjglj_yjjyszzh");
bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
@@ -43,11 +52,37 @@ public class SmsUtil {
/**
* 发送短信
*
- * @param content 短信内容
- * @param tel 手机号多个使用,拼接
- * @return 响应类
+ * @param content 短信内容
+ * @param tel 手机号多个使用,拼接
+ * @param errorMes 异常通知的类别 即redis的key
*/
- public static Object sendSms(String content, String tel) {
+ public void sendSms(String content, String tel, String errorMes) {
+ if (errorMes != null) {
+ Integer minutes = redisCache.getCacheObject(errorMes);
+ if (minutes == null) {
+ redisCache.setCacheObject(errorMes, 1, 10, TimeUnit.MINUTES);
+ return;
+ }
+ if (minutes < 10) {
+ redisCache.setCacheObject(errorMes, minutes + 1);
+ return;
+ }
+ }
+ // 限制每种tel每天只能发一次短信
+ Map dataKey = redisCache.getCacheMap("dataKey");
+ if (CollectionUtil.isNotEmpty(dataKey)) {
+ LocalDate sendDate = (LocalDate) dataKey.get(content);
+ if (LocalDate.now().equals(sendDate)) {
+ return;
+ } else {
+ dataKey.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", dataKey);
+ }
+ } else {
+ Map map = new HashMap<>();
+ map.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", map);
+ }
String token = getSmsToken();
Map bodyMap = new HashMap<>();
bodyMap.put("uid", "yjglj_yjjyszzh");
@@ -68,6 +103,5 @@ public class SmsUtil {
.body();
log.error("----------------------------短信响应======================");
log.error("短信: " + JSONUtil.toJsonStr(responseStr));
- return JSONUtil.parseObj(responseStr);
}
}
diff --git a/fire/pom.xml b/fire/pom.xml
index f8225b4..478aa18 100644
--- a/fire/pom.xml
+++ b/fire/pom.xml
@@ -17,15 +17,6 @@
UTF-8
-
-
-
- com.yingji
- utils
- 0.0.1-SNAPSHOT
-
-
-
fire
diff --git a/fire/src/main/java/com/yingji/quartz/FireQuartz.java b/fire/src/main/java/com/yingji/quartz/FireQuartz.java
index 5c39a4f..bbe36c6 100644
--- a/fire/src/main/java/com/yingji/quartz/FireQuartz.java
+++ b/fire/src/main/java/com/yingji/quartz/FireQuartz.java
@@ -4,11 +4,11 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
-import com.utils.SmsUtil;
import com.yingji.entity.Fire;
import com.yingji.entity.QuartzLog;
import com.yingji.service.FireService;
import com.yingji.service.QuartzLogService;
+import com.yingji.utils.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
@@ -40,24 +40,35 @@ public class FireQuartz {
@Resource
private QuartzLogService quartzLogService;
+ @Resource
+ private SmsUtil smsUtil;
+
/**
- * 保存所有数据
+ * 每天早上9点判断数据
*/
@Async
- @Scheduled(cron = "0 */5 * * * ? ")
- public void savaData() {
- log.info("119接口开始" + LocalDateTime.now());
+ @Scheduled(cron = "0 0 9 * * ? ")
+ public void sendMes() {
// 获取数据库中最新的创建时间
LocalDateTime newTime = fireService.findNewTime();
// 获取当前时间
LocalDateTime currentTime = LocalDateTime.now();
// 计算两者之间的持续时间
Duration duration = Duration.between(newTime, currentTime);
- // 判断时间差是否大于6小时
- if (duration.toHours() > 6) {
+ // 判断时间差是否大于12小时
+ if (duration.toHours() > 12) {
// 调用短信接口
- SmsUtil.sendSms("119原始接口超过6小时无数据", null);
+ smsUtil.sendSms("119原始接口超过12小时无数据", null,null);
}
+ }
+
+ /**
+ * 保存所有数据
+ */
+ @Async
+ @Scheduled(cron = "0 */5 * * * ? ")
+ public void savaData() {
+ log.info("119接口开始" + LocalDateTime.now());
QuartzLog quartzLog = new QuartzLog();
quartzLog.setInterfaceName(2);
quartzLog.setCreateTime(LocalDateTime.now());
@@ -76,7 +87,7 @@ public class FireQuartz {
}
} catch (Exception e) {
// 调用短信接口
- SmsUtil.sendSms("119调用算法接口异常", null);
+ smsUtil.sendSms("119调用算法接口异常", null,null);
}
}
quartzLog.setStatus(1);
@@ -84,13 +95,14 @@ public class FireQuartz {
} else {
quartzLog.setStatus(2);
// 调用短信接口
- SmsUtil.sendSms("119原始接口获取token失败", null);
+ smsUtil.sendSms("119原始接口获取token失败", null,null);
log.error("============119算法接口获取token失败119接口结束=============");
}
} catch (Exception e) {
quartzLog.setStatus(2);
// 调用短信接口
- SmsUtil.sendSms("119原始接口调用失败", null);
+// smsUtil.sendSms("119原始接口调用失败", null,"119ErrorMes");
+ smsUtil.sendSms("119原始接口调用失败", null,null);
} finally {
quartzLogService.save(quartzLog);
}
diff --git a/fire/src/main/java/com/yingji/utils/SmsUtil.java b/fire/src/main/java/com/yingji/utils/SmsUtil.java
new file mode 100644
index 0000000..d857965
--- /dev/null
+++ b/fire/src/main/java/com/yingji/utils/SmsUtil.java
@@ -0,0 +1,107 @@
+package com.yingji.utils;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.yingji.redis.RedisCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.time.LocalDate;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 短信通知工具类
+ *
+ * @author wu
+ * @since 2024/9/24 10:18
+ */
+@Component
+public class SmsUtil {
+ public static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
+
+ @Resource
+ private RedisCache redisCache;
+
+ /**
+ * 获取短信接口token
+ *
+ * @return token
+ */
+ public String getSmsToken() {
+ Map bodyMap = new HashMap<>();
+ bodyMap.put("uid", "yjglj_yjjyszzh");
+ bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
+ String bodyJson = JSONUtil.toJsonStr(bodyMap);
+ String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/token")
+ .body(bodyJson)
+ .execute()
+ .body();
+ JSONObject responseJson = JSONUtil.parseObj(responseStr);
+ log.error("----------------------------短信token======================");
+ log.error("短信token: " + JSONUtil.toJsonStr(responseJson));
+ return (String) responseJson.get("data");
+ }
+
+ /**
+ * 发送短信
+ *
+ * @param content 短信内容
+ * @param tel 手机号多个使用,拼接
+ * @param errorMes 异常通知的类别 即redis的key
+ */
+ public void sendSms(String content, String tel, String errorMes) {
+ if (errorMes != null) {
+ Integer minutes = redisCache.getCacheObject(errorMes);
+ if (minutes == null) {
+ redisCache.setCacheObject(errorMes, 1, 10, TimeUnit.MINUTES);
+ return;
+ }
+ if (minutes < 10) {
+ redisCache.setCacheObject(errorMes, minutes + 1);
+ return;
+ }
+ }
+ // 限制每种tel每天只能发一次短信
+ Map dataKey = redisCache.getCacheMap("dataKey");
+ if (CollectionUtil.isNotEmpty(dataKey)) {
+ LocalDate sendDate = (LocalDate) dataKey.get(content);
+ if (LocalDate.now().equals(sendDate)) {
+ return;
+ } else {
+ dataKey.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", dataKey);
+ }
+ } else {
+ Map map = new HashMap<>();
+ map.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", map);
+ }
+ String token = getSmsToken();
+ Map bodyMap = new HashMap<>();
+ bodyMap.put("uid", "yjglj_yjjyszzh");
+ bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
+ bodyMap.put("extensionNo", "2559");
+ if (StrUtil.isNotEmpty(tel)) {
+ bodyMap.put("tel", tel);
+ } else {
+ bodyMap.put("tel", "18870257135,18261462112,15850922852,18112760590");
+ }
+ bodyMap.put("content", "市应急局提醒:" + content);
+ String bodyJson = JSONUtil.toJsonStr(bodyMap);
+ log.error("bodyJson: " + bodyJson);
+ String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/send")
+ .header("Authorization", "Bearer " + token)
+ .body(bodyJson)
+ .execute()
+ .body();
+ log.error("----------------------------短信响应======================");
+ log.error("短信: " + JSONUtil.toJsonStr(responseStr));
+ }
+}
diff --git a/page/pom.xml b/page/pom.xml
index db0dbdf..fcc74a3 100644
--- a/page/pom.xml
+++ b/page/pom.xml
@@ -17,14 +17,6 @@
UTF-8
-
-
-
- com.yingji
- utils
- 0.0.1-SNAPSHOT
-
-
diff --git a/page/src/main/java/com/yingji/controller/FireController.java b/page/src/main/java/com/yingji/controller/FireController.java
index edda0ab..ff0a8fe 100644
--- a/page/src/main/java/com/yingji/controller/FireController.java
+++ b/page/src/main/java/com/yingji/controller/FireController.java
@@ -3,12 +3,12 @@ package com.yingji.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.utils.SmsUtil;
import com.yingji.base.controller.BaseController;
import com.yingji.base.domain.AjaxResult;
import com.yingji.entity.Fire;
import com.yingji.entity.dto.request.FireFindRequest;
import com.yingji.service.FireService;
+import com.yingji.utils.SmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
@@ -37,6 +37,9 @@ public class FireController extends BaseController {
@Resource
private FireService fireService;
+ @Resource
+ private SmsUtil smsUtil;
+
/**
* 分页条件查询所有数据
*
@@ -73,7 +76,7 @@ public class FireController extends BaseController {
@GetMapping("/sendSms")
@ApiOperation(value = "发送短信")
public AjaxResult sendSms() {
- SmsUtil.sendSms("发送短信测试", "17638176947");
+ smsUtil.sendSms("发送短信测试", "17638176947",null);
return success();
}
diff --git a/page/src/main/java/com/yingji/utils/SmsUtil.java b/page/src/main/java/com/yingji/utils/SmsUtil.java
new file mode 100644
index 0000000..d857965
--- /dev/null
+++ b/page/src/main/java/com/yingji/utils/SmsUtil.java
@@ -0,0 +1,107 @@
+package com.yingji.utils;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.yingji.redis.RedisCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.time.LocalDate;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 短信通知工具类
+ *
+ * @author wu
+ * @since 2024/9/24 10:18
+ */
+@Component
+public class SmsUtil {
+ public static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
+
+ @Resource
+ private RedisCache redisCache;
+
+ /**
+ * 获取短信接口token
+ *
+ * @return token
+ */
+ public String getSmsToken() {
+ Map bodyMap = new HashMap<>();
+ bodyMap.put("uid", "yjglj_yjjyszzh");
+ bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
+ String bodyJson = JSONUtil.toJsonStr(bodyMap);
+ String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/token")
+ .body(bodyJson)
+ .execute()
+ .body();
+ JSONObject responseJson = JSONUtil.parseObj(responseStr);
+ log.error("----------------------------短信token======================");
+ log.error("短信token: " + JSONUtil.toJsonStr(responseJson));
+ return (String) responseJson.get("data");
+ }
+
+ /**
+ * 发送短信
+ *
+ * @param content 短信内容
+ * @param tel 手机号多个使用,拼接
+ * @param errorMes 异常通知的类别 即redis的key
+ */
+ public void sendSms(String content, String tel, String errorMes) {
+ if (errorMes != null) {
+ Integer minutes = redisCache.getCacheObject(errorMes);
+ if (minutes == null) {
+ redisCache.setCacheObject(errorMes, 1, 10, TimeUnit.MINUTES);
+ return;
+ }
+ if (minutes < 10) {
+ redisCache.setCacheObject(errorMes, minutes + 1);
+ return;
+ }
+ }
+ // 限制每种tel每天只能发一次短信
+ Map dataKey = redisCache.getCacheMap("dataKey");
+ if (CollectionUtil.isNotEmpty(dataKey)) {
+ LocalDate sendDate = (LocalDate) dataKey.get(content);
+ if (LocalDate.now().equals(sendDate)) {
+ return;
+ } else {
+ dataKey.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", dataKey);
+ }
+ } else {
+ Map map = new HashMap<>();
+ map.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", map);
+ }
+ String token = getSmsToken();
+ Map bodyMap = new HashMap<>();
+ bodyMap.put("uid", "yjglj_yjjyszzh");
+ bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
+ bodyMap.put("extensionNo", "2559");
+ if (StrUtil.isNotEmpty(tel)) {
+ bodyMap.put("tel", tel);
+ } else {
+ bodyMap.put("tel", "18870257135,18261462112,15850922852,18112760590");
+ }
+ bodyMap.put("content", "市应急局提醒:" + content);
+ String bodyJson = JSONUtil.toJsonStr(bodyMap);
+ log.error("bodyJson: " + bodyJson);
+ String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/send")
+ .header("Authorization", "Bearer " + token)
+ .body(bodyJson)
+ .execute()
+ .body();
+ log.error("----------------------------短信响应======================");
+ log.error("短信: " + JSONUtil.toJsonStr(responseStr));
+ }
+}
diff --git a/pom.xml b/pom.xml
index 78fae65..98bdd27 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,7 +13,6 @@
rescue
page
fire
- utils
1.8
diff --git a/rescue/pom.xml b/rescue/pom.xml
index bd58434..b5d26c1 100644
--- a/rescue/pom.xml
+++ b/rescue/pom.xml
@@ -17,14 +17,6 @@
UTF-8
-
-
-
- com.yingji
- utils
- 0.0.1-SNAPSHOT
-
-
diff --git a/rescue/src/main/java/com/yingji/quartz/AccidentQuartz.java b/rescue/src/main/java/com/yingji/quartz/AccidentQuartz.java
index 1e6b89f..e9eb2cc 100644
--- a/rescue/src/main/java/com/yingji/quartz/AccidentQuartz.java
+++ b/rescue/src/main/java/com/yingji/quartz/AccidentQuartz.java
@@ -1,10 +1,10 @@
package com.yingji.quartz;
import cn.hutool.core.util.StrUtil;
-import com.utils.SmsUtil;
import com.yingji.entity.QuartzLog;
import com.yingji.service.AccidentService;
import com.yingji.service.QuartzLogService;
+import com.yingji.utils.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
@@ -32,9 +32,15 @@ public class AccidentQuartz {
@Resource
private QuartzLogService quartzLogService;
+ @Resource
+ private SmsUtil smsUtil;
+
+ /**
+ * 每天早上9点判断数据
+ */
@Async
- @Scheduled(cron = "0 */1 * * * ? ")
- public void savaData() {
+ @Scheduled(cron = "0 0 9 * * ? ")
+ public void sendMes() {
// 获取数据库中最新的创建时间
LocalDateTime newTime = accidentService.findNewTime();
// 获取当前时间
@@ -44,8 +50,13 @@ public class AccidentQuartz {
// 判断时间差是否大于24小时
if (duration.toHours() > 24) {
// 调用短信接口
- SmsUtil.sendSms("120原始接口超过24小时无数据", null);
+ smsUtil.sendSms("120原始接口超过24小时无数据", null, null);
}
+ }
+
+ @Async
+ @Scheduled(cron = "0 */1 * * * ? ")
+ public void savaData() {
QuartzLog quartzLog = new QuartzLog();
quartzLog.setInterfaceName(3);
quartzLog.setCreateTime(LocalDateTime.now());
@@ -58,12 +69,12 @@ public class AccidentQuartz {
} else {
quartzLog.setStatus(2);
// 调用短信接口
- SmsUtil.sendSms("120原始接口获取token失败", null);
+ smsUtil.sendSms("120原始接口获取token失败", null, null);
log.info("=================120定时任务获取token失败===============");
}
} catch (Exception e) {
// 调用短信接口
- SmsUtil.sendSms("120原始接口调用失败", null);
+ smsUtil.sendSms("120原始接口调用失败", null, "120ErrorMes");
e.printStackTrace();
quartzLog.setStatus(2);
} finally {
diff --git a/rescue/src/main/java/com/yingji/utils/SmsUtil.java b/rescue/src/main/java/com/yingji/utils/SmsUtil.java
new file mode 100644
index 0000000..d857965
--- /dev/null
+++ b/rescue/src/main/java/com/yingji/utils/SmsUtil.java
@@ -0,0 +1,107 @@
+package com.yingji.utils;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.yingji.redis.RedisCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.time.LocalDate;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 短信通知工具类
+ *
+ * @author wu
+ * @since 2024/9/24 10:18
+ */
+@Component
+public class SmsUtil {
+ public static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
+
+ @Resource
+ private RedisCache redisCache;
+
+ /**
+ * 获取短信接口token
+ *
+ * @return token
+ */
+ public String getSmsToken() {
+ Map bodyMap = new HashMap<>();
+ bodyMap.put("uid", "yjglj_yjjyszzh");
+ bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
+ String bodyJson = JSONUtil.toJsonStr(bodyMap);
+ String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/token")
+ .body(bodyJson)
+ .execute()
+ .body();
+ JSONObject responseJson = JSONUtil.parseObj(responseStr);
+ log.error("----------------------------短信token======================");
+ log.error("短信token: " + JSONUtil.toJsonStr(responseJson));
+ return (String) responseJson.get("data");
+ }
+
+ /**
+ * 发送短信
+ *
+ * @param content 短信内容
+ * @param tel 手机号多个使用,拼接
+ * @param errorMes 异常通知的类别 即redis的key
+ */
+ public void sendSms(String content, String tel, String errorMes) {
+ if (errorMes != null) {
+ Integer minutes = redisCache.getCacheObject(errorMes);
+ if (minutes == null) {
+ redisCache.setCacheObject(errorMes, 1, 10, TimeUnit.MINUTES);
+ return;
+ }
+ if (minutes < 10) {
+ redisCache.setCacheObject(errorMes, minutes + 1);
+ return;
+ }
+ }
+ // 限制每种tel每天只能发一次短信
+ Map dataKey = redisCache.getCacheMap("dataKey");
+ if (CollectionUtil.isNotEmpty(dataKey)) {
+ LocalDate sendDate = (LocalDate) dataKey.get(content);
+ if (LocalDate.now().equals(sendDate)) {
+ return;
+ } else {
+ dataKey.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", dataKey);
+ }
+ } else {
+ Map map = new HashMap<>();
+ map.put(content, LocalDate.now());
+ redisCache.setCacheMap("dataKey", map);
+ }
+ String token = getSmsToken();
+ Map bodyMap = new HashMap<>();
+ bodyMap.put("uid", "yjglj_yjjyszzh");
+ bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
+ bodyMap.put("extensionNo", "2559");
+ if (StrUtil.isNotEmpty(tel)) {
+ bodyMap.put("tel", tel);
+ } else {
+ bodyMap.put("tel", "18870257135,18261462112,15850922852,18112760590");
+ }
+ bodyMap.put("content", "市应急局提醒:" + content);
+ String bodyJson = JSONUtil.toJsonStr(bodyMap);
+ log.error("bodyJson: " + bodyJson);
+ String responseStr = HttpRequest.post("http://2.46.42.43:80/admin-api/sms/send")
+ .header("Authorization", "Bearer " + token)
+ .body(bodyJson)
+ .execute()
+ .body();
+ log.error("----------------------------短信响应======================");
+ log.error("短信: " + JSONUtil.toJsonStr(responseStr));
+ }
+}
diff --git a/utils/pom.xml b/utils/pom.xml
deleted file mode 100644
index 87e9d82..0000000
--- a/utils/pom.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
- com.yingji
- yingjiAlgorithms
- 0.0.1-SNAPSHOT
-
- 4.0.0
- utils
-
\ No newline at end of file