From 9905847295d326134d50fe7f005522cdfafe2763 Mon Sep 17 00:00:00 2001
From: lty <876263681@qq.com>
Date: 星期五, 07 十一月 2025 16:52:27 +0800
Subject: [PATCH] #erp商品档案同步

---
 src/main/java/com/zy/asrs/task/kingdee/handler/LoginAuthenticationHandler.java |  129 ++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/zy/asrs/task/kingdee/handler/LoginAuthenticationHandler.java b/src/main/java/com/zy/asrs/task/kingdee/handler/LoginAuthenticationHandler.java
index 3530b48..74a5da9 100644
--- a/src/main/java/com/zy/asrs/task/kingdee/handler/LoginAuthenticationHandler.java
+++ b/src/main/java/com/zy/asrs/task/kingdee/handler/LoginAuthenticationHandler.java
@@ -3,10 +3,13 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.core.common.Cools;
 import com.core.exception.CoolException;
-import com.zy.asrs.entity.Order;
+import com.zy.asrs.entity.ErpSecret;
+import com.zy.asrs.entity.WrkMast;
 import com.zy.asrs.service.ApiLogService;
+import com.zy.asrs.service.impl.ErpSecretServiceImpl;
 import com.zy.asrs.task.AbstractHandler;
 import com.zy.asrs.task.core.ReturnT;
 import com.zy.common.utils.HttpHandler;
@@ -18,6 +21,8 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 @Service
@@ -33,24 +38,27 @@
     //鐧诲綍鍦板潃
     private String loginaddress;
 
-    @Value("${erp.login.acctID}")
-    //璐﹀彿ID
-    private String acctID;
+    @Value("${erp.login.clientId}")
+    //WMS绔疘D
+    private String clientId;
+
+    @Value("${erp.login.clientSecret}")
+    //璐﹀彿瀵嗛挜
+    private String clientSecret;
 
     @Value("${erp.login.username}")
-    //璐﹀彿
     private String username;
 
-    @Value("${erp.login.password}")
-    //瀵嗙爜
-    private String password;
+    @Value("${erp.login.accountId}")
+    private String accountId;
 
-    @Value("${erp.login.lcid}")
-    //
-    private Integer lcid;
+    @Value("${erp.login.xAcfwIdentity}")
+    private String xAcfwIdentity;
 
     @Autowired
     private ApiLogService apiLogService;
