编辑代码

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class TQDataProcessor {

    private static final String ADMIN_UIN = "9690838";
    private static final String KEY = "bjtiandijinghua-key";
    private static final String TOKEN_URL = "http://webservice.edu.tq.cn/webservice/getAccessToken";
    private static final String BUSINESS_URL_TEMPLATE = "http://webservice.edu.tq.cn/webservice/phoneRecord/list?access_token=%s&admin_uin=%s&pageSize=2000&pageNum=1&id=&start_time=%d&end_time=%d";

    public static void main(String[] args) {
        try {
            // 获取当前时间戳
            long currentTimestamp = getCurrentTimestamp();

            // 生成签名
            String sign = generateSign(currentTimestamp);

            // 获取 access_token
            String accessToken = getAccessToken(currentTimestamp, sign);

            // 动态计算时间范围
            long startTime = getStartOfDayTimestamp(); // 当天 00:00:00
            long endTime = getEndOfDayTimestamp();     // 当天 14:59:59

            // 获取业务数据
            String businessData = getBusinessData(accessToken, startTime, endTime);

            // 将业务数据保存为 JSON 文件
            saveJsonToFile(businessData, "business_data.json");

            // 打印成功信息
            System.out.println("JSON 文件已生成:business_data.json");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取当前时间戳
    private static long getCurrentTimestamp() {
        return System.currentTimeMillis() / 1000;
    }

    // 生成签名
    private static String generateSign(long timestamp) throws NoSuchAlgorithmException {
        String input = ADMIN_UIN + "$" + KEY + "$" + timestamp;
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    // 获取 access_token
    private static String getAccessToken(long timestamp, String sign) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(TOKEN_URL);

        // 设置请求体
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode requestBody = objectMapper.createObjectNode();
        requestBody.put("admin_uin", ADMIN_UIN);
        requestBody.put("ctime", timestamp);
        requestBody.put("sign", sign);

        StringEntity entity = new StringEntity(requestBody.toString(), StandardCharsets.UTF_8);
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json");

        // 发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();
        String responseString = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);

        // 解析响应
        ObjectNode responseJson = objectMapper.readValue(responseString, ObjectNode.class);
        return responseJson.get("access_token").asText();
    }

    // 获取业务数据
    private static String getBusinessData(String accessToken, long startTime, long endTime) throws IOException {
        String businessUrl = String.format(BUSINESS_URL_TEMPLATE, accessToken, ADMIN_UIN, startTime, endTime);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(businessUrl);

        // 发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();
        return EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
    }

    // 将字符串保存为 JSON 文件
    private static void saveJsonToFile(String json, String fileName) throws IOException {
        try (FileWriter fileWriter = new FileWriter(fileName)) {
            fileWriter.write(json);
        }
    }

    // 获取当天 00:00:00 的时间戳
    private static long getStartOfDayTimestamp() {
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = today.atStartOfDay(); // 当天 00:00:00
        return startOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
    }

    // 获取当天 14:59:59 的时间戳
    private static long getEndOfDayTimestamp() {
        LocalDate today = LocalDate.now();
        LocalDateTime endOfDay = today.atTime(14, 59, 59); // 当天 14:59:59
        return endOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
    }
}