package com.weaver.secondev.util; import com.alibaba.fastjson.JSONObject; import cn.hutool.http.HttpRequest; import com.weaver.verupgrade.workflow.request.RequestManager; import lombok.extern.slf4j.Slf4j; import okhttp3.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; /** * @Author: calyrex * @CreateTime: 2025-06-03 * @Description: 用友三方接口获取工具 */ @Component @Slf4j public class ThirdInfoUtil { @Autowired private DataOperateUtil dataOperateUtil; public JSONObject getToken(){ String ipAddress = dataOperateUtil.getConfig("ip_address"); String bizCenter = dataOperateUtil.getConfig("biz_center"); String grantType = dataOperateUtil.getConfig("grant_type"); String clientId = dataOperateUtil.getConfig("client_id"); String clientSecret = dataOperateUtil.getConfig("client_secret"); String signature = dataOperateUtil.getConfig("signature"); String url = ipAddress + "/nccloud/opm/accesstoken?grant_type=" + grantType + "&client_id=" + clientId + "&client_secret=" + clientSecret + "&signature=" + signature + "&biz_center=" + bizCenter; String body = HttpRequest.post(url) .header("Content-Type", "application/x-www-form-urlencoded") .execute().body(); JSONObject result = JSONObject.parseObject(body); return result; } public Boolean transferStatusInfo(String url, String token, JSONObject body){ String request = HttpRequest.post(url) .header("Content-Type", "application/json") .header("client_id", dataOperateUtil.getConfig("client_id")) .header("signature", dataOperateUtil.getConfig("signature")) .header("access_token", token) .header("ucg_flag", "Y") .header("repeat_check", "y") .body(String.valueOf(body)) .execute().body(); log.info("transferInfo request:{}", request); boolean result = Boolean.parseBoolean(JSONObject.parseObject(request).get("success").toString()); return result; } public JSONObject transferInfo(String url, String token, JSONObject body){ String request = HttpRequest.post(url) .header("Content-Type", "application/json") .header("client_id", dataOperateUtil.getConfig("client_id")) .header("signature", dataOperateUtil.getConfig("signature")) .header("access_token", token) .header("ucg_flag", "Y") .header("repeat_check", "y") .body(String.valueOf(body)) .execute().body(); log.info("transferInfo request:{}", request); return JSONObject.parseObject(request); } public Boolean updateFile(String parentPath,String imageFileName, InputStream inputStream) throws IOException { OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("text/plain"); String url = dataOperateUtil.null2String(dataOperateUtil.getConfig("oa_address")) + "/service/FileUpLoad"; RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("file1",imageFileName, // RequestBody.create(MediaType.parse("application/octet-stream"), new File("D:\\works\\fwsz\\管委会\\test.pdf"))) RequestBody.create(MediaType.parse("application/octet-stream"), toByteArray(inputStream))) .build(); Request request = new Request.Builder() .url(url) .header("operType","upload") .header("dsName",dataOperateUtil.getConfig("dsName")) .header("parentPath",parentPath) .method("POST", body) .build(); Response response = client.newCall(request).execute(); String string = response.body().string(); JSONObject result = JSONObject.parseObject(string); return result.getBoolean("success"); } public Map getResultMapForAction(Map map, String key, Object value, RequestManager requestManager) { if (key != null && !key.isEmpty()) { map.put(key, value); } String msgContent = requestManager.getMessagecontent(); if (msgContent != null && !msgContent.isEmpty()) { map.put("msgContent", msgContent); } String msgId = requestManager.getMessageid(); if (msgId != null && !msgId.isEmpty()) { map.put("msgId", msgId); } return map; } public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024*4]; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } return output.toByteArray(); } }