You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
9.2 KiB
217 lines
9.2 KiB
3 years ago
|
/*
|
||
|
* Copyright (C), 2018-2021
|
||
|
* Project: starter
|
||
|
* FileName: PostJsonOutput
|
||
|
* Author: Louis
|
||
|
* Date: 2021/4/15 10:45
|
||
|
*/
|
||
|
package com.fr.plugin.idfh.action;
|
||
|
|
||
|
import com.fanruan.api.i18n.I18nKit;
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.net.http.rs.HttpRequestType;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.decision.system.bean.message.MessageUrlType;
|
||
|
import com.fr.decision.webservice.v10.message.MessageService;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
import com.fr.general.DateUtils;
|
||
|
import com.fr.io.utils.ResourceIOUtils;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.idfh.kit.HttpKit;
|
||
|
import com.fr.schedule.base.constant.ScheduleConstants;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.third.org.apache.http.HttpEntity;
|
||
|
import com.fr.third.org.apache.http.entity.StringEntity;
|
||
|
import com.fr.third.org.apache.http.entity.mime.HttpMultipartMode;
|
||
|
import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder;
|
||
|
import com.fr.third.org.apache.http.entity.mime.content.FileBody;
|
||
|
import com.fr.third.springframework.web.util.HtmlUtils;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.*;
|
||
|
|
||
|
/**
|
||
|
* <Function Description><br>
|
||
|
* <PostJsonOutput>
|
||
|
*
|
||
|
* @author fr.open
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class PostJsonOutput extends AbstractDataOutput {
|
||
|
public static final String KEY = "PostJsonOutput";
|
||
|
public static final String USERNAME = "username";
|
||
|
public static final String VALIDATE_CODE = "validateCode";
|
||
|
public static final String LOGIN_URL = "loginUrl";
|
||
|
|
||
|
@Override
|
||
|
public void checkHeader(Map<String, String> headers) throws Exception {
|
||
|
if (!headers.containsKey("Content-Type")) {
|
||
|
headers.put("Content-Type", "application/json");
|
||
|
}
|
||
|
if (!ComparatorUtils.equalsIgnoreCase(headers.get("Content-Type"), "application/json")) {
|
||
|
throw new Exception(I18nKit.getLocText("Plugin-httpaction_Header_Content-Type_Valid"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void operation(OutputHttp outputHttp, Map<String, Object> taskParams, Map<String, String> bodyParams, Map<String, String> headers) throws Exception {
|
||
|
LogKit.info("idfh-PostJsonOutput-operation-bodyParams:{}, taskParams:{}", bodyParams, taskParams);
|
||
|
JSONObject msgEntity = createMsgEntity(bodyParams, outputHttp.getBodyContent());
|
||
|
headers.put("Authorization", "Bearer " + getAPIToken(bodyParams));
|
||
|
LogKit.info("idfh-PostJsonOutput-operation-headers:{}", headers);
|
||
|
List<String> attachFiles = uploadFiles(taskParams, bodyParams.get("uploadUrl"), headers.get("Authorization"));
|
||
|
LogKit.info("idfh-PostJsonOutput-operation-attachFiles:{}", attachFiles);
|
||
|
if (outputHttp.isPreviewAttach()) {
|
||
|
handleContent(attachFiles, msgEntity);
|
||
|
}
|
||
|
String[] usernames = getUserNames(taskParams);
|
||
|
JSONArray msgList = JSONArray.create();
|
||
|
for (String username : usernames) {
|
||
|
msgEntity.put("originalMsgId", UUID.randomUUID());
|
||
|
msgEntity.put("employeeNo", username);
|
||
|
for (int i = 0; i < Math.min(attachFiles.size(), 5); i++) {
|
||
|
if (attachFiles.get(i).endsWith(ScheduleConstants.Suffix.PNG)) {
|
||
|
continue;
|
||
|
}
|
||
|
msgEntity.put("attachFile" + (i + 1), attachFiles.get(i));
|
||
|
}
|
||
|
msgList.add(msgEntity.copy());
|
||
|
}
|
||
|
JSONObject requestJo = JSONObject.create();
|
||
|
requestJo.put("msgList", msgList);
|
||
|
LogKit.info("idfh-PostJsonOutput-operation-requestJo:{}", requestJo.encode());
|
||
|
StringEntity stringEntity = new StringEntity(requestJo.encode(), "UTF-8");
|
||
|
String response = HttpKit.executeAndParse(com.fanruan.api.net.http.rs.HttpRequest.custom()
|
||
|
.url(outputHttp.getApiUrl()).post(stringEntity).headers(headers).build());
|
||
|
LogKit.info("idfh-PostJsonOutput-operation-response:{}", response);
|
||
|
sendTemplateMessage(HtmlUtils.htmlUnescape(response), PostJsonOutput.KEY);
|
||
|
}
|
||
|
|
||
|
private JSONObject createMsgEntity(Map<String, String> bodyParams, String bodyContent) {
|
||
|
JSONObject msgEntity = JSONObject.create();
|
||
|
msgEntity.put("srcSystem", bodyParams.get("srcSystem"));
|
||
|
msgEntity.put("keyword", bodyParams.get("keyword"));
|
||
|
msgEntity.put("dbSource", bodyParams.get("dbSource"));
|
||
|
msgEntity.put("target", bodyParams.get("target"));
|
||
|
msgEntity.put("msgContent", bodyContent);
|
||
|
msgEntity.put("createTime", DateUtils.getDate2LStr(new Date()));
|
||
|
bodyParams.remove("srcSystem");
|
||
|
bodyParams.remove("keyword");
|
||
|
bodyParams.remove("dbSource");
|
||
|
bodyParams.remove("target");
|
||
|
return msgEntity;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 消息内容加入图片HTML
|
||
|
*
|
||
|
* @param attachFiles
|
||
|
* @param msgEntity
|
||
|
*/
|
||
|
private void handleContent(List<String> attachFiles, JSONObject msgEntity) {
|
||
|
StringBuilder msgContent = new StringBuilder();
|
||
|
msgContent.append(msgEntity.getString("msgContent")).append("<br/>");
|
||
|
for (String file : attachFiles) {
|
||
|
if (file.endsWith(ScheduleConstants.Suffix.PNG)) {
|
||
|
msgContent.append("<img src=\"").append(file).append("\">");
|
||
|
}
|
||
|
}
|
||
|
msgEntity.put("msgContent", msgContent);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 上传附件操作
|
||
|
*
|
||
|
* @param taskParams
|
||
|
* @param url
|
||
|
* @param authorization
|
||
|
* @return
|
||
|
*/
|
||
|
private List<String> uploadFiles(Map<String, Object> taskParams, String url, String authorization) {
|
||
|
List<String> attachFiles = new ArrayList<>();
|
||
|
Map<String, String> headers = new HashMap<>();
|
||
|
headers.put("Authorization", authorization);
|
||
|
String[] outputFiles = (String[]) taskParams.get(ScheduleConstants.OUTPUT_FILES);
|
||
|
MultipartEntityBuilder builder;
|
||
|
String filePath;
|
||
|
File file;
|
||
|
FileBody fileBody;
|
||
|
HttpEntity httpEntity;
|
||
|
String response;
|
||
|
JSONObject result;
|
||
|
for (String outputFile : outputFiles) {
|
||
|
try {
|
||
|
LogKit.info("idfh-PostJsonOutput-uploadFiles-outputFile:{}", outputFile);
|
||
|
builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||
|
filePath = ResourceIOUtils.getRealPath(outputFile);
|
||
|
file = new File(filePath);
|
||
|
fileBody = new FileBody(file);
|
||
|
builder.addPart("file", fileBody);
|
||
|
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||
|
httpEntity = builder.setCharset(StandardCharsets.UTF_8).build();
|
||
|
response = HttpKit.uploadFile(url, httpEntity, StandardCharsets.UTF_8, headers, HttpRequestType.POST);
|
||
|
LogKit.info("idfh-PostJsonOutput-uploadFiles-response:{}", response);
|
||
|
result = new JSONObject(response);
|
||
|
if (ComparatorUtils.equals(result.getInt("code"), 0)) {
|
||
|
attachFiles.add(result.getString("data"));
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
return attachFiles;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取定时任务发送用户
|
||
|
*
|
||
|
* @param taskParams
|
||
|
* @return
|
||
|
*/
|
||
|
private String[] getUserNames(Map<String, Object> taskParams) {
|
||
|
String username = (String) taskParams.get(ScheduleConstants.USERNAME);
|
||
|
if (StringUtils.isNotEmpty(username)) {
|
||
|
return new String[]{username};
|
||
|
} else {
|
||
|
String[] usernames = (String[]) taskParams.get(ScheduleConstants.USERNAMES);
|
||
|
return usernames != null ? usernames : new String[0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取调用后续接口的凭证
|
||
|
*
|
||
|
* @param params
|
||
|
*/
|
||
|
private String getAPIToken(Map<String, String> params) throws Exception {
|
||
|
Map<String, String> bodyParams = new HashMap<>();
|
||
|
bodyParams.put(USERNAME, params.get(USERNAME));
|
||
|
bodyParams.put(VALIDATE_CODE, params.get(VALIDATE_CODE));
|
||
|
String bodyContent = JSONObject.mapFrom(bodyParams).encode();
|
||
|
StringEntity stringEntity = new StringEntity(bodyContent, "UTF-8");
|
||
|
Map<String, String> loginHeaders = new HashMap<>();
|
||
|
loginHeaders.put("Content-Type", "application/json");
|
||
|
String response = HttpKit.executeAndParse(com.fanruan.api.net.http.rs.HttpRequest.custom()
|
||
|
.url(params.get(LOGIN_URL)).post(stringEntity).headers(loginHeaders).build());
|
||
|
JSONObject result = new JSONObject(response);
|
||
|
if (ComparatorUtils.equals(result.getInt("code"), 0)) {
|
||
|
return result.getJSONObject("data").getString("apiToken");
|
||
|
}
|
||
|
return StringKit.EMPTY;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 接口返回结果发送消息
|
||
|
*
|
||
|
* @param response
|
||
|
*/
|
||
|
public static void sendTemplateMessage(String response, String type) {
|
||
|
String content = I18nKit.getLocText("Plugin-httpaction") + "-" +
|
||
|
type + "-" +
|
||
|
I18nKit.getLocText("Plugin-httpaction_Result_Message") +
|
||
|
response;
|
||
|
MessageService.getInstance().sendMessage2SupperRole(content, StringUtils.EMPTY, MessageUrlType.NONE);
|
||
|
}
|
||
|
}
|