添加发送短信限制每天一次;

修改接口超时限制每天早上9点判断
wushunjie
吴顺杰 2 weeks ago
parent d522e658a1
commit 634a52033a

@ -17,15 +17,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<!-- 通用工具-->
<dependency>
<groupId>com.yingji</groupId>
<artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build> <build>
<!--打包成jar包时的名字--> <!--打包成jar包时的名字-->
<finalName>alarm</finalName> <finalName>alarm</finalName>

@ -6,12 +6,12 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.utils.SmsUtil;
import com.yingji.entity.Alarm; import com.yingji.entity.Alarm;
import com.yingji.entity.QuartzLog; import com.yingji.entity.QuartzLog;
import com.yingji.entity.dto.request.AlarmRequest; import com.yingji.entity.dto.request.AlarmRequest;
import com.yingji.service.AlarmService; import com.yingji.service.AlarmService;
import com.yingji.service.QuartzLogService; import com.yingji.service.QuartzLogService;
import com.yingji.utils.SmsUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -45,6 +45,29 @@ public class AlarmQuartz {
@Resource @Resource
private QuartzLogService quartzLogService; 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(); emergencyAlgorithm();
} catch (Exception e) { } catch (Exception e) {
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("110调用算法接口异常", null); smsUtil.sendSms("110调用算法接口异常", null, null);
} }
} }
quartzLog.setStatus(1); quartzLog.setStatus(1);
} else { } else {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("110原始接口获取token失败", null); smsUtil.sendSms("110原始接口获取token失败", null, null);
log.error("============110原始接口获取token失败============="); log.error("============110原始接口获取token失败=============");
} }
} catch (Exception e) { } catch (Exception e) {
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("110原始接口调用失败", null); smsUtil.sendSms("110原始接口调用失败", null, "110ErrorMes");
quartzLog.setStatus(2); quartzLog.setStatus(2);
e.printStackTrace();
} finally { } finally {
quartzLogService.save(quartzLog); quartzLogService.save(quartzLog);
} }
@ -159,17 +181,6 @@ public class AlarmQuartz {
if (StrUtil.isEmpty(token)) { if (StrUtil.isEmpty(token)) {
return idList; 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(); LocalDateTime nowTime = alarmService.findNowTime();
log.info("==========================获取数据库中最新的时间{}", nowTime); log.info("==========================获取数据库中最新的时间{}", nowTime);

@ -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.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.yingji.redis.RedisCache;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
/** /**
* *
@ -16,16 +22,19 @@ import java.util.Map;
* @author wu * @author wu
* @since 2024/9/24 10:18 * @since 2024/9/24 10:18
*/ */
@Component
public class SmsUtil { public class SmsUtil {
public static final Logger log = LoggerFactory.getLogger(SmsUtil.class); public static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
@Resource
private RedisCache redisCache;
/** /**
* token * token
* *
* @return token * @return token
*/ */
public static String getSmsToken() { public String getSmsToken() {
Map<String, Object> bodyMap = new HashMap<>(); Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("uid", "yjglj_yjjyszzh"); bodyMap.put("uid", "yjglj_yjjyszzh");
bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe"); bodyMap.put("pwd", "f604d2d6de51573b5cef5c95c11ffabe");
@ -45,9 +54,35 @@ public class SmsUtil {
* *
* @param content * @param content
* @param tel 使, * @param tel 使,
* @return * @param errorMes rediskey
*/ */
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<String, Object> 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<String, Object> map = new HashMap<>();
map.put(content, LocalDate.now());
redisCache.setCacheMap("dataKey", map);
}
String token = getSmsToken(); String token = getSmsToken();
Map<String, Object> bodyMap = new HashMap<>(); Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("uid", "yjglj_yjjyszzh"); bodyMap.put("uid", "yjglj_yjjyszzh");
@ -68,6 +103,5 @@ public class SmsUtil {
.body(); .body();
log.error("----------------------------短信响应======================"); log.error("----------------------------短信响应======================");
log.error("短信: " + JSONUtil.toJsonStr(responseStr)); log.error("短信: " + JSONUtil.toJsonStr(responseStr));
return JSONUtil.parseObj(responseStr);
} }
} }

@ -17,15 +17,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<!-- 通用工具-->
<dependency>
<groupId>com.yingji</groupId>
<artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build> <build>
<!--打包成jar包时的名字--> <!--打包成jar包时的名字-->
<finalName>fire</finalName> <finalName>fire</finalName>

@ -4,11 +4,11 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.utils.SmsUtil;
import com.yingji.entity.Fire; import com.yingji.entity.Fire;
import com.yingji.entity.QuartzLog; import com.yingji.entity.QuartzLog;
import com.yingji.service.FireService; import com.yingji.service.FireService;
import com.yingji.service.QuartzLogService; import com.yingji.service.QuartzLogService;
import com.yingji.utils.SmsUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -40,24 +40,35 @@ public class FireQuartz {
@Resource @Resource
private QuartzLogService quartzLogService; private QuartzLogService quartzLogService;
@Resource
private SmsUtil smsUtil;
/** /**
* * 9
*/ */
@Async @Async
@Scheduled(cron = "0 */5 * * * ? ") @Scheduled(cron = "0 0 9 * * ? ")
public void savaData() { public void sendMes() {
log.info("119接口开始" + LocalDateTime.now());
// 获取数据库中最新的创建时间 // 获取数据库中最新的创建时间
LocalDateTime newTime = fireService.findNewTime(); LocalDateTime newTime = fireService.findNewTime();
// 获取当前时间 // 获取当前时间
LocalDateTime currentTime = LocalDateTime.now(); LocalDateTime currentTime = LocalDateTime.now();
// 计算两者之间的持续时间 // 计算两者之间的持续时间
Duration duration = Duration.between(newTime, currentTime); Duration duration = Duration.between(newTime, currentTime);
// 判断时间差是否大于6小时 // 判断时间差是否大于12小时
if (duration.toHours() > 6) { 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 quartzLog = new QuartzLog();
quartzLog.setInterfaceName(2); quartzLog.setInterfaceName(2);
quartzLog.setCreateTime(LocalDateTime.now()); quartzLog.setCreateTime(LocalDateTime.now());
@ -76,7 +87,7 @@ public class FireQuartz {
} }
} catch (Exception e) { } catch (Exception e) {
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("119调用算法接口异常", null); smsUtil.sendSms("119调用算法接口异常", null,null);
} }
} }
quartzLog.setStatus(1); quartzLog.setStatus(1);
@ -84,13 +95,14 @@ public class FireQuartz {
} else { } else {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("119原始接口获取token失败", null); smsUtil.sendSms("119原始接口获取token失败", null,null);
log.error("============119算法接口获取token失败119接口结束============="); log.error("============119算法接口获取token失败119接口结束=============");
} }
} catch (Exception e) { } catch (Exception e) {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("119原始接口调用失败", null); // smsUtil.sendSms("119原始接口调用失败", null,"119ErrorMes");
smsUtil.sendSms("119原始接口调用失败", null,null);
} finally { } finally {
quartzLogService.save(quartzLog); quartzLogService.save(quartzLog);
} }

