短信优化

This commit is contained in:
方亮 2026-02-04 09:51:19 +08:00
parent 1f6912a794
commit cb79c296d1
6 changed files with 60 additions and 14 deletions

View File

@ -16,6 +16,7 @@ import com.bonus.message.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.bonus.job.mapper.PmTaskMapper;
import com.bonus.job.domain.PmTask;
@ -41,6 +42,9 @@ public class PmTaskServiceImpl implements PmTaskService{
@Resource
private WorkerService workerService;
@Value("${message.signature}")
private String msgSign;
@Override
public List<PmTask> getMsgTaskList(PmTask task) {
@ -257,9 +261,10 @@ public class PmTaskServiceImpl implements PmTaskService{
List<WorkerVo> userList = new ArrayList<>();
userList.add(o);
MessageSendUtil messageSendUtil = new MessageSendUtil();
String msgResult = messageSendUtil.sendMessage(userList, o.getSendContent());
String content = msgSign +o.getSendContent();
String msgResult = messageSendUtil.sendMessage(userList, content);
if (msgResult == null){
return AjaxResult.error("短信发送返回异常:"+msgResult);
return AjaxResult.error("短信发送返回异常:"+ null);
}
String[] split = msgResult.split(",");
if (split.length == 2 && "ok".equals(split[0])) {

View File

@ -12,6 +12,7 @@ import org.quartz.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -39,6 +40,9 @@ public class WorkerSendMsgTask{
@Autowired
private ISysJobService jobService;
@Value("${message.signature}")
private String msgSign;
SnowflakeIdGenerator idGen = new SnowflakeIdGenerator(1, 1);
/**
@ -49,7 +53,7 @@ public class WorkerSendMsgTask{
try{
//查询任务发送人人员信息
List<WorkerVo> list = mapper.getWorkerByJobId(jobId);
sendInBatches(list);
sendInBatches(list, true);
//如果任务是指执行一次就将任务status停掉
SysJob job = mapper.getJobByJobId(jobId);
if ("2".equals(job.getMisfirePolicy())){
@ -69,7 +73,7 @@ public class WorkerSendMsgTask{
try{
//查询过生日的人员和短信内容
List<WorkerVo> list = mapper.getHappyBirthDayWorkerByJobId(jobId);
sendInBatches(list);
sendInBatches(list, false);
}catch (Exception e){
logger.error("人员短信调度,{}",e.getMessage());
}
@ -77,20 +81,21 @@ public class WorkerSendMsgTask{
/**
* 批量发送短信
* @param workers 人员
* @param flag 是否为普通短信
*/
public void sendInBatches(List<WorkerVo> workers) {
String content = workers.get(0).getSendContent();
public void sendInBatches(List<WorkerVo> workers, Boolean flag) {
long loopId = idGen.nextId();
final int BATCH_SIZE = 200;
if (workers.size() <= BATCH_SIZE) {
// 不超过200人单次发送
msgFlow(workers, content, loopId);
msgFlow(workers, loopId, flag);
} else {
// 超过200人分批次发送
for (int i = 0; i < workers.size(); i += BATCH_SIZE) {
int end = Math.min(i + BATCH_SIZE, workers.size());
List<WorkerVo> batch = workers.subList(i, end);
msgFlow(batch, content, loopId);
msgFlow(batch, loopId, flag);
}
}
}
@ -98,10 +103,21 @@ public class WorkerSendMsgTask{
/**
* 短信发送
*/
private String msgFlow(List<WorkerVo> userList, String content, long loopId) {
private String msgFlow(List<WorkerVo> userList, long loopId, Boolean flag) {
//1.发送短信
MessageSendUtil messageSendUtil = new MessageSendUtil();
String msgResult = messageSendUtil.sendMessage(userList, content);
String msgResult;
if(flag){
String sendContent = userList.get(0).getSendContent();
sendContent = msgSign + sendContent;
msgResult = messageSendUtil.sendMessage(userList,sendContent);
}else{
for (WorkerVo vo : userList) {
String sex = "1".equals(vo.getSex()) ? "先生" : "女士";
vo.setSendContent(msgSign + vo.getWorkerName()+sex+"您好,"+vo.getSendContent());
}
msgResult = messageSendUtil.sendStyleMessage(userList);
}
String[] split = msgResult.split(",");
if (split.length == 2 && "ok".equals(split[0])) {
// 批量发送成功更新任务状态为成功

View File

@ -45,9 +45,6 @@ public class MessageSendUtil {
ERROR_CODE_TO_MESSAGE.put("-1003", "用户通道异常");
}
/**
* 短信发送
*/
/**
* 短信发送
*/
@ -57,7 +54,6 @@ public class MessageSendUtil {
String mobiles = userList.stream()
.map(WorkerVo::getPhone)
.collect(Collectors.joining(","));
content = "【博诺思】"+content;
// 构建请求URL
String url = API_URL + msgUrl + "?ddtkey=" + DDTKEY + "&secretkey=" + SECRET_KEY;
String uri = url + "&mobile=" + mobiles + "&content=" + content;
@ -69,6 +65,29 @@ public class MessageSendUtil {
return convertToMessage(HttpRequestHelper.doPost(uri, param));
}
/**
* 变量短信发送
*/
public String sendStyleMessage(List<WorkerVo> userList) {
String msgUrl = "/styleSms_token";
// 构建手机号字符串用逗号分隔
String mobiles = userList.stream()
.map(WorkerVo::getPhone)
.collect(Collectors.joining(","));
String contents = userList.stream()
.map(WorkerVo::getSendContent)
.collect(Collectors.joining(""));
// 构建请求URL
String url = API_URL + msgUrl + "?ddtkey=" + DDTKEY + "&secretkey=" + SECRET_KEY;
String uri = url + "&mobile=" + mobiles + "&content=" + contents;
// 构建JSON参数
String param = String.format(
"{\"ddtkey\":\"%s\",\"secretkey\":\"%s\",\"mobile\":\"%s\",\"content\":\"%s\"}",
DDTKEY, SECRET_KEY, mobiles, contents
);
return convertToMessage(HttpRequestHelper.doPost(uri, param));
}
/**
* 将短信平台返回的错误码转换为中文说明
*

View File

@ -94,4 +94,6 @@ public class WorkerVo {
private String sendStatus;
private String reason;
private String submitTime;
}

View File

@ -128,3 +128,6 @@ xss:
excludes: /system/notice
# 匹配链接
urlPatterns: /system/*,/monitor/*,/tool/*
message:
signature: 【博诺思】

View File

@ -80,6 +80,7 @@
pt.send_content,
w.worker_name,
w.phone,
w.sex,
pt.id AS taskId,
w.id
FROM pm_task pt, pm_worker w