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 DeviceThumbnailQuery { @Value("${qianliyan.config.appId}") private String appId; @Value("${qianliyan.config.deviceThumbnailUrl}") private String deviceThumbnailUrl; @Value("${qianliyan.config.privateKey}") private String privateKey; private static final String TOKEN_KEY = "token_key:"; @Autowired private RedisService redisService; @Autowired private TokenFetcher tokenFetcher; public String getDeviceThumbnailUrl(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("deviceId", 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(deviceThumbnailUrl) .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"); String url = data.getString("url"); return url; } else { log.error("获取缩略图失败"); } } catch (Exception e) { e.printStackTrace(); } return null; } }