短信对接

This commit is contained in:
2024-12-27 17:07:53 +08:00
parent 841037f022
commit cff9a2fda4
6 changed files with 105 additions and 77 deletions
+81 -57
View File
@@ -31,79 +31,103 @@ import java.util.List;
@Slf4j
public class MsgApi {
/**
* token -redis - name
*/
private static final String msgTokenName = "msg_token";
private static final String APPID = "1197951921244139520";
private static final String ACCESS_TOKEN = "54743e1fb7786457edf04cc7274d98d5";
private static final String SCHOOL_CODE = "10295";
private static final String APP_KEY = "10c51794-45b8-4de5-ba6f-b1ed843c20f3";
private static final String APP_SECRET = "4acbf17e-4b17-4c47-b4bb-1576a066250a";
/**
* 发送消息api
*/
private static final String MSG_API = "https://gateway.jiangnan.edu.cn/mp_message_pocket_web-mp-restful-message-send/ProxyService/message_pocket_web-mp-restful-message-sendProxyService";
private static final String MSG_API = "https://xxdb.hmc.edu.cn/msgInfo/pushMsgInfo";
private static final String TOKEN_API = "https://xxdb.hmc.edu.cn/accessSystem/getAccessToken";
@Inject
private RedisService redisService;
/**
* 消息发送,url可不必填写
*
* @param content (必填)消息内容
* @param urlDesc Url链接的描述
* @param pcUrl pc端点击消息时的链接url
* @param mobileUrl 手机端点击消息时的链接url
* @param sendType (必填)0.PC门户通知和移动校园同时发送(通常为此种方式) 1.只发送PC门户 2.只发送移动校园 3.邮件 4.短信 5.微信企业号 6.钉钉企业内部应用工作通知 7.微信服务号 8.Welink
* @param receivers (必填)收件人集合 包含三个字段(userId为必填,其余两个字段根据sengType填写): userId:收件人的userId(职工号或学号)、mobile:手机号、email:邮箱地址
* @param channels 推送渠道 (INNER:站内消息; SMS:短信; Mail:邮件; WeChat:微信; DingTalk:钉钉;同时推送多个渠道用英文逗号分隔 )
* @param loginNames
* @param mtype 消息类型; 0: 图文 1: 文字 2: 外链
* @param title 标题
* @param content 内容
* @param imageUrl 图片URL
* @author zhf
* @description
*/
public void sendMsg(String content, String urlDesc, String pcUrl, String mobileUrl, String sendType, List<NutMap> receivers) {
public void sendMsg(List<String> channels, String loginNames, Integer mtype, String title, String content, String imageUrl,String link) {
try {
// if (!Globals.MyConfig.getBoolean("SendMsg")) {
// log.info("发送失败,消息暂未开启!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// return;
// }
if (!Globals.MyConfig.getBoolean("SendMsg")) {
log.info("发送失败,消息暂未开启!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return;
}
// if (Globals.isEnv(Env.dev)) {
// log.info("开发模式不允许发送短信!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// return;
// }
if (Globals.isEnv(Env.dev)) {
log.info("开发模式不允许发送短信!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return;
}
// if (StrUtil.isBlank(content) || StrUtil.isBlank(sendType) || Lang.isEmpty(receivers)) {
// log.info("发送失败,缺少需要参数请检查!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// return;
// }
// NutMap nutMap = receivers.stream().findFirst().orElse(null);
// assert nutMap != null;
// //获取签名
// String sign = SecureUtil.md5(ACCESS_TOKEN + SCHOOL_CODE + nutMap.getString("userId"));
//
// NutMap dataMap = new NutMap();
// dataMap.setv("schoolCode", SCHOOL_CODE);
// dataMap.setv("sign", sign);
// dataMap.setv("content", content);
// dataMap.setv("sendType", sendType);
// dataMap.setv("receiverType", 1);
// dataMap.setv("urlDesc", Strings.isNotBlank(urlDesc) ? urlDesc : "");
// dataMap.setv("pcUrl", Strings.isNotBlank(pcUrl) ? pcUrl : "");
// dataMap.setv("mobileUrl", Strings.isNotBlank(mobileUrl) ? mobileUrl : "");
// dataMap.setv("receivers", receivers);
//
// log.info(Json.toJson(dataMap));
//
// HttpRequest request = HttpUtil.createPost(MSG_API)
// .header("appId", APPID)
// .header("accessToken", ACCESS_TOKEN)
// .body(Json.toJson(dataMap));
//
// JSONObject jsonObject = JSON.parseObject(request.execute().body());
// int status = jsonObject.getInteger("status");
//
// if (status == 200) {
// log.info("发送成功>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// } else {
// log.info("发送失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// throw new RuntimeException("Message sending failed with status code: " + status + ", Message: " + jsonObject.getString("msg"));
// }
if (StrUtil.isBlank(title)||StrUtil.isBlank(content) ||Lang.isEmpty(channels) || Lang.isEmpty(loginNames)) {
log.info("发送失败,缺少需要参数请检查!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return;
}
String msgToken = getMsgToken();
NutMap map = new NutMap();
map.put("channel", String.join(",", channels));
//推送范围(1:普通模式;2:全体教师;3:全体学生;4:全体人员)
map.put("objScope", 1);
map.put("objIds", loginNames);
map.put("mtype", mtype);
map.put("sendStatus", 1);
map.put("title", title);
map.put("content", content);
map.put("imageUrl", imageUrl);
map.put("link", link);
/*HttpRequest request = HttpUtil.createPost(MSG_API)
.header("token", msgToken)
.body(Json.toJson(map));
JSONObject jsonObject = JSON.parseObject(request.execute().body());
int status = jsonObject.getInteger("code");
if (status == 200) {
log.info("发送成功>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
} else {
log.info("发送失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
throw new RuntimeException("Message sending failed with status code: " + status + ", Message: " + jsonObject.getString("message"));
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取token
*
* @return
*/
private String getMsgToken() {
String token = redisService.get(msgTokenName);
if (StrUtil.isBlank(token)) {
String body = HttpRequest.post(TOKEN_API + "?appKey=" + APP_KEY + "&appSecret=" + APP_SECRET).execute().body();
NutMap map = Json.fromJson(NutMap.class, body);
if (map.getInt("code") == 200) {
NutMap data = Json.fromJson(NutMap.class, map.getString("data"));
token = data.getString("accessToken");
redisService.setex(msgTokenName, 3600, token);
} else {
token = "";
}
}
return token;
}
}
@@ -169,9 +169,9 @@ public class LxyHdTjController {
List<Lxy_bm> grxxes = lxyGrxxService.dao().query(Lxy_bm.class, Cnd.where("hdid", "=", hdid).and("bmzt", "=", 5));
List<String> loginnames = grxxes.stream().map(Lxy_bm::getLoginname).collect(Collectors.toList());
loginnames.forEach(loginname -> {
msgApi.sendMsg(content, "", "", "", "4", new ArrayList<>() {{
/* msgApi.sendMsg(content, "", "", "", "4", new ArrayList<>() {{
add(NutMap.NEW().addv("userId", loginname));
}});
}});*/
});
return null;
}
@@ -125,19 +125,19 @@ public class MsgNotifyServiceImpl extends ViServiceImpl<MsgNotify> implements Ms
@Override
public void run() {
if (sendTypes.contains("0")) {
msgApi.sendMsg(content, null, null, null, "0", receivers);
// msgApi.sendMsg(content, null, null, null, "0", receivers);
}
if (sendTypes.contains("1")) {
msgApi.sendMsg(content, null, null, null, "1", receivers);
// msgApi.sendMsg(content, null, null, null, "1", receivers);
}
if (sendTypes.contains("2")) {
msgApi.sendMsg(content, null, null, null, "2", receivers);
// msgApi.sendMsg(content, null, null, null, "2", receivers);
}
if (sendTypes.contains("4")) {
msgApi.sendMsg(content, null, null, null, "4", receivers);
// msgApi.sendMsg(content, null, null, null, "4", receivers);
}
if (sendTypes.contains("5")) {
msgApi.sendMsg(content, null, null, null, "5", receivers);
// msgApi.sendMsg(content, null, null, null, "5", receivers);
}
}
}.start();
@@ -10,6 +10,7 @@ import io.v.nutz.base.dao.CndPlus;
import io.v.nutz.base.query.PageForm;
import io.v.nutz.sys.models.Sys_home_activity;
import io.v.nutz.task.services.TaskPlatformService;
import io.v.nutz.web.commons.base.Globals;
import io.v.nutz.zhgh.data.model.MatchConditionStructure;
import io.v.nutz.zhgh.data.service.MatchConditionStructureService;
import io.v.nutz.zhgh.mobileHome.service.MobileHomeService;
@@ -42,10 +43,7 @@ import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.trans.Trans;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -263,7 +261,7 @@ public class WelfareProjectMangeController {
projectService.dao().delete(MatchConditionStructure.class, project.getConditionStructureId());
}
projectService.dao().clear(WelfareProjectSubject.class, Cnd.where("projectId", "=", id));
if(ObjectUtil.isNotEmpty(welfareProjectSubject)){
if (ObjectUtil.isNotEmpty(welfareProjectSubject)) {
projectService.dao().clear(WelfareProjectSubjectOption.class, Cnd.where("subjectId", "=", welfareProjectSubject.getId()));
}
projectService.dao().clear(WelfareUserSelection.class, Cnd.where("welfareId", "=", id));
@@ -301,7 +299,7 @@ public class WelfareProjectMangeController {
public Object sendMsgToNotWelfareUsers(String projectId, String sendMsgValue) {
Sql sql = Sqls.create("""
SELECT
u.loginname as userId
u.loginname
FROM
welfare_list wl
left join sys_user u on u.id = wl.userId
@@ -318,7 +316,13 @@ public class WelfareProjectMangeController {
""");
sql.setParam("projectId", projectId);
List<NutMap> userList = listService.listMap(sql);
msgApi.sendMsg(sendMsgValue, "福利选择链接", "", "https://zhgh.jiangnan.edu.cn/mobile/welfare/list/receive?projectId=4fa233704ddb41bc8e34e966efeda0a4", "4", userList);
Set<String> loginNameList = userList.stream().map(user -> user.getString("loginname")).collect(Collectors.toSet());
String loginNames = loginNameList.stream().collect(Collectors.joining(","));
WelfareProject project = dao.fetch(WelfareProject.class, projectId);
String link = Globals.AppDomain + "/mobile/welfare/list/receive?projectId=%s".formatted(projectId);
String imageUrl = Globals.AppDomain + "/file_server/fileStreamPreview?id=" + project.getCover();
msgApi.sendMsg(List.of("DingTalk"), loginNames, 0, "职工福利", sendMsgValue, imageUrl, link);
return null;
}
@@ -464,9 +464,9 @@ public class RxdjShController {
sql.setParam("year", year);
List<Record> list = baseService.list(sql);
for (Record r : list) {
msgApi.sendMsg(content, "", "", "", "4", new ArrayList<>() {{
/* msgApi.sendMsg(content, "", "", "", "4", new ArrayList<>() {{
add(NutMap.NEW().addv("userId", r.getString("loginname")));
}});
}});*/
}
return null;
}