开发接口文档巨坑。
很多坑是明显的错误。
现在实现了这个代码,贴在下面。有兴趣的自己下载去试试。
const Base = require('./base.js');
const crypto = require('crypto');
const urllib = require('urllib');
module.exports = class extends Base {
aes_encrypt(message, key) {
// AES 128 GCM Mode
let iv = crypto.randomBytes( 12 );
let cipher = crypto.createCipheriv( 'aes-128-gcm', Buffer.from(key, "hex"), iv );
cipher.setAutoPadding( true );
// encrypt the given text
let encrypted = Buffer.concat( [ cipher.update(message, 'utf8'), cipher.final() ] );
// extract the auth tag
let tag = cipher.getAuthTag();
// generate output
return Buffer.concat( [iv, encrypted, tag] ).toString( 'base64' );
}
sort_object(data) {
let s_dict = Object.keys(data).sort();
let retval = '';
for( let key in s_dict ) {
let k = s_dict[key];
retval += k + data[k];
}
return retval;
}
create_sign(headers, query, body, key) {
let data = Object.assign( headers, query );
let hash = crypto.createHash('sha256');
return hash.update( key + this.sort_object(data, true) + body ).digest('hex');
}
create_ai(account) {
let md5 = crypto.createHash('md5');
return md5.update(account).digest().toString("hex");
}
async check(ai, name, identity) {
//Request
let content_type = 'application/json; charset=utf-8';
let headers = {
'appId': think.config('nppa.appid'),
'bizId': think.config('nppa.bizid'),
'timestamps': Date.now()
}
let body = {
ai: ai,
name: name,
idNum: identity,
}
let key = think.config('nppa.secret');
let enc_body = this.aes_encrypt( JSON.stringify(body), key );
let enc_body_str = '{"data":"' + enc_body + '"}';
headers['sign'] = this.create_sign( headers, '', enc_body_str, key );
headers['Content-Type'] = content_type;
let request_data = {
method: "POST",
headers: headers,
data: enc_body_str
}
const quest_func = async () =>
{
return new Promise((resolve, reject) => {
try {
urllib.request( "https://api.wlc.nppa.gov.cn/idcard/authentication/chec