@ -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<String, Object> 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 rediskey
*/
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<String, Object> 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<String, Object> map = new HashMap<>();
map.put(content, LocalDate.now());
redisCache.setCacheMap("dataKey", map);
}
String token = getSmsToken();
Map<String, Object> 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));
}
}

@ -17,14 +17,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<!-- 通用工具-->
<dependency>
<groupId>com.yingji</groupId>
<artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build> <build>
<!--打包成jar包时的名字--> <!--打包成jar包时的名字-->

@ -3,12 +3,12 @@ package com.yingji.controller;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.utils.SmsUtil;
import com.yingji.base.controller.BaseController; import com.yingji.base.controller.BaseController;
import com.yingji.base.domain.AjaxResult; import com.yingji.base.domain.AjaxResult;
import com.yingji.entity.Fire; import com.yingji.entity.Fire;
import com.yingji.entity.dto.request.FireFindRequest; import com.yingji.entity.dto.request.FireFindRequest;
import com.yingji.service.FireService; import com.yingji.service.FireService;
import com.yingji.utils.SmsUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -37,6 +37,9 @@ public class FireController extends BaseController {
@Resource @Resource
private FireService fireService; private FireService fireService;
@Resource
private SmsUtil smsUtil;
/** /**
* *
* *
@ -73,7 +76,7 @@ public class FireController extends BaseController {
@GetMapping("/sendSms") @GetMapping("/sendSms")
@ApiOperation(value = "发送短信") @ApiOperation(value = "发送短信")
public AjaxResult sendSms() { public AjaxResult sendSms() {
SmsUtil.sendSms("发送短信测试", "17638176947"); smsUtil.sendSms("发送短信测试", "17638176947",null);
return success(); return success();
} }

@ -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<String, Object> 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 rediskey
*/
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<String, Object> 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<String, Object> map = new HashMap<>();
map.put(content, LocalDate.now());
redisCache.setCacheMap("dataKey", map);
}
String token = getSmsToken();
Map<String, Object> 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));
}
}

