Compare commits
2 Commits
be84a05894
...
d6d759ef19
| Author | SHA1 | Date |
|---|---|---|
|
|
d6d759ef19 | |
|
|
e355aba267 |
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue