You've already forked FrameTour-BE
refactor(task): 移除通知模块依赖
- 删除了对通知模块的包引用 - 移除了通知模块相关的类导入- 清理了与通知功能相关的代码依赖 -优化了任务服务实现类的依赖结构 - 简化了下载通知任务器的代码引用 - 解除了通知工厂类的直接依赖关系
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
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;
|
||||
import java.util.Map;
|
||||
|
||||
public class NotifyFactory {
|
||||
public static INotifyAdapter get(NotifyType type) {
|
||||
return switch (type) {
|
||||
case SERVER_CHAN -> new ServerChanNotifyAdapter();
|
||||
case WX_MP_SRV -> 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;
|
||||
|
||||
public static void register(String name, INotifyAdapter adapter) {
|
||||
namedNotifier.put(name, adapter);
|
||||
}
|
||||
|
||||
public static INotifyAdapter via(String name) {
|
||||
INotifyAdapter adapter = namedNotifier.get(name);
|
||||
if (adapter == null) {
|
||||
throw new RuntimeException("未定义的通知方式:"+name);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
public static INotifyAdapter via() {
|
||||
if (defaultNotifier == null) {
|
||||
throw new RuntimeException("未定义默认通知方式");
|
||||
}
|
||||
return defaultNotifier;
|
||||
}
|
||||
|
||||
public static void setDefault(String defaultStorage) {
|
||||
NotifyFactory.defaultNotifier = via(defaultStorage);
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
package com.ycwl.basic.notify.adapters;
|
||||
|
||||
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface INotifyAdapter {
|
||||
void loadConfig(Map<String, String> _config);
|
||||
|
||||
void sendTo(NotifyContent notifyContent, String to);
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
package com.ycwl.basic.notify.adapters;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||
import com.ycwl.basic.notify.entity.ServerChanConfig;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ServerChanNotifyAdapter implements INotifyAdapter {
|
||||
ServerChanConfig config;
|
||||
|
||||
@Override
|
||||
public void loadConfig(Map<String, String> _config) {
|
||||
ServerChanConfig config = new ServerChanConfig();
|
||||
config.setKey(_config.get("key"));
|
||||
config.checkEverythingOK();
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(NotifyContent notifyContent, String to) {
|
||||
scSend(notifyContent.getTitle(), notifyContent.getContent(), config.getKey());
|
||||
}
|
||||
|
||||
public static String scSend(String title, String content, String key) {
|
||||
try {
|
||||
String api;
|
||||
|
||||
// 判断 sendkey 是否以 "sctp" 开头,并提取数字部分拼接 URL
|
||||
if (key.startsWith("sctp")) {
|
||||
Pattern pattern = Pattern.compile("sctp(\\d+)t");
|
||||
Matcher matcher = pattern.matcher(key);
|
||||
if (matcher.find()) {
|
||||
String num = matcher.group(1);
|
||||
api = "https://" + num + ".push.ft07.com/send/" + key +".send";
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid sendkey format for sctp");
|
||||
}
|
||||
} else {
|
||||
api = "https://sctapi.ftqq.com/" + key + ".send";
|
||||
}
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("title", title);
|
||||
body.put("desp", content);
|
||||
return HttpUtil.post(api, body);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
package com.ycwl.basic.notify.adapters;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.ycwl.basic.utils.JacksonUtil;
|
||||
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);
|
||||
Map<String, Object> jsonObject = JacksonUtil.parseObject(response, Map.class);
|
||||
ACCESS_TOKEN = (String) jsonObject.get("access_token");
|
||||
Integer expiresIn = (Integer) jsonObject.get("expires_in");
|
||||
expireTime = new Date(System.currentTimeMillis() + (expiresIn != null ? expiresIn : 7200) * 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, JacksonUtil.toJSONString(params));
|
||||
System.out.println(response);
|
||||
}
|
||||
}
|
@@ -1,22 +0,0 @@
|
||||
package com.ycwl.basic.notify.entity;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
package com.ycwl.basic.notify.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ServerChanConfig {
|
||||
private String key;
|
||||
|
||||
public void checkEverythingOK() {
|
||||
}
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
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() {
|
||||
|
||||
}
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
package com.ycwl.basic.notify.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum NotifyType {
|
||||
WX_MP_SRV("WX_MP_SRV"),
|
||||
SERVER_CHAN("SERVER_CHAN");
|
||||
|
||||
private final String type;
|
||||
|
||||
NotifyType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
package com.ycwl.basic.notify.starter;
|
||||
|
||||
import com.ycwl.basic.notify.NotifyFactory;
|
||||
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||
import com.ycwl.basic.notify.starter.config.NotifyConfigItem;
|
||||
import com.ycwl.basic.notify.starter.config.OverallNotifyConfig;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class NotifyAutoConfigurator {
|
||||
private final OverallNotifyConfig config;
|
||||
public NotifyAutoConfigurator(OverallNotifyConfig config) {
|
||||
this.config = config;
|
||||
if (config != null) {
|
||||
if (config.getConfigs() != null) {
|
||||
loadConfig();
|
||||
}
|
||||
if (StringUtils.isNotBlank(config.getDefaultUse())) {
|
||||
NotifyFactory.setDefault(config.getDefaultUse());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
for (NotifyConfigItem item : config.getConfigs()) {
|
||||
INotifyAdapter adapter = NotifyFactory.get(item.getType());
|
||||
adapter.loadConfig(item.getConfig());
|
||||
NotifyFactory.register(item.getName(), adapter);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package com.ycwl.basic.notify.starter.config;
|
||||
|
||||
import com.ycwl.basic.notify.enums.NotifyType;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class NotifyConfigItem {
|
||||
private String name;
|
||||
private NotifyType type;
|
||||
private Map<String, String> config;
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
package com.ycwl.basic.notify.starter.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "notify")
|
||||
@Data
|
||||
public class OverallNotifyConfig {
|
||||
private String defaultUse;
|
||||
private List<NotifyConfigItem> configs;
|
||||
}
|
@@ -41,10 +41,6 @@ import com.ycwl.basic.model.task.req.TaskReqVo;
|
||||
import com.ycwl.basic.model.task.req.TaskSuccessReqVo;
|
||||
import com.ycwl.basic.model.task.req.WorkerAuthReqVo;
|
||||
import com.ycwl.basic.model.task.resp.TaskSyncRespVo;
|
||||
import com.ycwl.basic.notify.NotifyFactory;
|
||||
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||
import com.ycwl.basic.notify.enums.NotifyType;
|
||||
import com.ycwl.basic.repository.DeviceRepository;
|
||||
import com.ycwl.basic.repository.FaceRepository;
|
||||
import com.ycwl.basic.repository.RenderWorkerRepository;
|
||||
|
@@ -14,10 +14,6 @@ import com.ycwl.basic.model.pc.mp.MpConfigEntity;
|
||||
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
|
||||
import com.ycwl.basic.model.pc.scenic.entity.ScenicEntity;
|
||||
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
|
||||
import com.ycwl.basic.notify.NotifyFactory;
|
||||
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||
import com.ycwl.basic.notify.enums.NotifyType;
|
||||
import com.ycwl.basic.repository.ScenicRepository;
|
||||
import com.ycwl.basic.repository.TemplateRepository;
|
||||
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
|
||||
|
Reference in New Issue
Block a user