package com.ruoyi.system.utils.ticketUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruoyi.system.dto.CreateOrderRequest; import com.ruoyi.system.vo.CreateOrderVo; import com.ruoyi.system.vo.TicketListVo; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; import java.util.List; /** * 门票接口 */ public class TicketUtil { public List getTicketList() throws Exception { ArrayList ticketListVos = new ArrayList<>(); String userId = "1d9a42e0-9de3-451b-9964-4e9f8e62b101"; String method = "getProductInfo"; String timestamp = System.currentTimeMillis() + ""; String desKey = "TFD9BEOK"; // DES密钥必须是8字节 String userKey = "TFD9BEOK"; // 用于签名的用户密钥 // 1. 构建请求体数据 HashMap bodyData = new HashMap<>(); bodyData.put("pageIndex", 1); bodyData.put("pageSize", 100); bodyData.put("sceneryNo", ""); String requestBody = JSON.toJSONString(bodyData); System.out.println("原始请求体: " + requestBody); // 2. 使用DES加密请求体 String encryptedBody = DesUtil.encrypt(requestBody, desKey); System.out.println("DES加密后的请求体: " + encryptedBody); // 3. 生成签名 String params = userId + method + timestamp + encryptedBody + userKey; String md5 = SecureUtil.md5(params); System.out.println("MD5签名: " + md5); // 4. 构建请求头 HashMap headMap = new HashMap<>(); headMap.put("user_id", userId); headMap.put("method", method); headMap.put("timestamp", timestamp); headMap.put("sign", md5); // 5. 构建完整的请求消息 HashMap fullRequestMap = new HashMap<>(); fullRequestMap.put("requestHead", headMap); fullRequestMap.put("requestBody", encryptedBody); // 6. 将完整消息转换为JSON字符串 String fullRequestJson = JSON.toJSONString(fullRequestMap); System.out.println("完整请求JSON: " + fullRequestJson); // 7. 对整个JSON字符串进行Base64编码 String base64EncodedRequest = Base64.getEncoder().encodeToString(fullRequestJson.getBytes("UTF-8")); System.out.println("Base64编码后的请求: " + base64EncodedRequest); // 8. 发送HTTP请求 HttpRequest post = HttpUtil.createPost("http://ota.huilvyun.com.cn:8086/scenic-ota/ota/v2/zshl/rent034/getProductInfo"); // 设置请求头 post.header("Content-Type", "text/plain;charset=UTF-8"); // 根据要求,可能是纯文本 // 或者如果是application/json,则使用: // post.header("Content-Type", "application/json;charset=UTF-8"); // 直接将Base64编码后的字符串作为body发送 post.body(base64EncodedRequest); System.out.println("发送请求..."); HttpResponse execute = post.execute(); // 9. 处理响应 System.out.println("响应状态码: " + execute.getStatus()); String responseBody = execute.body(); System.out.println("原始响应Body: " + responseBody); // 10. 解析响应(假设响应也是Base64编码的) if (responseBody != null && !responseBody.isEmpty()) { try { // 先Base64解码 byte[] decodedBytes = Base64.getDecoder().decode(responseBody); String decodedResponse = new String(decodedBytes, "UTF-8"); System.out.println("Base64解码后的响应: " + decodedResponse); String responseBody1 = JSONObject.parseObject(decodedResponse).get("responseBody").toString(); String decryptedResponseBody = DesUtil.decrypt(responseBody1, desKey); System.out.println("解密后的响应: " + decryptedResponseBody); JSONArray productList = JSONObject.parseObject(decryptedResponseBody).getJSONArray("productList"); for (int i = 0; i < productList.size(); i++) { JSONObject product = productList.getJSONObject(i); TicketListVo ticketListVo = new TicketListVo(); ticketListVo.setProductId(product.getString("productId")); ticketListVo.setProductName(product.getString("productName")); ticketListVo.setPrice(product.getLong("price")); ticketListVo.setSceneryNo(product.getString("sceneryNo")); ticketListVo.setSceneryName(product.getString("sceneryName")); ticketListVo.setBeginSaleDate(product.getString("beginSaleDate")); ticketListVo.setEndSaleDate(product.getString("endSaleDate")); ticketListVo.setIsCanRefund(product.getInteger("isCanRefund")); ticketListVos.add(ticketListVo); } return ticketListVos; } catch (IllegalArgumentException e) { System.out.println("响应不是有效的Base64格式: " + e.getMessage()); } } return ticketListVos; } public CreateOrderVo saveOrder(CreateOrderRequest request) throws Exception { String userId = "1d9a42e0-9de3-451b-9964-4e9f8e62b101"; String method = "createOrder"; String timestamp = System.currentTimeMillis() + ""; String desKey = "TFD9BEOK"; // DES密钥必须是8字节 String userKey = "TFD9BEOK"; // 用于签名的用户密钥 // 1. 构建请求体数据 // HashMap bodyData = new HashMap<>(); // bodyData.put("pageIndex", 1); // bodyData.put("pageSize", 100); // bodyData.put("sceneryNo", ""); String requestBody = JSON.toJSONString(request); System.out.println("原始请求体: " + requestBody); // 2. 使用DES加密请求体 String encryptedBody = DesUtil.encrypt(requestBody, desKey); System.out.println("DES加密后的请求体: " + encryptedBody); // 3. 生成签名 String params = userId + method + timestamp + encryptedBody + userKey; String md5 = SecureUtil.md5(params); System.out.println("MD5签名: " + md5); // 4. 构建请求头 HashMap headMap = new HashMap<>(); headMap.put("user_id", userId); headMap.put("method", method); headMap.put("timestamp", timestamp); headMap.put("sign", md5); // 5. 构建完整的请求消息 HashMap fullRequestMap = new HashMap<>(); fullRequestMap.put("requestHead", headMap); fullRequestMap.put("requestBody", encryptedBody); // 6. 将完整消息转换为JSON字符串 String fullRequestJson = JSON.toJSONString(fullRequestMap); System.out.println("完整请求JSON: " + fullRequestJson); // 7. 对整个JSON字符串进行Base64编码 String base64EncodedRequest = Base64.getEncoder().encodeToString(fullRequestJson.getBytes("UTF-8")); System.out.println("Base64编码后的请求: " + base64EncodedRequest); // 8. 发送HTTP请求 HttpRequest post = HttpUtil.createPost("http://ota.huilvyun.com.cn:8086/scenic-ota/ota/v2/zshl/rent034/createOrder"); // 设置请求头 post.header("Content-Type", "text/plain;charset=UTF-8"); // 根据要求,可能是纯文本 // 或者如果是application/json,则使用: // post.header("Content-Type", "application/json;charset=UTF-8"); // 直接将Base64编码后的字符串作为body发送 post.body(base64EncodedRequest); System.out.println("发送请求..."); HttpResponse execute = post.execute(); // 9. 处理响应 System.out.println("响应状态码: " + execute.getStatus()); String responseBody = execute.body(); System.out.println("原始响应Body: " + responseBody); // 10. 解析响应(假设响应也是Base64编码的) if (responseBody != null && !responseBody.isEmpty()) { try { // 先Base64解码 byte[] decodedBytes = Base64.getDecoder().decode(responseBody); String decodedResponse = new String(decodedBytes, "UTF-8"); System.out.println("Base64解码后的响应: " + decodedResponse); String res_code = JSONObject.parseObject(decodedResponse).get("res_code").toString(); if("1000".equals(res_code)){ String responseBody1 = JSONObject.parseObject(decodedResponse).get("responseBody").toString(); String decryptedResponseBody = DesUtil.decrypt(responseBody1, desKey); System.out.println("解密后的响应: " + decryptedResponseBody); JSONObject jsonObject = JSONObject.parseObject(decryptedResponseBody); CreateOrderVo createOrderVo = new CreateOrderVo(); createOrderVo.setOrderId(jsonObject.getString("orderId")); createOrderVo.setOrderAmount(jsonObject.getString("orderAmount")); createOrderVo.setPartnerOrderId(jsonObject.getString("partnerOrderId")); return createOrderVo; }else { String res_msg = JSONObject.parseObject(decodedResponse).get("res_msg").toString(); System.out.println("错误信息: " + res_msg); throw new RuntimeException(res_msg); } } catch (IllegalArgumentException e) { System.out.println("响应不是有效的Base64格式: " + e.getMessage()); } } return null; } public static void main(String[] args) { try { } catch (Exception e) { throw new RuntimeException(e); } } }