liujie
2026-03-09 e1bbc0cf1d8bb72fbd41976712bb847d242f3671
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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<TicketListVo> getTicketList() throws Exception {
        ArrayList<TicketListVo> 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<String, Object> 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<String, Object> headMap = new HashMap<>();
        headMap.put("user_id", userId);
        headMap.put("method", method);
        headMap.put("timestamp", timestamp);
        headMap.put("sign", md5);
 
        // 5. 构建完整的请求消息
        HashMap<String, Object> 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<String, Object> 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<String, Object> headMap = new HashMap<>();
        headMap.put("user_id", userId);
        headMap.put("method", method);
        headMap.put("timestamp", timestamp);
        headMap.put("sign", md5);
 
        // 5. 构建完整的请求消息
        HashMap<String, Object> 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);
        }
    }
}