Node.js 示例
// 依赖:Node.js 18+(内置 fetch、crypto),无需第三方包
// 运行:node gateway-client.js
// AppSecret 是平台发放的 RSA 私钥(Base64 DER PKCS#8)
const crypto = require('node:crypto');
const appId = process.env.GATEWAY_APPID;
const appSecret = process.env.GATEWAY_APPSECRET;
if (!appId) throw new Error('请设置 GATEWAY_APPID');
if (!appSecret) throw new Error('请设置 GATEWAY_APPSECRET');
const baseUrl = process.env.GATEWAY_BASEURL ?? 'https://t-apiexpo.zhuopux.com';
async function main() {
const content = JSON.stringify({
RealName: '张三',
IDCardNO: '110101199001011234',
PhoneNum: '13800000000',
BarCode: 'T20260001',
});
const request = {
appid: appId,
method: 'AppointmentNotFaceAndBarCode',
timestamp: Date.now().toString(),
charset: 'UTF-8',
version: '1.0',
content,
};
request.sign = sign(buildCanonical(request), appSecret);
const response = await fetch(`${baseUrl}/api/gateway/receive`, {
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: JSON.stringify(request),
});
console.log('HTTP', response.status);
console.log(await response.text());
}
function buildCanonical(r) {
return ['appid', 'charset', 'content', 'method', 'timestamp', 'version']
.sort()
.map((k) => `${k}=${r[k]}`)
.join('&');
}
function sign(canonical, privateKeyBase64) {
const md5Hex = crypto.createHash('md5').update(canonical, 'utf8').digest('hex');
const signer = crypto.createSign('RSA-SHA256');
signer.update(md5Hex, 'utf8');
return signer.sign(
{
key: `-----BEGIN PRIVATE KEY-----\n${privateKeyBase64}\n-----END PRIVATE KEY-----`,
format: 'pem',
type: 'pkcs8',
},
'base64'
);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});