Compare commits

...

2 Commits

Author SHA1 Message Date
曹鹏飞 d6d759ef19 refactor(rtx-send): 将RTX服务器地址改为配置项
- 使用@Value注解注入rtx服务器相关URL配置
- 替换硬编码的RTX服务器基础路径为配置属性
- 调整sendNotify方法中的URL拼接逻辑
- 调整sendIM方法中的URL拼接逻辑
- 保持消息内容编码逻辑不变,确保GBK编码要求
2026-04-14 20:00:34 +08:00
曹鹏飞 e355aba267 feat(rtx-send): 新增RTX消息发送服务
- 添加RtxSendService类实现RTX通知消息发送功能
- 支持发送弹窗标题和内容的通知消息,使用GBK编码转换
- 支持发送即时消息(IM),包含发送人和接收人参数
- 通过RestTemplate发送HTTP GET请求调用RTX CGI接口
- 对接口响应进行成功失败判断并打印日志
- 捕获编码异常和请求异常保证服务稳定运行
2026-04-14 19:56:33 +08:00
1 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,108 @@
package com.nflg.qms.admin.service;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
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 {
@Value("${rtx.server.url}")
private String rtxServerUrl;
@Value("${rtx.notify.url}")
private String notifyUrl;
@Value("${rtx.im.url}")
private String imUrl;
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 = rtxServerUrl + notifyUrl;
// 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 = rtxServerUrl + imUrl;
// 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());
}
}
}