#!/usr/bin/env node const crypto = require("crypto"); // ==================== API配置 ==================== const ACCESS_ID = "XXXXXXXXX"; // 替换为您的ACCESS_ID const APP_SECRET = "XXXXXXXXXXXXXXXXXXXXXXX"; // 替换为您的app_secret const BASE_URL = 'https://api.tianyuanapi.com'; // ==================== 测试参数 ==================== const API_CODE = "FLXG0V4B"; // 替换为您的API编号 const PARAMS = { "name": "XXXX", "id_card": "XXXXXXXXXXXXXXX", "auth_date": "20250722-20250923" }; // ==================== 加密解密函数 ==================== function encrypt_data(data) { const key = Buffer.from(APP_SECRET, "hex"); const iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv("aes-128-cbc", key, iv); cipher.setAutoPadding(true); let encrypted = cipher.update(data, 'utf8'); encrypted = Buffer.concat([iv, encrypted, cipher.final()]); return encrypted.toString("base64"); } function decrypt_data(encrypted_data) { const key = Buffer.from(APP_SECRET, "hex"); const encryptedBuffer = Buffer.from(encrypted_data, "base64"); const iv = encryptedBuffer.slice(0, 16); const ciphertext = encryptedBuffer.slice(16); const decipher = crypto.createDecipheriv("aes-128-cbc", key, iv); decipher.setAutoPadding(true); let decrypted = decipher.update(ciphertext); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString('utf8'); } // ==================== 主测试函数 ==================== async function test_api() { try { console.log(`=== 测试API: ${API_CODE} ===`); console.log(`请求参数: ${JSON.stringify(PARAMS, null, 2)}`); // 加密参数 const params_json = JSON.stringify(PARAMS); const encrypted_data = encrypt_data(params_json); // 构建请求 const headers = { 'Content-Type': 'application/json', 'Access-Id': ACCESS_ID }; const url = `${BASE_URL}/api/v1/${API_CODE}`; const request_data = { 'data': encrypted_data, 'options': { 'json': true } }; console.log(`请求URL: ${url}`); console.log(`请求头: ${JSON.stringify(headers, null, 2)}`); // 发送请求 const start_time = Date.now(); const response = await fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(request_data), signal: AbortSignal.timeout(30000) // 30秒超时 }); const elapsed_time = Date.now() - start_time; console.log(`\n=== 响应信息 ===`); console.log(`状态码: ${response.status}`); console.log(`耗时: ${elapsed_time}ms`); if (response.status !== 200) { console.log(`请求失败: ${response.status} ${response.statusText}`); return; } // 解析响应 try { const response_json = await response.json(); console.log(`原始响应: ${JSON.stringify(response_json, null, 2)}`); // 检查响应格式 if (!('code' in response_json)) { console.log("直接返回业务数据"); return; } // 标准格式处理 const api_code = response_json.code; const api_message = response_json.message || ''; const encrypted_response = response_json.data || ''; console.log(`API响应码: ${api_code}`); console.log(`API消息: ${api_message}`); if (api_code !== 0) { console.log(`API错误: ${api_message}`); return; } if (!encrypted_response) { console.log("无加密数据返回"); return; } // 解密数据 try { const decrypted_data = decrypt_data(encrypted_response); const result_data = JSON.parse(decrypted_data); console.log(`\n=== 解密后的数据 ===`); console.log(JSON.stringify(result_data, null, 2)); } catch (e) { console.log(`解密失败: ${e.message}`); } } catch (e) { console.log(`响应不是JSON格式: ${e.message}`); } } catch (e) { console.log(`测试异常: ${e.message}`); } } // 运行测试 test_api();