xuhy
2026-03-11 67bf2fb3c479e791d008d7bbf7ad9f88adca6a44
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
package com.ruoyi.web.controller.tool.monitor;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
/**
 * 设备查询封装
 */
@Slf4j
@Component
public class DeviceQuery {
 
    @Value("${qianliyan.config.appId}")
    private String appId;
    @Value("${qianliyan.config.deviceUrl}")
    private String deviceUrl;
    @Value("${qianliyan.config.privateKey}")
    private String privateKey;
    
    private static final String TOKEN_KEY = "token_key:";
 
    @Autowired
    private RedisService redisService;
    @Autowired
    private TokenFetcher tokenFetcher;
    @Autowired
    private DeviceThumbnailQuery deviceThumbnailQuery;
 
    public JSONObject getDeviceList(Integer page, Integer pageSize,Integer onlineStatus) {
        try {
            String token = redisService.getCacheObject(TOKEN_KEY);
            if(!org.springframework.util.StringUtils.hasLength(token)){
                token = tokenFetcher.getToken();
            }
 
            String timestamp = String.valueOf(System.currentTimeMillis());
 
            JSONObject requestBody = new JSONObject();
            requestBody.put("page", page);
            requestBody.put("pageSize", pageSize);
            requestBody.put("onlineStatus", onlineStatus);
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            Map signParams = new LinkedHashMap();
            signParams.put("appid", appId);
            signParams.put("md5", md5);
            signParams.put("timestamp", timestamp);
            signParams.put("token", token);
            signParams.put("version", "1.0.0");
            String jsonString = JSONUtil.toJsonStr(signParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(privateKey);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            // 5. 发送请求
            String response = HttpRequest.post(deviceUrl)
                    .header("content-type", "application/json")
                    .header("appid", appId)
                    .header("md5", md5)
                    .header("timestamp", timestamp)
                    .header("version", "1.0.0")
                    .header("signature", signatureString)
                    .header("token", token)
                    .body(requestBodyStr)
                    .execute().body();
            System.err.println("Response: " + response);
 
            JSONObject jsonObject = JSONObject.parseObject(response);
            if ("000000".equals(jsonObject.getString("resultCode"))) {
                log.info("获取列表成功");
                JSONArray data = jsonObject.getJSONArray("data");
 
                for (int i = 0; i < data.size(); i++) {
                    JSONObject item = data.getJSONObject(i);
                    String deviceId = item.getString("deviceId");
                    // 查询缩略图
                    String deviceThumbnailUrl = deviceThumbnailQuery.getDeviceThumbnailUrl(deviceId);
                    item.put("deviceThumbnailUrl", deviceThumbnailUrl);
                }
                return jsonObject;
            } else {
                log.error("获取列表失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("resultCode", "000000");
        jsonObject1.put("resultMsg", "成功");
        jsonObject1.put("data", new JSONArray());
        jsonObject1.put("total", 0);
        jsonObject1.put("page", 1);
        jsonObject1.put("pageSize", 10);
        return jsonObject1;
    }
 
}