+    @Autowired
+    private ErpSecretServiceImpl erpSecretService;
 
     /**
      * 45鍒嗛挓鐧诲綍涓�娆¢噾铦剁郴缁�
@@ -58,57 +66,68 @@
      */
     @Transactional
     public synchronized ReturnT<String> start() {
+        ErpSecret erpSecret = erpSecretService.selectOne(new EntityWrapper<ErpSecret>().eq("account_id", accountId));
+        String time = String.valueOf(System.currentTimeMillis());
+        // 鍒涘缓璇锋眰鍙傛暟
+        JSONObject requestPayload = new JSONObject();
+        requestPayload.put("client_id", clientId);
+        requestPayload.put("client_secret", clientSecret);
+        requestPayload.put("username", username);
+        requestPayload.put("accountId", accountId);
+        requestPayload.put("nonce", erpSecret.getRefreshToken());//鍒锋柊token
+        requestPayload.put("timestamp", time);
+        requestPayload.put("language", "zh_CN");
 
-        LoginParam loginParam =new LoginParam();
-        loginParam.setAcctID(acctID);
-        loginParam.setUsername(username);
-        loginParam.setPassword(password);
-        loginParam.setLcid(lcid);
-        //涓婃姤
+        // 鍒涘缓璇锋眰澶�
+        Map<String, Object> headers = new HashMap<>();
+        headers.put("x-acgw-identity", xAcfwIdentity);  // 鑷畾涔夎姹傚ご
+        headers.put("Content-Type", "application/json;charset=UTF-8");
         String response = "";
-        String cookie = "";
         boolean success = false;
+        String accessToken = "";
         try {
+            // 浣跨敤 HttpHandler 鍙戣捣璇锋眰
             response = new HttpHandler.Builder()
                     .setUri(URL)
-                    .setPath(loginaddress)
-                    .setJson(JSON.toJSONString(loginParam))
+                    .setPath(loginaddress)  // 鐧诲綍鎺ュ彛璺緞
+                    .setJson(requestPayload.toJSONString())  // 璇锋眰浣�
+                    .setHeaders(headers)  // 璁剧疆璇锋眰澶�
                     .build()
-                    .doPost();
-            JSONObject jsonObject = JSON.parseObject(response);
-            Object sessionId = findValueByKey(JSON.parseObject(response), "SessionId");
-            cookie="ASP.NET_SessionId="+sessionId+";"+"kdservice-sessionid="+jsonObject.getString("KDSVCSessionId");
-            if (Cools.isEmpty(jsonObject.getString("Message"))) {
+                    .doPost();  // 鎵цPOST璇锋眰
+            // 瑙f瀽杩斿洖缁撴灉
+            JSONObject jsonResponse = JSON.parseObject(response);
+            // 鍒ゆ柇璇锋眰鏄惁鎴愬姛
+            if (jsonResponse.getBooleanValue("status")) {
+                accessToken = jsonResponse.getJSONObject("data").getString("access_token");
+                String refreshToken = jsonResponse.getJSONObject("data").getString("refresh_token");
                 success = true;
+                erpSecret.setRefreshToken(refreshToken);
+                erpSecret.setAccessToken(accessToken);
+                if(erpSecretService.saveErpSecret(erpSecret)){
+                    log.info("鑾峰彇鏇存柊Token鎴愬姛: {}", accessToken);
+                }
             } else {
-                callApiLogSaveLog(JSON.toJSONString(loginParam),false);
-                log.error("璇锋眰鎺ュ彛澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", URL+loginaddress, JSON.toJSONString(loginParam), response);
-//                throw new CoolException("鐧诲綍閲戣澏澶辫触");
+                log.error("鑾峰彇Token澶辫触锛岄敊璇俊鎭細{}", jsonResponse.getString("message"));
+                callApiLogSaveLog(requestPayload.toJSONString(), false);
             }
+
         } catch (Exception e) {
-            log.error("fail", e);
-            callApiLogSaveLog(JSON.toJSONString(loginParam),false);
-            log.error("璇锋眰鎺ュ彛澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", URL+loginaddress, JSON.toJSONString(loginParam), response);
-//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            log.error("鑾峰彇Token澶辫触锛屽紓甯镐俊鎭細", e);
+            callApiLogSaveLog(requestPayload.toJSONString(), false);
             return FAIL.setMsg(e.getMessage());
-        } finally {
-//            try {
-//                // 淇濆瓨鎺ュ彛鏃ュ織
-//                apiLogService.save(
-//                        "鐧诲綍閲戣澏",
-//                        URL + loginaddress,
-//                        null,
-//                        "127.0.0.1",
-//                        JSON.toJSONString(loginParam),
-//                        response,
-//                        success
-//                );
-//            } catch (Exception e) { log.error("", e); }
         }
-        return SUCCESS.setContent(cookie);
+
+        if (!success) {
+            return FAIL.setMsg("Token鑾峰彇澶辫触");
+        }
+
+        return SUCCESS.setContent(accessToken);
     }
 
 
+    /**
+     * 閫掑綊鏌ユ壘JSON涓殑鎸囧畾瀛楁
+     */
     public static Object findValueByKey(JSONObject json, String key) {
         Set<String> keySet = json.keySet();
         for (String k : keySet) {
@@ -117,15 +136,15 @@
                 return v;
             } else if (v instanceof JSONArray) {
                 int size = ((JSONArray) v).size();
-                for (int i = 0; i <= size - 1; i++) {
+                for (int i = 0; i < size; i++) {
                     Object result = findValueByKey((JSONObject) ((JSONArray) v).get(i), key);
                     if (result != null){
                         return result;
                     }
                 }
-            } else if (v instanceof JSONObject){
+            } else if (v instanceof JSONObject) {
                 Object result = findValueByKey((JSONObject) v, key);
-                if (result != null){
+                if (result != null) {
                     return result;
                 }
             }
@@ -133,14 +152,12 @@
         return null;
     }
 
+    /**
+     * 淇濆瓨API鏃ュ織
+     */
     public void callApiLogSaveLog(String response, Boolean bool) {
-        apiLogService.save("鐧诲綍閲戣澏澶辫触", URL, "null", loginaddress,
-                "濂楄处ID锛�"+acctID+"銆佽处鍙凤細"+username+"銆佸瘑鐮侊細"+password,
+        apiLogService.save("鐧诲綍閲戣澏澶辫触", URL, "绔彛", loginaddress,
+                "璐﹀彿Id锛�"+accountId+"銆佺敤鎴风Id锛�"+clientId+"銆佺敤鎴峰瘑閽ワ細"+clientSecret+"銆乽sername锛�"+username+"銆乺efresh_token锛�",
                 response, bool);
     }
-
-
 }
-
-
-

--
Gitblit v1.9.1