package com.jeesite.modules.mes.util; import com.alibaba.fastjson.JSONObject; import com.jeesite.common.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; /** * U9C OAuth2 登录工具类 */ public class U9cAuthUtil { private static final Logger log = LoggerFactory.getLogger(U9cAuthUtil.class); private static final String AUTH_URL = "http://10.0.10.11/u9c/webapi/OAuth2/AuthLogin" + "?clientid=mes" + "&clientsecret=4cb93d6876d0484a9e4d3549b80cf5a1" + "&entCode=100" + "&userCode=yonyou" + "&orgCode=100"; private static final String CREATE_RCV_URL = "http://10.0.10.11/u9c/webapi/CustLJRMESOpenAPI/CreateRCV"; private U9cAuthUtil() { } /** * 调用 U9C OAuth2 登录接口,获取 token * * @return 登录成功返回 token,失败返回 null */ public static String getToken() { JSONObject response = authLogin(); if (response == null) { return null; } Boolean success = response.getBoolean("Success"); if (success != null && success) { return response.getString("Data"); } log.warn("U9C 登录失败: {}", response.getString("ResMsg")); return null; } /** * 调用 CreateRCV 接口,自动获取 token 并写入请求头 * * @param requestBody JSON 请求体 * @return 接口响应,失败返回 null */ public static JSONObject createRCV(String requestBody) { String token = getToken(); if (StringUtils.isEmpty(token)) { log.warn("CreateRCV 请求失败: 无法获取 token"); return null; } return postWithToken(CREATE_RCV_URL, requestBody, token); } /** * 调用 CreateRCV 接口,自动获取 token 并写入请求头 * * @param requestBody JSON 请求体 * @return 接口响应,失败返回 null */ public static JSONObject createRCV(JSONObject requestBody) { return createRCV(requestBody == null ? "{}" : requestBody.toJSONString()); } /** * 调用 U9C OAuth2 登录接口,返回完整响应 */ public static JSONObject authLogin() { HttpURLConnection connection = null; try { URL url = new URL(AUTH_URL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); connection.setDoInput(true); connection.setUseCaches(false); int responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { log.warn("U9C 登录请求失败, HTTP状态码: {}", responseCode); return null; } String body = readResponse(connection.getInputStream()); return JSONObject.parseObject(body); } catch (Exception e) { log.error("U9C 登录请求异常", e); return null; } finally { if (connection != null) { connection.disconnect(); } } } private static JSONObject postWithToken(String url, String requestBody, String token) { HttpURLConnection connection = null; try { connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(10000); connection.setReadTimeout(30000); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); connection.setRequestProperty("token", token); byte[] bodyBytes = StringUtils.defaultString(requestBody).getBytes(StandardCharsets.UTF_8); try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(bodyBytes); outputStream.flush(); } int responseCode = connection.getResponseCode(); InputStream inputStream = responseCode == HttpURLConnection.HTTP_OK ? connection.getInputStream() : connection.getErrorStream(); if (inputStream == null) { log.warn("CreateRCV 请求失败, HTTP状态码: {}", responseCode); return null; } String body = readResponse(inputStream); if (responseCode != HttpURLConnection.HTTP_OK) { log.warn("CreateRCV 请求失败, HTTP状态码: {}, 响应: {}", responseCode, body); return null; } return JSONObject.parseObject(body); } catch (Exception e) { log.error("CreateRCV 请求异常", e); return null; } finally { if (connection != null) { connection.disconnect(); } } } private static String readResponse(InputStream inputStream) throws Exception { StringBuilder result = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { result.append(line); } } return result.toString(); } }