支持微信服务通知,调整方法
This commit is contained in:
parent
afaac1bb0c
commit
9b32c2fd75
@ -62,7 +62,10 @@ public class CustomExceptionHandle {
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public ApiResponse<String> handle(Exception 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);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.ycwl.basic.notify;
|
||||
|
||||
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||
import com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter;
|
||||
import com.ycwl.basic.notify.adapters.WxMpSrvNotifyAdapter;
|
||||
import com.ycwl.basic.notify.enums.NotifyType;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -12,11 +13,18 @@ public class NotifyFactory {
|
||||
switch (type) {
|
||||
case SERVER_CHAN:
|
||||
return new ServerChanNotifyAdapter();
|
||||
case WX_MP_SRV:
|
||||
return new WxMpSrvNotifyAdapter();
|
||||
default:
|
||||
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 INotifyAdapter defaultNotifier = null;
|
||||
|
@ -7,5 +7,5 @@ import java.util.Map;
|
||||
public interface INotifyAdapter {
|
||||
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
|
||||
public void send(NotifyContent notifyContent) {
|
||||
public void sendTo(NotifyContent notifyContent, String to) {
|
||||
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.NoArgsConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class NotifyContent {
|
||||
private String title;
|
||||
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;
|
||||
|
||||
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
|
||||
public enum NotifyType {
|
||||
WX_MP_SRV("WX_MP_SRV"),
|
||||
SERVER_CHAN("SERVER_CHAN");
|
||||
|
||||
private final String type;
|
||||
|
Loading…
x
Reference in New Issue
Block a user