Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增webhook通知 #330

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.oddfar.campus.common.enums;

/**
* @author gitsilence
* @date 2024-03-17
*/
public enum NoticeEnum {
PUSH_PLUS, WEB_HOOk
;

public static NoticeEnum getNoticeType(int type) {
for (NoticeEnum value : NoticeEnum.values()) {
if (value.ordinal() == type) {
return value;
}
}
return WEB_HOOk;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.oddfar.campus.business.controller;

import com.oddfar.campus.business.entity.ILog;
import com.oddfar.campus.business.entity.IUser;
import com.oddfar.campus.business.mapper.IUserMapper;
import com.oddfar.campus.business.notice.NoticeHelper;
import com.oddfar.campus.business.service.IMTService;
import com.oddfar.campus.business.service.IShopService;
import com.oddfar.campus.common.annotation.Anonymous;
import com.oddfar.campus.common.annotation.ApiResource;
import com.oddfar.campus.common.annotation.Log;
import com.oddfar.campus.common.domain.R;
import com.oddfar.campus.common.enums.ResBizTypeEnum;
import com.oddfar.campus.framework.manager.AsyncManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -37,6 +41,25 @@ public R test1() {
return R.ok();
}

/**
* 需要接口权限
*/
@PreAuthorize("@ss.resourceAuth()")
@GetMapping(value = "/sendNotice", name = "发送通知的测试")
public R sendNotice() {
IUser iUser = new IUser();
iUser.setToken("11111");
iUser.setPushPlusToken("222");
iUser.setMobile(131313131L);
iUser.setRemark("zs");
ILog iLog = new ILog();
iLog.setStatus(0);
iLog.setMobile(131313131L);
iLog.setLogContent("test ~~~~");
NoticeHelper.sendNotice(iUser, iLog);
return R.ok();
}

/**
* 需要 'campus:test' 权限字符串
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.oddfar.campus.business.notice;

import com.oddfar.campus.common.enums.NoticeEnum;
import org.springframework.beans.factory.InitializingBean;

/**
* @author gitsilence
* @date 2024-03-17
*/
public abstract class AbstractNoticeHandler implements NoticeHandler, InitializingBean {


public abstract NoticeEnum getBizType();

@Override
public void afterPropertiesSet() throws Exception {
NoticeFactory.addService(getBizType(), this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.oddfar.campus.business.notice;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* @author gitsilence
* @date 2024-03-17
*/
@ConfigurationProperties(prefix = "notice.config")
@Component
@Data
public class NoticeConfig {

/**
* 发送类型
*/
private Integer noticeType = 1;

/**
* api url
*/
private String apiUrl = "";

/**
* 请求体
*
*/
private String requestBody;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.oddfar.campus.business.notice;

import com.oddfar.campus.common.enums.NoticeEnum;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.xml.ws.Service;

/**
* @author gitsilence
* @date 2024-03-17
*/
public class NoticeFactory {

private static final Map<NoticeEnum, NoticeHandler> SERVICE_MAP = Collections.synchronizedMap(new HashMap<>());

public static void addService(NoticeEnum noticeEnum, NoticeHandler service) {
if (SERVICE_MAP.containsKey(noticeEnum)) {
return;
}
SERVICE_MAP.put(noticeEnum, service);
}

public static NoticeHandler getService(NoticeEnum noticeEnum) {
NoticeHandler handler = SERVICE_MAP.get(noticeEnum);
if (handler == null) {
throw new RuntimeException("服务未找到");
}
return handler;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.oddfar.campus.business.notice;

import com.oddfar.campus.business.entity.ILog;
import com.oddfar.campus.business.entity.IUser;
import java.util.TimerTask;

/**
* @author gitsilence
* @date 2024-03-17
*/
public interface NoticeHandler {

/**
* 发送通知
* @param token
* @param title
* @param content
* @param template
*/
TimerTask sendNotice(String token, String title, String content, String template);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.oddfar.campus.business.notice;

import com.oddfar.campus.business.entity.ILog;
import com.oddfar.campus.business.entity.IUser;
import com.oddfar.campus.common.enums.NoticeEnum;
import com.oddfar.campus.common.utils.SpringUtils;
import com.oddfar.campus.common.utils.StringUtils;
import com.oddfar.campus.framework.manager.AsyncManager;

/**
* @author gitsilence
* @date 2024-03-17
*/
public class NoticeHelper {

public static void sendNotice(IUser iUser, ILog operLog) {
String token = iUser.getPushPlusToken();
String title, content;
if (operLog.getStatus() == 0) {
//预约成功
title = iUser.getRemark() + "-i茅台执行成功";
content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent();
} else {
//预约失败
title = iUser.getRemark() + "-i茅台执行失败";
content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent();
}
AsyncManager.me().execute(NoticeFactory.getService(NoticeEnum.getNoticeType(SpringUtils.getBean(NoticeConfig.class)
.getNoticeType()))
.sendNotice(token, title, content, "txt"));
}

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
package com.oddfar.campus.business.api;
package com.oddfar.campus.business.notice.impl;

import cn.hutool.http.HttpUtil;
import com.oddfar.campus.business.entity.ILog;
import com.oddfar.campus.business.entity.IUser;
import com.oddfar.campus.business.notice.AbstractNoticeHandler;
import com.oddfar.campus.common.enums.NoticeEnum;
import com.oddfar.campus.common.utils.StringUtils;
import com.oddfar.campus.framework.manager.AsyncManager;

import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import org.springframework.stereotype.Component;

/**
* @author zhiyuan
*/
public class PushPlusApi {
@Component
public class PushPlusApiHandlerImpl extends AbstractNoticeHandler {


public static void sendNotice(IUser iUser, ILog operLog) {
String token = iUser.getPushPlusToken();
if (StringUtils.isEmpty(token)) {
return;
}
String title, content;
if (operLog.getStatus() == 0) {
//预约成功
title = iUser.getRemark() + "-i茅台执行成功";
content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent();
AsyncManager.me().execute(sendNotice(token, title, content, "txt"));
} else {
//预约失败
title = iUser.getRemark() + "-i茅台执行失败";
content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent();
AsyncManager.me().execute(sendNotice(token, title, content, "txt"));
}


}

/**
* push推送
*
Expand All @@ -45,7 +28,7 @@ public static void sendNotice(IUser iUser, ILog operLog) {
* @param content 具体消息内容
* @param template 发送消息模板
*/
public static TimerTask sendNotice(String token, String title, String content, String template) {
public TimerTask sendNotice(String token, String title, String content, String template) {
return new TimerTask() {
@Override
public void run() {
Expand All @@ -62,4 +45,8 @@ public void run() {
};
}

@Override
public NoticeEnum getBizType() {
return NoticeEnum.PUSH_PLUS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.oddfar.campus.business.notice.impl;

import cn.hutool.http.HttpUtil;
import com.oddfar.campus.business.notice.AbstractNoticeHandler;
import com.oddfar.campus.business.notice.NoticeConfig;
import com.oddfar.campus.common.enums.NoticeEnum;
import com.oddfar.campus.common.utils.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.TimerTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* @author gitsilence
* @date 2024-03-17
*/
@Component
public class WebhookHandlerImpl extends AbstractNoticeHandler {

@Autowired
private NoticeConfig noticeConfig;

@Override
public NoticeEnum getBizType() {
return NoticeEnum.WEB_HOOk;
}

@Override
public TimerTask sendNotice(String token, String title, String content, String template) {
return new TimerTask() {
@Override
public void run() {
String apiUrl = StringUtils.replace(noticeConfig.getApiUrl(), "{token}", token);
String requestBody = null;
try {
requestBody = StringUtils.replace(noticeConfig.getRequestBody(), "{title}", title)
.replace("{content}", URLEncoder.encode(content, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

HttpUtil.post(apiUrl, requestBody);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.oddfar.campus.business.service;

import com.oddfar.campus.business.api.PushPlusApi;
import com.oddfar.campus.business.notice.NoticeHelper;
import com.oddfar.campus.business.entity.ILog;
import com.oddfar.campus.business.entity.IUser;
import com.oddfar.campus.common.utils.SpringUtils;
Expand Down Expand Up @@ -33,7 +33,7 @@ public static void reservation(IUser iUser, String logContent) {

AsyncManager.me().execute(recordOper(operLog));
//推送
PushPlusApi.sendNotice(iUser, operLog);
NoticeHelper.sendNotice(iUser, operLog);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions campus-modular/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ mybatis-plus:
logic-delete-value: 1
logic-not-delete-value: 0

notice:
config:
notice-type: 1
api-url: http://192.168.0.106:8080/bot/msg/send?access_token={token}&group_id=xxx
request-body: '{"message":"{content}"}'


# Swagger配置
Expand Down