commit
This commit is contained in:
@@ -20,103 +20,112 @@ import org.nutz.lang.util.NutMap;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zxy
|
* @author zxy
|
||||||
* @Description 消息API
|
* @Description 消息API
|
||||||
* @createTime 2022年01月27日 11:20:00
|
* @createTime 2022年01月27日 11:20:00
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@IocBean
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@IocBean
|
||||||
public class MsgApi {
|
public class MsgApi {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param msgTitle 消息标题
|
|
||||||
* @param msgContent 消息内容
|
|
||||||
* @param msgType 消息类型(0:图文,1:文字,2:外链)
|
|
||||||
* @param sendStatus 发送状态,1:实时发送 2:定时消息
|
|
||||||
* @param sendChannel 发送渠道Mail:邮件,WeChat:微信, IntelligenceMode:智能渠道 多个渠道用英文逗号隔开(如果选择智能模式则不可再选其他模式)
|
|
||||||
* @param receivers 接收人,Json格式(当sendMode为普通模式normal 时必填) 接收人格式:type为接收人类型,默认为User; usreId可以填写工号或手机号;name为接收人姓名
|
|
||||||
* @param waitLink 页面的跳转地址,当消息是待办消息时(必填)。
|
|
||||||
* @author hpf
|
|
||||||
* @description
|
|
||||||
*/
|
|
||||||
public void sendMsg(String msgTitle, String msgContent, Integer msgType, Integer sendStatus, String sendChannel, List<msgUser> receivers, String waitLink) {
|
|
||||||
try {
|
|
||||||
if (!Globals.MyConfig.getBoolean("SendMsg")) {
|
|
||||||
log.info("发送失败,消息暂未开启!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Globals.isEnv(Env.dev)) {
|
|
||||||
log.info("开发模式不允许发送短信!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StrUtil.isBlank(msgTitle)||StrUtil.isBlank(msgContent) ||StrUtil.isEmpty(sendChannel)||receivers.size()==0) {
|
|
||||||
log.info("发送失败,缺少需要参数请检查!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String msgToken = getMsgToken();
|
|
||||||
NutMap map = new NutMap();
|
|
||||||
|
|
||||||
List<Map<String, String>> list = List.of(Map.of("type", "User", "name", "工会测试", "usreId", "LSGH0001"));
|
|
||||||
|
|
||||||
map.put("token", msgToken);
|
|
||||||
map.put("msgtitle", msgTitle);
|
|
||||||
map.put("msgContent", msgContent);
|
|
||||||
map.put("msgType", msgType);
|
|
||||||
map.put("sendStatus", sendStatus);
|
|
||||||
map.put("sendChannel", sendChannel);
|
|
||||||
map.put("sendMode", "normal");
|
|
||||||
map.put("receivers", Json.toJson(list));
|
|
||||||
map.put("waitLink", waitLink);
|
|
||||||
//发送
|
|
||||||
map.put("sendMsgType", 1);
|
|
||||||
|
|
||||||
Header header = Header.create();
|
|
||||||
org.nutz.http.Response response = Http.post3(MSG_API, Json.toJson(map), header, 200000);
|
|
||||||
System.out.println(response);
|
|
||||||
|
|
||||||
HttpRequest request = HttpUtil.createPost(MSG_API)
|
|
||||||
.body(Json.toJson(map));
|
|
||||||
JSONObject jsonObject = JSON.parseObject(request.execute().body());
|
|
||||||
int status = jsonObject.getInteger("rspCode");
|
|
||||||
|
|
||||||
if (status == 000000) {
|
|
||||||
log.info("发送成功>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
|
|
||||||
} else {
|
|
||||||
log.info("发送失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
|
|
||||||
throw new RuntimeException("Message sending failed with status code: " + status + ", Message: " + jsonObject.getString("rspMsg"));
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static final String TOKEN_API = "https://mc.zjitc.edu.cn/msg/getThirdAPIToken";
|
private static final String TOKEN_API = "https://mc.zjitc.edu.cn/msg/getThirdAPIToken";
|
||||||
private static final String APP_Id = "MSG10029935";
|
private static final String APP_Id = "MSG10029935";
|
||||||
private static final String APP_PASSWORD = "N2NkOTI3NDVmZjY0ZTkwYTI0OGVjYjQzYjZhNDIyMzE";
|
private static final String APP_PASSWORD = "N2NkOTI3NDVmZjY0ZTkwYTI0OGVjYjQzYjZhNDIyMzE";
|
||||||
private static final String MSG_TOKEN_NAME = "msgToken";
|
private static final String MSG_TOKEN_NAME = "msgToken";
|
||||||
private static final String MSG_API = "https://mc.zjitc.edu.cn/message/sendMessageApi";
|
private static final String MSG_API = "https://mc.zjitc.edu.cn/message/sendMessageApi";
|
||||||
|
private static final String SMS_API = "https://mc.zjitc.edu.cn/message/sendMessageBySMSApi";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送短信(单个用户)
|
||||||
|
*/
|
||||||
|
public void sendSMSMsg(String msgContent, String userName, String mobile) {
|
||||||
|
sendSMSMsg(msgContent, List.of(Map.of("name", userName, "mobile", mobile)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送短信(批量用户)
|
||||||
|
*/
|
||||||
|
public void sendSMSMsg(String msgContent, List<Map<String, String>> receivers) {
|
||||||
|
NutMap paramsMap = new NutMap();
|
||||||
|
paramsMap.put("token", getMsgToken());
|
||||||
|
paramsMap.put("msgContent", msgContent);
|
||||||
|
paramsMap.put("receivers", Json.toJson(receivers));
|
||||||
|
sendMessage(SMS_API, paramsMap, "1", "短信");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送微信消息给单个用户
|
||||||
|
*/
|
||||||
|
public void sendTextMsg(String msgTitle, String msgContent, String loginName, String userName) {
|
||||||
|
List<Map<String, String>> receivers = List.of(Map.of("type", "User", "name", userName, "usreId", loginName));
|
||||||
|
sendTextMsg(msgTitle, msgContent, receivers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送微信消息给多个用户
|
||||||
|
*/
|
||||||
|
public void sendTextMsg(String msgTitle, String msgContent, List<Map<String, String>> receivers) {
|
||||||
|
NutMap paramsMap = new NutMap();
|
||||||
|
paramsMap.put("token", getMsgToken());
|
||||||
|
paramsMap.put("msgtitle", msgTitle);
|
||||||
|
paramsMap.put("msgContent", msgContent);
|
||||||
|
paramsMap.put("msgType", 1);
|
||||||
|
paramsMap.put("sendStatus", 1);
|
||||||
|
paramsMap.put("sendChannel", "WeChat");
|
||||||
|
paramsMap.put("sendMode", "normal");
|
||||||
|
paramsMap.put("receivers", Json.toJson(receivers));
|
||||||
|
paramsMap.put("sendMsgType", 1);
|
||||||
|
sendMessage(MSG_API, paramsMap, "000000", "微信消息");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(String apiUrl, NutMap paramsMap, String successCode, String msgTypeDesc) {
|
||||||
|
try {
|
||||||
|
JSONObject response = sendRequest(apiUrl, paramsMap);
|
||||||
|
String rspCode = response.getString("rspCode");
|
||||||
|
if (Objects.equals(rspCode, successCode)) {
|
||||||
|
log.info("{} 发送成功", msgTypeDesc);
|
||||||
|
} else {
|
||||||
|
String errorMsg = String.format("%s 发送失败,错误码:%s,错误信息:%s",
|
||||||
|
msgTypeDesc,
|
||||||
|
rspCode,
|
||||||
|
response.getString("rspMsg"));
|
||||||
|
log.error(errorMsg);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("发送 {} 异常", msgTypeDesc, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JSONObject sendRequest(String apiUrl, NutMap paramsMap) {
|
||||||
|
if (!Globals.MyConfig.getBoolean("SendMsg")) {
|
||||||
|
log.warn("消息未启用,跳过发送");
|
||||||
|
}
|
||||||
|
if (Globals.isEnv(Env.dev)) {
|
||||||
|
log.warn("开发环境禁止发送真实消息");
|
||||||
|
}
|
||||||
|
String jsonBody = Json.toJson(paramsMap);
|
||||||
|
log.debug("请求体: {}", jsonBody);
|
||||||
|
HttpRequest request = HttpUtil.createPost(apiUrl).body(jsonBody);
|
||||||
|
String responseBody = request.execute().body();
|
||||||
|
log.debug("响应体: {}", responseBody);
|
||||||
|
return JSON.parseObject(responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取token
|
* 获取token
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private String getMsgToken() {
|
private String getMsgToken() {
|
||||||
String token = redisService.get(MSG_TOKEN_NAME);
|
String token = redisService.get(MSG_TOKEN_NAME);
|
||||||
if (StrUtil.isBlank(token)) {
|
if (StrUtil.isBlank(token)) {
|
||||||
String body = HttpRequest.post(TOKEN_API + "?appId=" + APP_Id + "&appPassword=" + APP_PASSWORD).execute().body();
|
String body = HttpRequest.post(TOKEN_API + "?appId=" + APP_Id + "&appPassword=" + APP_PASSWORD).execute().body();
|
||||||
NutMap map = Json.fromJson(NutMap.class, body);
|
NutMap map = Json.fromJson(NutMap.class, body);
|
||||||
if (map.getInt("rspCode") == 000000) {
|
if (Objects.equals("000000", map.getString("rspCode"))) {
|
||||||
NutMap data = Json.fromJson(NutMap.class, map.getString("resultData"));
|
NutMap data = Json.fromJson(NutMap.class, map.getString("resultData"));
|
||||||
token = data.getString("token");
|
token = data.getString("token");
|
||||||
redisService.setex(MSG_TOKEN_NAME, 3600, token);
|
redisService.setex(MSG_TOKEN_NAME, 3600, token);
|
||||||
@@ -126,6 +135,4 @@ public class MsgApi {
|
|||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user