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; } }