|
@@ -4,16 +4,31 @@ import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.collection.ListUtil;
|
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.huawei.shade.org.apache.http.HttpEntity;
|
|
|
+import com.huawei.shade.org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import com.huawei.shade.org.apache.http.client.methods.HttpGet;
|
|
|
+import com.huawei.shade.org.apache.http.client.methods.HttpPost;
|
|
|
+import com.huawei.shade.org.apache.http.entity.StringEntity;
|
|
|
+import com.huawei.shade.org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import com.huawei.shade.org.apache.http.impl.client.HttpClients;
|
|
|
+import com.huawei.shade.org.apache.http.util.EntityUtils;
|
|
|
+import com.simuwang.base.pojo.CoreMailAttachmentDTO;
|
|
|
import com.simuwang.base.pojo.dos.*;
|
|
|
import com.simuwang.daq.util.HttpClientUtil;
|
|
|
import jakarta.validation.constraints.NotNull;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -26,6 +41,9 @@ public class CoreMailApiService {
|
|
|
@Value("${coremail.url}")
|
|
|
private String coreMailUrl;
|
|
|
|
|
|
+ @Value("${email.file.path}")
|
|
|
+ private String navFilePath;
|
|
|
+
|
|
|
/**
|
|
|
* 登录获取token
|
|
|
*
|
|
@@ -69,56 +87,168 @@ public class CoreMailApiService {
|
|
|
|
|
|
public List<CoreMailFolderDTO> getAllFolders(String token, String cookie) {
|
|
|
String url = coreMailUrl + "/coremail/s/json?func=mbox:getAllFolders&sid=" + token;
|
|
|
- Map<String, String> requestParam = new HashMap<>();
|
|
|
- requestParam.put("Cookie.Coremail", cookie);
|
|
|
- requestParam.put("sid", token);
|
|
|
- CoreMailFolderParam coreMailFolderParam = new CoreMailFolderParam();
|
|
|
- coreMailFolderParam.setCoremail(cookie);
|
|
|
-
|
|
|
- Map<String, Object> bodyMap = new HashMap<>();
|
|
|
+ Map<String, String> bodyMap = new HashMap<>();
|
|
|
bodyMap.put("flush", "ture");
|
|
|
bodyMap.put("stats", "false");
|
|
|
bodyMap.put("threads", "false");
|
|
|
bodyMap.put("order", "name");
|
|
|
- bodyMap.put("Coremail", cookie);
|
|
|
String bodyParam = JSONObject.toJSONString(bodyMap);
|
|
|
- log.info("获取邮箱文件夹接口请求体 -> url:{}, requestParam:{}, bodyParam:{}", url, requestParam, bodyParam);
|
|
|
+ log.info("获取邮箱文件夹接口请求体 -> url:{}, bodyParam:{}", url, bodyParam);
|
|
|
+ String resp = null;
|
|
|
try {
|
|
|
- String resp = HttpUtil.get(url, bodyMap);
|
|
|
- log.info("获取邮箱文件夹接口响应体:{}", resp);
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Cookie", "Coremail=" + cookie);
|
|
|
+ // 设置请求体
|
|
|
+ StringEntity entity = new StringEntity(bodyParam);
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ // 发送请求
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (responseEntity != null) {
|
|
|
+ resp = EntityUtils.toString(responseEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // log.info("获取邮箱文件夹接口响应体:{}", resp);
|
|
|
if (StrUtil.isBlank(resp)) {
|
|
|
log.warn("获取邮箱文件夹接口返回空响应");
|
|
|
return CollUtil.newArrayList();
|
|
|
}
|
|
|
-
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(resp);
|
|
|
+ if (jsonObject == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!"S_OK".equals(jsonObject.getString("code"))) {
|
|
|
+ log.error("获取邮箱文件夹失败, 返回码:{}", jsonObject.getString("code"));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String var = jsonObject.get("var") != null ? jsonObject.get("var").toString() : null;
|
|
|
+ if (var != null) {
|
|
|
+ return JSON.parseArray(var, CoreMailFolderDTO.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
log.error("获取邮箱文件夹接口异常:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
}
|
|
|
- return ListUtil.toList(new CoreMailFolderDTO(1, "inbox"));
|
|
|
+ return ListUtil.toList(new CoreMailFolderDTO(1, "收件箱"));
|
|
|
}
|
|
|
|
|
|
- public List<CoreMailInfoDTO> getFolderMails(String token, Integer folderId) {
|
|
|
+ public List<CoreMailInfoDTO> getFolderMails(String token, String cookie, Integer folderId) {
|
|
|
String url = coreMailUrl + "/coremail/s/json?func=mbox:listMessages&sid=" + token;
|
|
|
- Map<String, String> requestParam = new HashMap<>();
|
|
|
- requestParam.put("sid", token);
|
|
|
CoreMailInfoParam coreMailInfoParam = new CoreMailInfoParam();
|
|
|
coreMailInfoParam.setFid(folderId);
|
|
|
- coreMailInfoParam.setFilter(new CoreMailInfoParam.FilterCondition("3:", "净值,业绩报酬,规模"));
|
|
|
+ coreMailInfoParam.setFilter(new CoreMailInfoParam.FilterCondition("1:"));
|
|
|
String bodyParam = JSONObject.toJSONString(coreMailInfoParam);
|
|
|
- log.info("获取文件夹邮件接口请求体 -> url:{}, requestParam:{}, bodyParam:{}", url, requestParam, bodyParam);
|
|
|
+ log.info("获取邮件接口请求体 -> url:{}, bodyParam:{}", url, bodyParam);
|
|
|
+ String resp = null;
|
|
|
try {
|
|
|
- String resp = HttpUtil.post(url, bodyParam);
|
|
|
- log.info("获取文件夹邮件接口响应体:{}", resp);
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Cookie", "Coremail=" + cookie);
|
|
|
+ // 设置请求体
|
|
|
+ StringEntity entity = new StringEntity(bodyParam);
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ // 发送请求
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (responseEntity != null) {
|
|
|
+ resp = EntityUtils.toString(responseEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // log.info("获取邮件接口响应体:{}", resp);
|
|
|
if (StrUtil.isBlank(resp)) {
|
|
|
- log.warn("获取文件夹邮件接口返回空响应");
|
|
|
+ log.warn("获取邮件接口返回空响应");
|
|
|
return CollUtil.newArrayList();
|
|
|
}
|
|
|
-
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(resp);
|
|
|
+ if (jsonObject == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!"S_OK".equals(jsonObject.getString("code"))) {
|
|
|
+ log.error("获取邮件失败, 返回码:{}", jsonObject.getString("code"));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String var = jsonObject.get("var") != null ? jsonObject.get("var").toString() : null;
|
|
|
+ if (var != null) {
|
|
|
+ return JSON.parseArray(var, CoreMailInfoDTO.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
-
|
|
|
- log.error("获取文件夹邮件接口异常:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
+ log.error("获取邮件接口异常:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
}
|
|
|
+ return CollUtil.newArrayList();
|
|
|
+ }
|
|
|
|
|
|
+ public List<CoreMailAttachmentDTO> getAttachmentOfMail(String token, String cookie, String mid) {
|
|
|
+ String url = coreMailUrl +
|
|
|
+ "/coremail/s/json?func=mbox:readMessage&sid=" + token +
|
|
|
+ "&mid=" + mid;
|
|
|
+ String resp = null;
|
|
|
+ try {
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ // 设置请求头
|
|
|
+ httpGet.setHeader("Cookie", "Coremail=" + cookie);
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (responseEntity != null) {
|
|
|
+ resp = EntityUtils.toString(responseEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // log.info("获取邮件附件接口响应体:{}", resp);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(resp);
|
|
|
+ if (jsonObject == null) {
|
|
|
+ return CollUtil.newArrayList();
|
|
|
+ }
|
|
|
+ if (!"S_OK".equals(jsonObject.getString("code"))) {
|
|
|
+ log.error("获取邮件附件失败, 返回码:{}", jsonObject.getString("code"));
|
|
|
+ return CollUtil.newArrayList();
|
|
|
+ }
|
|
|
+ String var = jsonObject.get("var") != null ? jsonObject.get("var").toString() : null;
|
|
|
+ if (var == null) {
|
|
|
+ return CollUtil.newArrayList();
|
|
|
+ }
|
|
|
+ JSONObject varJsonObject = JSONObject.parseObject(var);
|
|
|
+ return varJsonObject.getJSONArray("attachments").toJavaList(CoreMailAttachmentDTO.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取邮件附件接口异常:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
+ }
|
|
|
return CollUtil.newArrayList();
|
|
|
}
|
|
|
+
|
|
|
+ public File getFileFromAttachmentMail(String token, String cookie, String mid, int part, String fileName) {
|
|
|
+ String url = coreMailUrl +
|
|
|
+ "/coremail/s/json?func=mbox:getMessageData&sid=" + token +
|
|
|
+ "&mid=" + mid +
|
|
|
+ "&part=" + part;
|
|
|
+
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ // 设置 Cookie 请求头
|
|
|
+ httpGet.setHeader("Cookie", "Coremail=" + cookie);
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (responseEntity != null) {
|
|
|
+ InputStream content = responseEntity.getContent();
|
|
|
+ // 将二进制数据流转成字节数组
|
|
|
+ byte[] contentData = IOUtils.toByteArray(content);
|
|
|
+
|
|
|
+ // 保存为文件
|
|
|
+ File targetFile = new File(navFilePath + mid + "/" + fileName);
|
|
|
+ FileUtils.writeByteArrayToFile(targetFile, contentData);
|
|
|
+
|
|
|
+ log.info("文件已成功保存至: {}", targetFile.getAbsolutePath());
|
|
|
+ return targetFile;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("保存附件时发生异常: {}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
}
|