diff --git a/ic/src/private/nc/bs/ic/HttpPostOtherSysImpl.java b/ic/src/private/nc/bs/ic/HttpPostOtherSysImpl.java new file mode 100644 index 0000000..0f2fd70 --- /dev/null +++ b/ic/src/private/nc/bs/ic/HttpPostOtherSysImpl.java @@ -0,0 +1,89 @@ +package nc.bs.ic; + +import com.alibaba.fastjson.JSONObject; +import nc.IHttpPostOtherSys; +import nc.hr.utils.PubEnv; +import nc.itf.scmpub.reference.uap.para.SysParaInitQuery; +import nc.pub.fa.common.util.StringUtils; +import nc.vo.pubapp.pattern.exception.ExceptionUtils; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +public class HttpPostOtherSysImpl implements IHttpPostOtherSys { + + @Override + public String callLE(String url, JSONObject json) { + String leip = SysParaInitQuery.getParaString(PubEnv.getPk_group(), "LEIP"); +// String leip = "http://60.214.115.166:20003"; + String baseurl = leip + url; + String token = this.getLEToken(leip); + Map headers = new HashMap<>(); + headers.put("x-token", token); + String postResult = doPost(baseurl, headers, json); + JSONObject result = JSONObject.parseObject(postResult); + String code = result.getString("code"); + if (StringUtils.equals(code, "0")) { + return postResult; + } else { + String message = result.getString("message"); + ExceptionUtils.wrappBusinessException(message); + return message; + } + } + + private String getLEToken(String leip) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("username", "nc_le"); + jsonObject.put("password", "7qTx8aPxVQ3n3j3HaKxZvLepyCyNm6gk967V7qcmJZkfBbJFEQZqPdQJMgfQNWdQ"); + String result = doPost(leip + "/login", null, jsonObject); + JSONObject parsed = JSONObject.parseObject(result); + String token = parsed.getString("token"); + return token; + } + + private String doPost(String baseurl, Map map, JSONObject json) { + BufferedReader reader = null; + try { + URL url = new URL(baseurl);// 创建连接 + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setUseCaches(false); + connection.setInstanceFollowRedirects(true); + connection.setRequestMethod("POST"); // 设置请求方式 + // 设置接收数据的格式 + connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式 + if (map != null) { + for (String key : map.keySet()) { + connection.setRequestProperty(key, map.get(key)); + } + } + connection.connect(); + // 一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的 + OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // UTF-8编码 + if (json != null) { + out.append(json.toString()); + } + out.flush(); + out.close(); + // 读取响应 + reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + String line; + String res = ""; + while ((line = reader.readLine()) != null) { + res += line; + } + reader.close(); + return res; + } catch (Exception e) { +// e.printStackTrace(); + throw new RuntimeException(e); + } + } +} diff --git a/ic/src/public/nc/IHttpPostOtherSys.java b/ic/src/public/nc/IHttpPostOtherSys.java new file mode 100644 index 0000000..0c51069 --- /dev/null +++ b/ic/src/public/nc/IHttpPostOtherSys.java @@ -0,0 +1,15 @@ +package nc; + +import com.alibaba.fastjson.JSONObject; + + +public interface IHttpPostOtherSys { + + /** + * xuwjl@yonyou.com 2025年4月24日 15:24:14 + * @param url LE的接口地址,不包含ip + * @param json 入参 + */ + public String callLE(String url, JSONObject json); + +}