api-test/server.js
2024-10-15 21:10:08 +08:00

263 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const crypto = require('crypto');
const axios = require('axios');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = 3000;
// AES CBC 加密函数,返回 Base64
function aesEncrypt(plainText, key) {
const keyBuffer = Buffer.from(key, 'hex'); // 将16进制的密钥转换为 Buffer
const blockSize = 16; // AES 块大小
const iv = crypto.randomBytes(blockSize); // 生成随机 IV
const cipher = crypto.createCipheriv('aes-128-cbc', keyBuffer, iv);
cipher.setAutoPadding(true);
let encrypted = cipher.update(plainText);
encrypted = Buffer.concat([iv, encrypted, cipher.final()]);
return encrypted.toString('base64');
}
// AES CBC 解密函数,返回解密后的明文
function aesDecrypt(encryptedText, key) {
const encryptedBuffer = Buffer.from(encryptedText, 'base64');
const keyBuffer = Buffer.from(key, 'hex');
const blockSize = 16;
const iv = encryptedBuffer.slice(0, blockSize);
const encryptedData = encryptedBuffer.slice(blockSize);
const decipher = crypto.createDecipheriv('aes-128-cbc', keyBuffer, iv);
decipher.setAutoPadding(true);
let decrypted = decipher.update(encryptedData);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
// 模拟请求参数
const mockData = {
"FLXG3D56": {
mobile_no: "18276151590",
id_card: "45212220000827423X",
name: "张荣宏",
time_range: "5",
description: "特殊名单验证"
},
"FLXG54F5": {
mobile_no: "13812345678",
description: "易诉人群识别"
},
"FLXG162A": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "张三",
description: "团伙欺诈排查(通用版)"
},
"FLXG970F": {
id_card: "110101199003076534",
name: "李四",
description: "风险人员核验"
},
"FLXG5876": {
mobile_no: "13812345678",
description: "易诉人"
},
"FLXG9687": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "王五",
description: "电诈风险预警-标准版"
},
"FLXGC9D1": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "赵六",
description: "黑灰产等级"
},
"FLXGCA3D": {
id_card: "110101199003076534",
name: "李四",
description: "个人综合涉诉"
},
"FLXGDEC7": {
id_card: "110101199003076534",
name: "张三",
description: "个人不良"
},
"IVYZ385E": {
id_card: "110101199003076534",
name: "张三",
description: "自然人生存状态标识"
},
"IVYZ5733": {
name: "李四",
id_card: "110101199003076534",
description: "单人婚姻登记信息核验"
},
"IVYZ9363": {
man_name: "张三",
man_id_card: "110101199003076534",
woman_name: "王五",
woman_id_card: "110101199003076535",
description: "双人婚姻状态识别"
},
"JRZQ0A03": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "张三",
description: "借贷意向验证"
},
"JRZQ4AA8": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "王五",
description: "偿债压力指数"
},
"JRZQ8203": {
mobile_no: "13812345678",
id_card: "110101199003076534",
name: "赵六",
description: "借贷行为验证"
},
"JRZQDCBE": {
mobile_no: "13812345678",
id_card: "110101199003076534",
bank_card: "6212261901001234567",
name: "张三",
description: "银行卡四要素验证"
},
"QYGL2ACD": {
ent_name: "某企业",
legal_person: "李四",
ent_code: "91310000123456789X",
description: "企业三要素核验"
},
"QYGL6F2D": {
id_card: "110101199003076534",
description: "人企关联"
},
"QYGL45BD": {
ent_name: "某公司",
legal_person: "王五",
ent_code: "91310000123456789X",
id_card: "110101199003076534",
description: "企业法人四要素核验"
},
"QYGL8261": {
ent_name: "某公司",
description: "企业综合涉诉"
},
"QYGLB4C0": {
id_card: "110101199003076534",
description: "股东人企关系精准版"
},
"YYSY4B37": {
mobile_no: "13812345678",
description: "手机在网时长"
},
"YYSY4B21": {
mobile_no: "13812345678",
description: "手机在网状态"
},
"YYSY6F2E": {
mobile_no: "13812345678",
mobile_type: "CMCC",
id_card: "110101199003076534",
name: "张三",
description: "运营商三要素核验(详版)"
},
"YYSY09CD": {
mobile_no: "13812345678",
mobile_type: "CUCC",
id_card: "110101199003076534",
name: "李四",
description: "运营商三要素验证(简版)"
},
"YYSYBE08": {
mobile_no: "13812345678",
name: "张三",
description: "运营商二要素核验(手机号、姓名)"
},
"YYSYD50F": {
mobile_no: "13812345678",
id_card: "110101199003076534",
description: "运营商二要素核验(手机号、身份证)"
},
"YYSYF7DB": {
mobile_no: "13812345678",
start_date: "20220101",
description: "手机二次卡"
}
};
// 使用 EJS 模板引擎
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// 解析POST请求的数据
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 提供静态文件(比如 CSS
app.use(express.static(path.join(__dirname, 'public')));
// 提供前端页面
app.get('/', (req, res) => {
res.render('index', { interfaces: Object.keys(mockData), mockData: mockData });
});
// 处理 API 请求
app.post('/request-api', async (req, res) => {
const { interfaceName, params } = req.body;
const url = `http://api.tianyuanapi.com/api/v1/${interfaceName}`;
// const url = `http://127.0.0.1:10003/api/v1/${interfaceName}`;
const accessId = 'aa16cc6e9da90461';
const key = 'ff83609b2b24fc73196aac3d3dfb874f'; // AES 16进制密钥
const data = JSON.parse(params); // 使用用户自定义的参数
const jsonStr = JSON.stringify(data);
const encryptedData = aesEncrypt(jsonStr, key);
try {
const response = await axios.post(url, { data: encryptedData }, {
headers: {
'Access-Id': accessId,
'Content-Type': 'application/json'
}
});
console.log("response.data", response.data)
const { code, message, data: encryptedResponseData } = response.data;
let decryptedData = null;
// 如果有返回data无论code是否为0都尝试解密
if (encryptedResponseData) {
try {
decryptedData = aesDecrypt(encryptedResponseData, key);
decryptedData = JSON.parse(decryptedData); // 解密后的数据解析为对象
} catch (decryptError) {
// 解密失败时返回null
decryptedData = null;
}
}
// 返回响应无论code是否为0统一返回加密和解密数据
res.json({
code,
success: code === 0,
message,
encryptedResponse: encryptedResponseData || null,
decryptedResponse: decryptedData
});
} catch (error) {
res.json({ success: false, message: '请求失败', error: error.message });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});