U9cAuthUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.jeesite.modules.mes.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jeesite.common.lang.StringUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.io.BufferedReader;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import java.nio.charset.StandardCharsets;
  13. /**
  14. * U9C OAuth2 登录工具类
  15. */
  16. public class U9cAuthUtil {
  17. private static final Logger log = LoggerFactory.getLogger(U9cAuthUtil.class);
  18. private static final String AUTH_URL = "http://10.0.10.11/u9c/webapi/OAuth2/AuthLogin"
  19. + "?clientid=mes"
  20. + "&clientsecret=4cb93d6876d0484a9e4d3549b80cf5a1"
  21. + "&entCode=100"
  22. + "&userCode=yonyou"
  23. + "&orgCode=100";
  24. private static final String CREATE_RCV_URL = "http://10.0.10.11/u9c/webapi/CustLJRMESOpenAPI/CreateRCV";
  25. private U9cAuthUtil() {
  26. }
  27. /**
  28. * 调用 U9C OAuth2 登录接口,获取 token
  29. *
  30. * @return 登录成功返回 token,失败返回 null
  31. */
  32. public static String getToken() {
  33. JSONObject response = authLogin();
  34. if (response == null) {
  35. return null;
  36. }
  37. Boolean success = response.getBoolean("Success");
  38. if (success != null && success) {
  39. return response.getString("Data");
  40. }
  41. log.warn("U9C 登录失败: {}", response.getString("ResMsg"));
  42. return null;
  43. }
  44. /**
  45. * 调用 CreateRCV 接口,自动获取 token 并写入请求头
  46. *
  47. * @param requestBody JSON 请求体
  48. * @return 接口响应,失败返回 null
  49. */
  50. public static JSONObject createRCV(String requestBody) {
  51. String token = getToken();
  52. if (StringUtils.isEmpty(token)) {
  53. log.warn("CreateRCV 请求失败: 无法获取 token");
  54. return null;
  55. }
  56. return postWithToken(CREATE_RCV_URL, requestBody, token);
  57. }
  58. /**
  59. * 调用 CreateRCV 接口,自动获取 token 并写入请求头
  60. *
  61. * @param requestBody JSON 请求体
  62. * @return 接口响应,失败返回 null
  63. */
  64. public static JSONObject createRCV(JSONObject requestBody) {
  65. return createRCV(requestBody == null ? "{}" : requestBody.toJSONString());
  66. }
  67. /**
  68. * 调用 U9C OAuth2 登录接口,返回完整响应
  69. */
  70. public static JSONObject authLogin() {
  71. HttpURLConnection connection = null;
  72. try {
  73. URL url = new URL(AUTH_URL);
  74. connection = (HttpURLConnection) url.openConnection();
  75. connection.setRequestMethod("GET");
  76. connection.setConnectTimeout(10000);
  77. connection.setReadTimeout(10000);
  78. connection.setDoInput(true);
  79. connection.setUseCaches(false);
  80. int responseCode = connection.getResponseCode();
  81. if (responseCode != HttpURLConnection.HTTP_OK) {
  82. log.warn("U9C 登录请求失败, HTTP状态码: {}", responseCode);
  83. return null;
  84. }
  85. String body = readResponse(connection.getInputStream());
  86. return JSONObject.parseObject(body);
  87. } catch (Exception e) {
  88. log.error("U9C 登录请求异常", e);
  89. return null;
  90. } finally {
  91. if (connection != null) {
  92. connection.disconnect();
  93. }
  94. }
  95. }
  96. private static JSONObject postWithToken(String url, String requestBody, String token) {
  97. HttpURLConnection connection = null;
  98. try {
  99. connection = (HttpURLConnection) new URL(url).openConnection();
  100. connection.setRequestMethod("POST");
  101. connection.setConnectTimeout(10000);
  102. connection.setReadTimeout(30000);
  103. connection.setDoInput(true);
  104. connection.setDoOutput(true);
  105. connection.setUseCaches(false);
  106. connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  107. connection.setRequestProperty("token", token);
  108. byte[] bodyBytes = StringUtils.defaultString(requestBody).getBytes(StandardCharsets.UTF_8);
  109. try (OutputStream outputStream = connection.getOutputStream()) {
  110. outputStream.write(bodyBytes);
  111. outputStream.flush();
  112. }
  113. int responseCode = connection.getResponseCode();
  114. InputStream inputStream = responseCode == HttpURLConnection.HTTP_OK
  115. ? connection.getInputStream()
  116. : connection.getErrorStream();
  117. if (inputStream == null) {
  118. log.warn("CreateRCV 请求失败, HTTP状态码: {}", responseCode);
  119. return null;
  120. }
  121. String body = readResponse(inputStream);
  122. if (responseCode != HttpURLConnection.HTTP_OK) {
  123. log.warn("CreateRCV 请求失败, HTTP状态码: {}, 响应: {}", responseCode, body);
  124. return null;
  125. }
  126. return JSONObject.parseObject(body);
  127. } catch (Exception e) {
  128. log.error("CreateRCV 请求异常", e);
  129. return null;
  130. } finally {
  131. if (connection != null) {
  132. connection.disconnect();
  133. }
  134. }
  135. }
  136. private static String readResponse(InputStream inputStream) throws Exception {
  137. StringBuilder result = new StringBuilder();
  138. try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
  139. String line;
  140. while ((line = reader.readLine()) != null) {
  141. result.append(line);
  142. }
  143. }
  144. return result.toString();
  145. }
  146. }