封装数据发送(九公司消息发送)
parent
80f4b403f3
commit
1a26abd93a
|
@ -47,7 +47,7 @@ public class MessagesController {
|
|||
if (body == null || !body.containsKey("key") || !body.containsKey("value")) {
|
||||
throw new RuntimeException("参数为空");
|
||||
}
|
||||
PushRecords pushRecords = JSON.parseObject(decrypt(body, privateKey), PushRecords.class);
|
||||
PushRecords pushRecords = JSON.parseObject(decrypt(body), PushRecords.class);
|
||||
Set<ConstraintViolation<PushRecords>> validate = validator.validate(pushRecords);
|
||||
if (!validate.isEmpty()) {
|
||||
throw new ConstraintViolationException(validate);
|
||||
|
@ -62,9 +62,9 @@ public class MessagesController {
|
|||
return result;
|
||||
}
|
||||
|
||||
private String decrypt(Map<String, Object> body, String privateKey) {
|
||||
private String decrypt(Map<String, Object> body) {
|
||||
try {
|
||||
RSA rsa = new RSA(privateKey, null);
|
||||
RSA rsa = new RSA(MessagesController.privateKey, null);
|
||||
byte[] aesKey = rsa.decrypt(Convert.toStr(body.get("key")), KeyType.PrivateKey);
|
||||
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, aesKey);
|
||||
return aes.decryptStr(Convert.toStr(body.get("value")), StandardCharsets.UTF_8);
|
||||
|
|
|
@ -32,4 +32,6 @@ public class PushRecords implements Serializable {
|
|||
|
||||
@NotBlank(message = "发送时间不能为空")
|
||||
private String sendTime;
|
||||
|
||||
private Integer state;
|
||||
}
|
||||
|
|
|
@ -7,4 +7,6 @@ public interface MessagesMapper {
|
|||
void install(PushRecords pushRecords);
|
||||
|
||||
String findUserIdByPhone(@Param("phone") String phone);
|
||||
|
||||
void updateById(PushRecords pushRecords);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.zcloud.service.messages;
|
||||
|
||||
import com.zcloud.entity.messages.PushRecords;
|
||||
|
||||
/**
|
||||
* 发送消息接口
|
||||
*/
|
||||
public interface PushMessages {
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param pushRecords 消息实体
|
||||
* @param userId 用户id
|
||||
*/
|
||||
void push(PushRecords pushRecords, String userId) throws Exception;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.zcloud.service.messages.impl;
|
||||
|
||||
import com.zcloud.entity.messages.PushRecords;
|
||||
import com.zcloud.service.messages.PushMessages;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 发送站内信
|
||||
*/
|
||||
@Component("messages3")
|
||||
@AllArgsConstructor
|
||||
public class AllMessagesImpl implements PushMessages {
|
||||
|
||||
private final InsideMessagesImpl insideMessages;
|
||||
private final SmsMessagesImpl smsMessages;
|
||||
|
||||
@Override
|
||||
public void push(PushRecords pushRecords, String userId) throws Exception {
|
||||
insideMessages.push(pushRecords, userId);
|
||||
smsMessages.push(pushRecords, userId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.zcloud.service.messages.impl;
|
||||
|
||||
import com.zcloud.entity.PageData;
|
||||
import com.zcloud.entity.messages.PushRecords;
|
||||
import com.zcloud.service.bus.NoticeCorpService;
|
||||
import com.zcloud.service.messages.PushMessages;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 发送站内信
|
||||
*/
|
||||
@Component("messages2")
|
||||
@AllArgsConstructor
|
||||
public class InsideMessagesImpl implements PushMessages {
|
||||
|
||||
private final NoticeCorpService noticeCorpService;
|
||||
|
||||
@Override
|
||||
public void push(PushRecords pushRecords, String userId) throws Exception {
|
||||
PageData mes = new PageData();
|
||||
mes.put("BIANMA", "PUSH_RECORDS");
|
||||
mes.put("SENDER_ID", "九公司应急管理");
|
||||
mes.put("SENDER_NAME", "九公司应急管理");
|
||||
mes.put("SYNOPSIS", pushRecords.getMessageTitle());
|
||||
mes.put("CORPINFO_ID", "48a8ca9815814c979814ddcf041c5cd5");
|
||||
mes.put("RECEIVER_ID", userId);
|
||||
PageData content = new PageData();
|
||||
content.put("msg", pushRecords.getMessageContent());
|
||||
mes.put("CONTENT", content);// 站内信内容
|
||||
noticeCorpService.sendNotice(mes);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
package com.zcloud.service.messages.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zcloud.entity.PageData;
|
||||
import com.zcloud.entity.messages.PushRecords;
|
||||
import com.zcloud.mapper.datasource.messages.MessagesMapper;
|
||||
import com.zcloud.service.bus.NoticeCorpService;
|
||||
import com.zcloud.service.messages.MessagesService;
|
||||
import com.zcloud.service.messages.PushMessages;
|
||||
import com.zcloud.util.DateUtil;
|
||||
import com.zcloud.util.UuidUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
@ -13,13 +12,16 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MessagesServiceImpl implements MessagesService {
|
||||
|
||||
private final MessagesMapper mapper;
|
||||
private final NoticeCorpService noticeCorpService;
|
||||
private final Map<String, PushMessages> messages;
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
@ -27,59 +29,19 @@ public class MessagesServiceImpl implements MessagesService {
|
|||
// 先记录数据
|
||||
pushRecords.setId(UuidUtil.get32UUID());
|
||||
pushRecords.setSendTime(DateUtil.getTime());
|
||||
pushRecords.setState(1);
|
||||
mapper.install(pushRecords);
|
||||
// 根据手机号查找用户id
|
||||
String userId = mapper.findUserIdByPhone(pushRecords.getPhone());
|
||||
if (StrUtil.isEmpty(userId)) {
|
||||
throw new RuntimeException("该用户不存在");
|
||||
}
|
||||
// 调用对应消息推送实现
|
||||
Integer type = pushRecords.getMessageType();
|
||||
// 1-短信 2-平台信息 3-全发
|
||||
if (type == 1) {
|
||||
pushSms(pushRecords);
|
||||
} else if (type == 2) {
|
||||
PageData data = pushInfo(pushRecords, userId);
|
||||
noticeCorpService.sendNotice(data);
|
||||
} else if (type == 3) {
|
||||
pushSms(pushRecords);
|
||||
PageData data = pushInfo(pushRecords, userId);
|
||||
noticeCorpService.sendNotice(data);
|
||||
} else {
|
||||
throw new RuntimeException("消息类型:【" + type + "】错误");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送平台信息(站内信)
|
||||
* 注意 不能再此方法中调用数据库 再类中直接调用 会导致事务失效 private 也会导致事务失效
|
||||
*
|
||||
* @param pushRecords 消息
|
||||
* @param userId 接收人id
|
||||
*/
|
||||
private PageData pushInfo(PushRecords pushRecords, String userId) throws Exception {
|
||||
PageData mes = new PageData();
|
||||
mes.put("BIANMA", "PUSH_RECORDS");
|
||||
mes.put("SENDER_ID", "九公司应急管理"); // 发送人员ID
|
||||
mes.put("SENDER_NAME", "九公司应急管理"); // 发送人员姓名
|
||||
mes.put("SYNOPSIS", pushRecords.getMessageTitle()); // 站内信标题
|
||||
//mes.put("WORKURL", "/pages/application/high-risk-work/limited-space/gas-analysis/list?NameLikes=" + pd.getString("WORK_PERMIT_NUMBER")); // 操作链接
|
||||
mes.put("CORPINFO_ID", "48a8ca9815814c979814ddcf041c5cd5");// 企业id
|
||||
mes.put("RECEIVER_ID", userId); // 接收人员ID
|
||||
PageData content = new PageData();
|
||||
content.put("msg", pushRecords.getMessageContent());// 作业编号
|
||||
mes.put("CONTENT", content);// 站内信内容
|
||||
return mes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送平台信息(站内信)
|
||||
* 注意 不能再此方法中调用数据库 再类中直接调用 会导致事务失效 private 也会导致事务失效
|
||||
*
|
||||
* @param pushRecords 消息
|
||||
*/
|
||||
private void pushSms(PushRecords pushRecords) {
|
||||
// todo
|
||||
log.error("===================>推送短信信息");
|
||||
log.error("消息内容:{}", pushRecords);
|
||||
PushMessages pushMessages = messages.get("messages" + type);
|
||||
pushMessages.push(pushRecords, userId);
|
||||
// 修改状态
|
||||
pushRecords.setState(2);
|
||||
mapper.updateById(pushRecords);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.zcloud.service.messages.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.zcloud.entity.messages.PushRecords;
|
||||
import com.zcloud.service.messages.PushMessages;
|
||||
import com.zcloud.util.SendSmsUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*/
|
||||
@Component("messages1")
|
||||
@AllArgsConstructor
|
||||
public class SmsMessagesImpl implements PushMessages {
|
||||
|
||||
@Override
|
||||
public void push(PushRecords pushRecords, String userId) throws ParseException {
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("messageContent", pushRecords.getMessageContent());
|
||||
// todo 应该是短信模版tpId 没有 别的OK了
|
||||
if (!SendSmsUtil.sendSms(null, object, pushRecords.getSendTime(), pushRecords.getPhone())) {
|
||||
throw new RuntimeException("系统故障:短信发送失败");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,12 @@
|
|||
package com.zcloud.util;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
|
@ -14,6 +18,7 @@ import java.util.Date;
|
|||
/**
|
||||
* 重点工程发送短信工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class SendSmsUtil {
|
||||
|
||||
private static String USERNAME = "qhdzyhy";
|
||||
|
@ -27,43 +32,44 @@ public class SendSmsUtil {
|
|||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param records
|
||||
* @param time 发送时间 为空或小于当前时间则立即发送
|
||||
* records格式实例 records是 JSONArray 里边的内容通过调用下方方法getRecords获取 JSONObject 定义一个JSONArray后 put进去即可
|
||||
* "records":[
|
||||
* {
|
||||
* "mobile":"138****0000",
|
||||
* "tpContent":{
|
||||
* "var1":"变量1",
|
||||
* "var2":"变量2"
|
||||
* }
|
||||
* },
|
||||
* {
|
||||
* "mobile":"138****0001",
|
||||
* "tpContent":{
|
||||
* "var1":"变量2",
|
||||
* "var2":"变量2"
|
||||
* }
|
||||
* }
|
||||
* ]
|
||||
* @param time 发送时间 为空或小于当前时间则立即发送
|
||||
* records格式实例 records是 JSONArray 里边的内容通过调用下方方法getRecords获取 JSONObject 定义一个JSONArray后 put进去即可
|
||||
* "records":[
|
||||
* {
|
||||
* "mobile":"138****0000",
|
||||
* "tpContent":{
|
||||
* "var1":"变量1",
|
||||
* "var2":"变量2"
|
||||
* }
|
||||
* },
|
||||
* {
|
||||
* "mobile":"138****0001",
|
||||
* "tpContent":{
|
||||
* "var1":"变量2",
|
||||
* "var2":"变量2"
|
||||
* }
|
||||
* }
|
||||
* ]
|
||||
*/
|
||||
public static void sendSms(String tpId,JSONArray records ,String time) throws ParseException {
|
||||
public static void sendSms(String tpId, JSONArray records, String time) throws ParseException {
|
||||
JSONObject json = new JSONObject();
|
||||
Long tKey = System.currentTimeMillis()/1000;
|
||||
String passWord = MD5.md5(PASSWORD+tKey);
|
||||
Long tKey = System.currentTimeMillis() / 1000;
|
||||
String passWord = MD5.md5(PASSWORD + tKey);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
//扔参数
|
||||
json.put("username",USERNAME);
|
||||
json.put("password",passWord);
|
||||
json.put("tKey",tKey);
|
||||
json.put("signature",SIGNATURE);
|
||||
json.put("tpId",tpId);
|
||||
if(StringUtils.isNotBlank(time)){
|
||||
if(sdf.parse(time).after(new Date())){
|
||||
json.put("time",time);
|
||||
json.put("username", USERNAME);
|
||||
json.put("password", passWord);
|
||||
json.put("tKey", tKey);
|
||||
json.put("signature", SIGNATURE);
|
||||
json.put("tpId", tpId);
|
||||
if (StringUtils.isNotBlank(time)) {
|
||||
if (sdf.parse(time).after(new Date())) {
|
||||
json.put("time", time);
|
||||
}
|
||||
}
|
||||
json.put("records",records);
|
||||
json.put("records", records);
|
||||
System.out.println(json.toJSONString());
|
||||
String result = HttpRequest.post(URL)
|
||||
.timeout(60000)
|
||||
|
@ -71,45 +77,64 @@ public class SendSmsUtil {
|
|||
System.out.println(result);
|
||||
}
|
||||
|
||||
// {"msg":"template error","tpId":"null","code":4014,"msgId":"172950016144709288961"}
|
||||
// {"msg":"success","tpId":"121487","code":200,"msgId":"172950025828957168641","invalidList":[]}
|
||||
public static boolean sendSms(String tpId, JSONObject records, String time, String phone) throws ParseException {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("username", USERNAME);
|
||||
json.put("tKey", System.currentTimeMillis() / 1000);
|
||||
json.put("password", MD5.md5(PASSWORD + json.getLong("tKey")));
|
||||
json.put("signature", SIGNATURE);
|
||||
json.put("tpId", tpId);
|
||||
if (StringUtils.isNotBlank(time) && DateUtil.parseDateTime(time).after(new Date())) {
|
||||
json.put("time", time);
|
||||
}
|
||||
JSONArray array = new JSONArray();
|
||||
array.add(getRecords(phone, records));
|
||||
json.put("records", array);
|
||||
HttpRequest request = HttpRequest.post(URL)
|
||||
.timeout(60000)
|
||||
.body(json.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
try (HttpResponse response = request.execute()) {
|
||||
return JSON.parseObject(response.body()).getInteger("code") == 200;
|
||||
} catch (Exception e) {
|
||||
log.error("发送短信异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 封装要发送的电话以及要替换的内容
|
||||
* @param mobile 手机号
|
||||
*
|
||||
* @param mobile 手机号
|
||||
* @param tpContent 替换内容
|
||||
* @return
|
||||
* 格式实例 var1表示要替换的内容 与要替换的内容保持一致即可 变量1表示替换的内容
|
||||
* {
|
||||
* "mobile":"138****0000",
|
||||
* "tpContent":{
|
||||
* "var1":"变量1",
|
||||
* "var2":"变量2"
|
||||
* }
|
||||
* }
|
||||
* @return 格式实例 var1表示要替换的内容 与要替换的内容保持一致即可 变量1表示替换的内容
|
||||
* {
|
||||
* "mobile":"138****0000",
|
||||
* "tpContent":{
|
||||
* "var1":"变量1",
|
||||
* "var2":"变量2"
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public static JSONObject getRecords(String mobile, JSONObject tpContent ){
|
||||
public static JSONObject getRecords(String mobile, JSONObject tpContent) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("mobile",mobile);
|
||||
if(ObjectUtils.hashCode(tpContent)!=0){
|
||||
json.put("tpContent",tpContent);
|
||||
json.put("mobile", mobile);
|
||||
if (ObjectUtils.hashCode(tpContent) != 0) {
|
||||
json.put("tpContent", tpContent);
|
||||
}
|
||||
return json;
|
||||
|
||||
}//d22f8a5d4a35b3761dc9525186e652a0
|
||||
|
||||
//测试接口
|
||||
public static void main(String[] args) throws ParseException {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
|
||||
//接受返回结果
|
||||
JSONObject records = new JSONObject();
|
||||
//定义替换参数
|
||||
//JSONObject tpContent = new JSONObject();
|
||||
//tpContent.put("userName","卓云企业");
|
||||
//tpContent.put("time","2023-09-21 15:56:20");
|
||||
//records = getRecords("18617456701",tpContent);
|
||||
jsonArray.add(records);
|
||||
|
||||
sendSms("null",jsonArray,null);
|
||||
}
|
||||
// public static void main(String[] args) throws ParseException {
|
||||
// //定义替换参数
|
||||
// JSONObject tpContent = new JSONObject();
|
||||
// tpContent.put("HIDDENDESCR", "卓云企业测试");
|
||||
// JSONObject object = sendSms("121487", tpContent, DateUtil.formatDateTime(new Date()), "18630387571");
|
||||
// System.out.println(object.toJSONString());
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,9 @@
|
|||
<if test="sendTime != null and sendTime != ''">
|
||||
SEND_TIME,
|
||||
</if>
|
||||
<if test="state != null">
|
||||
STATE,
|
||||
</if>
|
||||
</trim>
|
||||
values
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
|
@ -44,10 +47,38 @@
|
|||
<if test="sendTime != null and sendTime != ''">
|
||||
#{sendTime},
|
||||
</if>
|
||||
<if test="state != null">
|
||||
#{state},
|
||||
</if>
|
||||
</trim>
|
||||
</select>
|
||||
|
||||
<select id="findUserIdByPhone" resultType="java.lang.String">
|
||||
select USER_ID from sys_user where USERNAME = #{phone} and ISDELETE = 0 limit 1
|
||||
</select>
|
||||
|
||||
<update id="updateById">
|
||||
update push_records
|
||||
<set>
|
||||
<if test="phone != null and phone != ''">
|
||||
PHONE = #{phone},
|
||||
</if>
|
||||
<if test="messageTitle != null and messageTitle != ''">
|
||||
MESSAGE_TITLE = #{messageTitle},
|
||||
</if>
|
||||
<if test="messageContent != null and messageContent != ''">
|
||||
MESSAGE_CONTENT = #{messageContent},
|
||||
</if>
|
||||
<if test="messageType != null">
|
||||
MESSAGE_TYPE = #{messageType},
|
||||
</if>
|
||||
<if test="sendTime != null and sendTime != ''">
|
||||
SEND_TIME = #{sendTime},
|
||||
</if>
|
||||
<if test="state != null">
|
||||
STATE = #{state},
|
||||
</if>
|
||||
</set>
|
||||
where ID = #{id}
|
||||
</update>
|
||||
</mapper>
|
Loading…
Reference in New Issue