xuhy
2026-03-10 b9b3787b2cb651d550a34e955e7c3037e8a1ffd1
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
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.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.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
 
/**
 * 设备直播链接查询封装
 */
@Slf4j
@Component
public class DeviceLiveQuery {
 
    @Value("${qianliyan.config.appId}")
    private String appId;
    @Value("${qianliyan.config.deviceLiveUrl}")
    private String deviceLiveUrl;
    @Value("${qianliyan.config.privateKey}")
    private String privateKey;
    private static final String TOKEN_KEY = "token_key:";
 
    @Autowired
    private RedisService redisService;
    @Autowired
    private TokenFetcher tokenFetcher;
 
    public JSONObject getDeviceLiveList(String deviceId) {
        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", deviceId);
            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(deviceLiveUrl)
                    .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("获取直播链接成功");
                JSONObject data = jsonObject.getJSONObject("data");
                return data;
            } else {
                log.error("获取直播链接失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("expiresIn", 0);
        jsonObject1.put("url", "");
        return jsonObject1;
    }
 
}