Przeglądaj źródła

佳博打票机连通调试

liudongzhi 1 tydzień temu
rodzic
commit
01015e578e

+ 85 - 24
alien-store/src/main/java/shop/alien/store/service/impl/PrintService.java

@@ -8,24 +8,11 @@ import okhttp3.Request;
 import okhttp3.RequestBody;
 import okhttp3.Response;
 import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.http.HttpEntity;
-import org.apache.http.NameValuePair;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.config.RequestConfig;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.*;
-import org.apache.http.client.utils.URLEncodedUtils;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
 import org.springframework.stereotype.Service;
 import shop.alien.entity.store.PrinterDO;
 import shop.alien.store.util.Base64Util;
 import shop.alien.store.util.ShaUtil;
-
 import java.io.BufferedReader;
-import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
@@ -82,8 +69,8 @@ public class PrintService {
         String brand = printer.getBrand();
         if ("XINYE".equalsIgnoreCase(brand)) {
             return printXinYe(printer, content);
-        } else if ("JIAHANG".equalsIgnoreCase(brand)) {
-            return printJiaHang(printer, content);
+        } else if ("JIABO".equalsIgnoreCase(brand)) {
+            return printJiaBo(printer, content);
         } else if ("SHANGPENG".equalsIgnoreCase(brand)) {
             return printShangPeng(printer, content);
         }
@@ -112,15 +99,89 @@ public class PrintService {
         return post("https://open.xpyun.net/api/openapi/xprinter/print", req);
     }
 
-    // ==================== 佳航打印实现 ====================
-    private boolean printJiaHang(PrinterDO p, String content) {
-        JSONObject req = new JSONObject();
-        req.put("merchantId", p.getUserId());
-        req.put("deviceNo", p.getSn());
-        req.put("key", p.getApiKey());
-        req.put("printContent", Base64Util.encode(content));
-        req.put("copy", 1);
-        return post("http://api.jhprt.com/api/print", req);
+
+
+    // ==================== 佳博打印实现 ====================
+    private boolean printJiaBo(PrinterDO p, String content) {
+        String url = "http://api.poscom.cn/apisc/sendMsg";
+        String memberCode = p.getUserId(); // 假设 userId 是 memberCode
+        String apiKey = p.getApiKey();     // 假设 apiKey 是签名 Key
+        String deviceId = p.getSn();       // 打印机 SN 作为设备 ID
+
+        try {
+//            String reqTime = String.valueOf(System.currentTimeMillis());
+            String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
+            // 注意:佳博签名逻辑通常是 MD5(memberCode + deviceId + token + reqTime + key)
+
+            String signContent = memberCode + deviceId + "" + reqTime + apiKey;
+          String securityCode = DigestUtils.md5Hex(signContent);
+
+//            String securityCode = DigestUtils.md5Hex("4554A0DBFCB544F73393C6D6FE251E76" + "dmems3mhexv" + "" + reqTime + "MDH6X7DG4H0REWUFNUPUKGIUGPMN9RO1");
+            Map<String, String> params = new HashMap<>();
+            params.put("reqTime", reqTime);
+            params.put("securityCode", securityCode);
+            params.put("memberCode", memberCode);
+            params.put("deviceID", deviceId); // 必须传入设备 ID
+            params.put("token", "");          // 如果有 token 请填入
+            params.put("mode", "2");          // 2 通常代表云打印模式
+            params.put("msgDetail", content); // 传入实际打印内容
+            params.put("cmdType", "ESC");     // 指令类型
+            params.put("charset", "4");       // 字符集:
+//            params.put("msgNo", String.valueOf(System.currentTimeMillis())); // 唯一流水号
+            params.put("times", "");         // 打印份数
+            params.put("reprint", "0");       // 0-不重打
+            return sendJiaBoPost(url, params);
+
+        } catch (Exception e) {
+            log.error("佳博打印异常: deviceId={}, error={}", deviceId, e.getMessage(), e);
+            return false;
+        }
+
+    }
+
+    // 佳博专用 POST 请求 (Form 表单格式)
+    private boolean sendJiaBoPost(String urlStr, Map<String, String> params) {
+        HttpURLConnection conn = null;
+        try {
+            URL url = new URL(urlStr);
+            conn = (HttpURLConnection) url.openConnection();
+            conn.setRequestMethod("POST");
+            conn.setDoOutput(true);
+            conn.setConnectTimeout(5000);
+            conn.setReadTimeout(10000);
+            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
+
+            StringBuilder sb = new StringBuilder();
+            for (Map.Entry<String, String> entry : params.entrySet()) {
+                if (sb.length() > 0) sb.append("&");
+                sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
+            }
+
+            try (OutputStream os = conn.getOutputStream()) {
+                os.write(sb.toString().getBytes("UTF-8"));
+            }
+
+            int code = conn.getResponseCode();
+            if (code == 200) {
+                try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
+                    String line;
+                    StringBuilder res = new StringBuilder();
+                    while ((line = br.readLine()) != null) res.append(line);
+                    
+                    log.info("佳博打印响应: {}", res);
+                    // 佳博成功判断:通常包含 "result":0 或 "success"
+                    return res.toString().contains("\"result\":0") || res.toString().contains("\"err\":0");
+                }
+            } else {
+                log.error("佳博打印请求失败,HTTP 状态码: {}", code);
+                return false;
+            }
+        } catch (Exception e) {
+            log.error("佳博打印网络异常: error={}", e.getMessage(), e);
+            return false;
+        } finally {
+            if (conn != null) conn.disconnect();
+        }
     }
 
     // ==================== 商鹏打印实现 (V1 官方签名版) ====================