@ -13,7 +13,6 @@
<module>rescue</module> <module>rescue</module>
<module>page</module> <module>page</module>
<module>fire</module> <module>fire</module>
<module>utils</module>
</modules> </modules>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>

@ -17,14 +17,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<!-- 通用工具-->
<dependency>
<groupId>com.yingji</groupId>
<artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build> <build>
<!--打包成jar包时的名字--> <!--打包成jar包时的名字-->

@ -1,10 +1,10 @@
package com.yingji.quartz; package com.yingji.quartz;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.utils.SmsUtil;
import com.yingji.entity.QuartzLog; import com.yingji.entity.QuartzLog;
import com.yingji.service.AccidentService; import com.yingji.service.AccidentService;
import com.yingji.service.QuartzLogService; import com.yingji.service.QuartzLogService;
import com.yingji.utils.SmsUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -32,9 +32,15 @@ public class AccidentQuartz {
@Resource @Resource
private QuartzLogService quartzLogService; private QuartzLogService quartzLogService;
@Resource
private SmsUtil smsUtil;
/**
* 9
*/
@Async @Async
@Scheduled(cron = "0 */1 * * * ? ") @Scheduled(cron = "0 0 9 * * ? ")
public void savaData() { public void sendMes() {
// 获取数据库中最新的创建时间 // 获取数据库中最新的创建时间
LocalDateTime newTime = accidentService.findNewTime(); LocalDateTime newTime = accidentService.findNewTime();
// 获取当前时间 // 获取当前时间
@ -44,8 +50,13 @@ public class AccidentQuartz {
// 判断时间差是否大于24小时 // 判断时间差是否大于24小时
if (duration.toHours() > 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 quartzLog = new QuartzLog();
quartzLog.setInterfaceName(3); quartzLog.setInterfaceName(3);
quartzLog.setCreateTime(LocalDateTime.now()); quartzLog.setCreateTime(LocalDateTime.now());
@ -58,12 +69,12 @@ public class AccidentQuartz {
} else { } else {
quartzLog.setStatus(2); quartzLog.setStatus(2);
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("120原始接口获取token失败", null); smsUtil.sendSms("120原始接口获取token失败", null, null);
log.info("=================120定时任务获取token失败==============="); log.info("=================120定时任务获取token失败===============");
} }
} catch (Exception e) { } catch (Exception e) {
// 调用短信接口 // 调用短信接口
SmsUtil.sendSms("120原始接口调用失败", null); smsUtil.sendSms("120原始接口调用失败", null, "120ErrorMes");
e.printStackTrace(); e.printStackTrace();
quartzLog.setStatus(2); quartzLog.setStatus(2);
} finally { } finally {

@ -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<String, Object> 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 rediskey
*/
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<String, Object> 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<String, Object> map = new HashMap<>();
map.put(content, LocalDate.now());
redisCache.setCacheMap("dataKey", map);
}
String token = getSmsToken();
Map<String, Object> 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));
}
}

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.yingji</groupId>
<artifactId>yingjiAlgorithms</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>utils</artifactId>
</project>
Loading…
Cancel
Save