支持微信服务通知,调整方法
This commit is contained in:
parent
afaac1bb0c
commit
9b32c2fd75
@ -62,7 +62,10 @@ public class CustomExceptionHandle {
|
|||||||
@ExceptionHandler(value = Exception.class)
|
@ExceptionHandler(value = Exception.class)
|
||||||
public ApiResponse<String> handle(Exception e) {
|
public ApiResponse<String> handle(Exception e) {
|
||||||
LOGGER.error("系统异常 -> {}", e.getMessage(), e);
|
LOGGER.error("系统异常 -> {}", e.getMessage(), e);
|
||||||
NotifyFactory.to().send(new NotifyContent("帧途后台报错了!", e.getMessage() + "\n\n" + Arrays.toString(e.getStackTrace())));
|
NotifyFactory.to().sendTo(
|
||||||
|
new NotifyContent("帧途后台报错了!", e.getMessage() + "\n\n" + Arrays.toString(e.getStackTrace())),
|
||||||
|
"default_user"
|
||||||
|
);
|
||||||
return ApiResponse.buildResult(BizCodeEnum.SERVER_UNKONWN_ERROR);
|
return ApiResponse.buildResult(BizCodeEnum.SERVER_UNKONWN_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package com.ycwl.basic.notify;
|
|||||||
|
|
||||||
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||||
import com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter;
|
import com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter;
|
||||||
|
import com.ycwl.basic.notify.adapters.WxMpSrvNotifyAdapter;
|
||||||
import com.ycwl.basic.notify.enums.NotifyType;
|
import com.ycwl.basic.notify.enums.NotifyType;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -12,11 +13,18 @@ public class NotifyFactory {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case SERVER_CHAN:
|
case SERVER_CHAN:
|
||||||
return new ServerChanNotifyAdapter();
|
return new ServerChanNotifyAdapter();
|
||||||
|
case WX_MP_SRV:
|
||||||
|
return new WxMpSrvNotifyAdapter();
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("不支持的通知类型");
|
throw new RuntimeException("不支持的通知类型");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static INotifyAdapter get(NotifyType type, Map<String, String> config) {
|
||||||
|
INotifyAdapter adapter = get(type);
|
||||||
|
adapter.loadConfig(config);
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
|
||||||
protected static Map<String, INotifyAdapter> namedNotifier = new HashMap<>();
|
protected static Map<String, INotifyAdapter> namedNotifier = new HashMap<>();
|
||||||
protected static INotifyAdapter defaultNotifier = null;
|
protected static INotifyAdapter defaultNotifier = null;
|
||||||
|
@ -7,5 +7,5 @@ import java.util.Map;
|
|||||||
public interface INotifyAdapter {
|
public interface INotifyAdapter {
|
||||||
void loadConfig(Map<String, String> _config);
|
void loadConfig(Map<String, String> _config);
|
||||||
|
|
||||||
void send(NotifyContent notifyContent);
|
void sendTo(NotifyContent notifyContent, String to);
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ public class ServerChanNotifyAdapter implements INotifyAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(NotifyContent notifyContent) {
|
public void sendTo(NotifyContent notifyContent, String to) {
|
||||||
scSend(notifyContent.getTitle(), notifyContent.getContent(), config.getKey());
|
scSend(notifyContent.getTitle(), notifyContent.getContent(), config.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.ycwl.basic.notify.adapters;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||||
|
import com.ycwl.basic.notify.entity.WxMpSrvConfig;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class WxMpSrvNotifyAdapter implements INotifyAdapter{
|
||||||
|
private WxMpSrvConfig config;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadConfig(Map<String, String> _config) {
|
||||||
|
WxMpSrvConfig config = new WxMpSrvConfig();
|
||||||
|
config.setAppId(_config.get("appId"));
|
||||||
|
config.setAppSecret(_config.get("appSecret"));
|
||||||
|
if (_config.containsKey("state")) {
|
||||||
|
config.setState(_config.get("state"));
|
||||||
|
}
|
||||||
|
config.checkEverythingOK();
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendTo(NotifyContent notifyContent, String openId) {
|
||||||
|
Map<String, Object> params = notifyContent.getParams();
|
||||||
|
params.put("touser", openId);
|
||||||
|
params.put("miniprogram_state", config.getState());
|
||||||
|
sendServiceNotification(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String SEND_TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s";
|
||||||
|
|
||||||
|
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
|
||||||
|
private String ACCESS_TOKEN = "";
|
||||||
|
private Date expireTime = new Date();
|
||||||
|
|
||||||
|
private String getAccessToken() {
|
||||||
|
if (ACCESS_TOKEN != null && !ACCESS_TOKEN.isEmpty()) {
|
||||||
|
if (expireTime.getTime() > System.currentTimeMillis()) {
|
||||||
|
return ACCESS_TOKEN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String url = String.format(ACCESS_TOKEN_URL, config.getAppId(), config.getAppSecret());
|
||||||
|
String response = HttpUtil.get(url);
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
|
ACCESS_TOKEN = jsonObject.getString("access_token");
|
||||||
|
expireTime = new Date(System.currentTimeMillis() + jsonObject.getInteger("expires_in") * 1000);
|
||||||
|
return ACCESS_TOKEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendServiceNotification(Map<String, Object> params) {
|
||||||
|
String url = String.format(SEND_TEMPLATE_MESSAGE_URL, getAccessToken());
|
||||||
|
String response = HttpUtil.post(url, JSONObject.toJSONString(params));
|
||||||
|
System.out.println(response);
|
||||||
|
}
|
||||||
|
}
|
@ -4,10 +4,19 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class NotifyContent {
|
public class NotifyContent {
|
||||||
private String title;
|
private String title;
|
||||||
private String content;
|
private String content;
|
||||||
|
private Map<String, Object> params = new HashMap<>();
|
||||||
|
|
||||||
|
public NotifyContent(String title, String content) {
|
||||||
|
this.title = title;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,5 @@ public class ServerChanConfig {
|
|||||||
private String key;
|
private String key;
|
||||||
|
|
||||||
public void checkEverythingOK() {
|
public void checkEverythingOK() {
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.ycwl.basic.notify.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class WxMpSrvConfig {
|
||||||
|
private String appId;
|
||||||
|
private String appSecret;
|
||||||
|
private String state = "formal";
|
||||||
|
private String templateId;
|
||||||
|
|
||||||
|
public void checkEverythingOK() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import lombok.Getter;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum NotifyType {
|
public enum NotifyType {
|
||||||
|
WX_MP_SRV("WX_MP_SRV"),
|
||||||
SERVER_CHAN("SERVER_CHAN");
|
SERVER_CHAN("SERVER_CHAN");
|
||||||
|
|
||||||
private final String type;
|
private final String type;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user