From 52a7c9b093bcb05ee128b3ad8338007aad2cfccb Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期二, 27 一月 2026 17:09:26 +0800
Subject: [PATCH] 业务管理:问题修复
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/api/MesApiUtils.java | 45 +++++++++++++++++++++++++++------------------
1 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/api/MesApiUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/api/MesApiUtils.java
index 98f2f2f..72d7fe5 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/api/MesApiUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/api/MesApiUtils.java
@@ -3,7 +3,11 @@
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
+import com.ruoyi.common.config.mes.MesConfig;
+import com.ruoyi.common.config.mes.MesProperties;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -15,20 +19,23 @@
@Component
public class MesApiUtils {
- @Value("${mes.ztzb.ip}")
- String ip;
+ @Autowired
+ private MesConfig mesConfig;
- @Value("${mes.ztzb.user}")
- String user;
-
- @Value("${mes.ztzb.password}")
- String password;
+ /**
+ * 鏍规嵁宸ュ巶鍩熷洖鍘籱es閰嶇疆
+ * @param contract
+ * @return
+ */
+ public MesProperties getMesPropertiesByContract(String contract){
+ return mesConfig.getProps().stream().filter(f-> org.apache.commons.lang3.StringUtils.equals(f.getContract(),contract)).findFirst().orElse(new MesProperties());
+ }
/**
* 鑾峰彇token璇锋眰閾炬帴
* @return
*/
- private String getAuthTokenUrl() {
+ private String getAuthTokenUrl(String ip) {
return ip + "/auth/oauth/token?randomStr=blockPuzzle&grant_type=password";
}
@@ -36,7 +43,7 @@
* 鏂板瀹炴椂搴撳瓨璇锋眰閾炬帴
* @return
*/
- private String getBatchAddStockUrl(){
+ private String getBatchAddStockUrl(String ip){
return ip + "/mes/stock/batchAddStock";
}
@@ -44,12 +51,12 @@
* 鑾峰彇mes绯荤粺token
* @return 鎺ュ彛鍝嶅簲缁撴灉
*/
- public String getToken(){
+ public String getToken(MesProperties mesProperties){
try{
Map<String,Object> bodyMap = new HashMap<>();
- bodyMap.put("username",user);
- String bodyStr = "username="+user+"&password="+password;
- String response = HttpRequest.post(getAuthTokenUrl())
+ bodyMap.put("username",mesProperties.getUser());
+ String bodyStr = "username="+mesProperties.getUser()+"&password="+mesProperties.getPassword();
+ String response = HttpRequest.post(getAuthTokenUrl(mesProperties.getIp()))
.header("Authorization", "Basic cGlnOnBpZw==")
.header("Content-Type", "application/x-www-form-urlencoded")
.body(bodyStr,"application/x-www-form-urlencoded").execute().body();
@@ -67,13 +74,15 @@
/**
* mes鏂板瀹炴椂搴撳瓨鎺ュ彛
+ * @param contract 宸ュ巶鍩�
* @param requestJsonStr 璇锋眰鍙傛暟json瀛楃涓�
* @return 鎺ュ彛鍝嶅簲缁撴灉
*/
- public boolean batchAddStock(String requestJsonStr){
+ public boolean batchAddStock(String contract,String requestJsonStr){
try{
- String response = HttpRequest.post(getBatchAddStockUrl())
- .header("Authorization", getToken())
+ MesProperties mesProperties = getMesPropertiesByContract(contract);
+ String response = HttpRequest.post(getBatchAddStockUrl(mesProperties.getIp()))
+ .header("Authorization", getToken(mesProperties))
.header("Content-Type", "application/json")
.body(requestJsonStr)
.execute()
@@ -82,11 +91,11 @@
if(entries.getInt("code")==0){
return true;
}else{
- throw new RuntimeException("鍚屾鍒癕ES澶辫触锛�"+entries.getStr("msg"));
+ throw new RuntimeException("銆�"+contract+"銆戝悓姝ュ埌MES澶辫触锛�"+entries.getStr("msg"));
}
}catch (Exception e){
e.printStackTrace();
- throw new RuntimeException("鍚屾MES瀹炴椂搴撳瓨鎺ュ彛寮傚父:"+e.getMessage());
+ throw new RuntimeException("銆�"+contract+"銆戝悓姝ES瀹炴椂搴撳瓨鎺ュ彛寮傚父:"+e.getMessage());
}
}
--
Gitblit v1.9.3