feat(rtx-send): 新增RTX消息发送服务
- 添加RtxSendService类实现RTX通知消息发送功能 - 支持发送弹窗标题和内容的通知消息,使用GBK编码转换 - 支持发送即时消息(IM),包含发送人和接收人参数 - 通过RestTemplate发送HTTP GET请求调用RTX CGI接口 - 对接口响应进行成功失败判断并打印日志 - 捕获编码异常和请求异常保证服务稳定运行
This commit is contained in:
parent
be84a05894
commit
e355aba267
|
|
@ -0,0 +1,100 @@
|
||||||
|
package com.nflg.qms.admin.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class RtxSendService {
|
||||||
|
|
||||||
|
private static final String RTX_SERVER_URL = "http://192.168.1.100:8012" ;
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public RtxSendService(RestTemplate restTemplate) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 RTX 通知消息
|
||||||
|
* @param receivers 接收人(填RTX登录名,非姓名)
|
||||||
|
* @param title 弹窗标题
|
||||||
|
* @param content 消息内容
|
||||||
|
*/
|
||||||
|
public void sendNotify(List<String> receivers, String title, String content) {
|
||||||
|
try {
|
||||||
|
// 1. 构建请求 URL
|
||||||
|
String url = RTX_SERVER_URL + "/sendnotify.cgi";
|
||||||
|
|
||||||
|
// 2. 拼接参数 (极其重要:中文必须转码,且RTX默认通常要求 GBK 编码)
|
||||||
|
String encodedTitle = URLEncoder.encode(title, "GBK");
|
||||||
|
String encodedContent = URLEncoder.encode(content, "GBK");
|
||||||
|
|
||||||
|
String fullUrl = UriComponentsBuilder.fromHttpUrl(url)
|
||||||
|
.queryParam("receiver", StrUtil.join(",", receivers))
|
||||||
|
.queryParam("title", encodedTitle)
|
||||||
|
.queryParam("msg", encodedContent)
|
||||||
|
.toUriString();
|
||||||
|
|
||||||
|
// 3. 发送 GET 请求 (RTX CGI接口通常接受GET)
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(fullUrl, String.class);
|
||||||
|
|
||||||
|
// 4. 处理结果
|
||||||
|
if (response.getStatusCode().is2xxSuccessful() &&
|
||||||
|
response.getBody() != null &&
|
||||||
|
response.getBody().contains("0")) { // RTX接口返回包含 "<code>0</code>" 表示成功
|
||||||
|
System.out.println("RTX 发送成功");
|
||||||
|
} else {
|
||||||
|
System.err.println("RTX 发送失败,返回内容: " + response.getBody());
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
System.err.println("编码异常: " + e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("请求 RTX 异常: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 RTX 通知消息
|
||||||
|
* @param receiver 接收人(填RTX登录名,非姓名)
|
||||||
|
* @param content 消息内容
|
||||||
|
*/
|
||||||
|
public void sendIM(String sender,String receiver, String content) {
|
||||||
|
try {
|
||||||
|
// 1. 构建请求 URL
|
||||||
|
String url = RTX_SERVER_URL + "/sendIM.cgi";
|
||||||
|
|
||||||
|
// 2. 拼接参数 (极其重要:中文必须转码,且RTX默认通常要求 GBK 编码)
|
||||||
|
String encodedContent = URLEncoder.encode(content, "GBK");
|
||||||
|
|
||||||
|
String fullUrl = UriComponentsBuilder.fromHttpUrl(url)
|
||||||
|
.queryParam("sender", sender)
|
||||||
|
.queryParam("receiver", receiver)
|
||||||
|
.queryParam("msg", encodedContent)
|
||||||
|
.toUriString();
|
||||||
|
|
||||||
|
// 3. 发送 GET 请求 (RTX CGI接口通常接受GET)
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(fullUrl, String.class);
|
||||||
|
|
||||||
|
// 4. 处理结果
|
||||||
|
if (response.getStatusCode().is2xxSuccessful() &&
|
||||||
|
response.getBody() != null &&
|
||||||
|
response.getBody().contains("0")) { // RTX接口返回包含 "<code>0</code>" 表示成功
|
||||||
|
System.out.println("RTX 发送成功");
|
||||||
|
} else {
|
||||||
|
System.err.println("RTX 发送失败,返回内容: " + response.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
System.err.println("编码异常: " + e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("请求 RTX 异常: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue