【国家防沉迷实名认证系统–NODEJS】
首先是插件引入和常量定义
const https = require('https');
const querystring = require("querystring");
const crypto = require('crypto');
let host = 'api.wlc.nppa.gov.cn';
let host2 = 'api2.wlc.nppa.gov.cn';
let path = '/idcard/authentication/check';
let queryPath = "/idcard/authentication/query";
let loginoutPath = "/behavior/collection/loginout";
let method = 'POST';
let secretKey = '';
let appId = '';
let bizId = '';
let signPreStr = secretKey + 'appId' + appId + 'bizId' + bizId + 'timestamps';
然后上代码:
Authentication.prototype.getIdentityResult = function (ai, identity, name, cb) {
let idJson = {
'ai': ai,
'name': name,
'idNum': identity
};
let postData = JSON.stringify(idJson);
let encr = encrypt(postData);
let encrData = JSON.stringify({
'data': encr
});
let timestamps = Date.now();
let signStr = signPreStr + timestamps + encrData;
let sha256 = crypto.createHash('sha256');
sha256.update(signStr, 'utf8');
let sign = sha256.digest('hex');
let identityResult = '';
let options = {
hostname: host,
path: path,
method: method,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': encrData.length,
'appId': appId,
'bizId': bizId,
'timestamps': timestamps,
'sign': sign
}
};
let self = this;
let req = https.request(options, (res) => {
res.setEncoding('utf-8');
if (res.statusCode === 200) {
res.on('data', (chunk) => {
identityResult += chunk;
});
res.on('end', () => {
logger.info('get identity result : %j, idJson: %j', identityResult, idJson);
let result = JSON.parse(identityResult);
if (0 === result.errcode) {
if (0 === result.data.result.status) {
cb && cb(null, idJson);
return;
} else if (1 === result.data.result.status) {
cb();
return;
}
} else if (2004 === result.errcode) {
cb();
return;
}
cb();
});
} else {
cb();
}
});
req.write(encrData);
req.on('error', (e) => {
cb();
});
req.end();
};
let encrypt = function (postData) {
try {
let key = Buffer.from(secretKey, 'hex');
let iv = crypto.randomBytes(12);
let cipher = crypto.createCipheriv('aes-128-gcm', key, iv);
let encr = cipher.update(postData, 'utf8', 'base64');
encr += cipher.final('base64');
let tags = cipher.getAuthTag();
encr = Buffer.from(encr, 'base64');
let totalLength = iv.length + encr.length + tags.length;
let bufferData = Buffer.concat([iv, encr, tags], totalLength);
return bufferData.toString('base64');
} catch (e) {
console.log("Encrypt is error", e);
return null;
}
};
Authentication.prototype.queryAuthentication = function (ai, cb) {
let getData = querystring.stringify({
'ai': ai
});
let timestamps = Date.now();
let signStr = secretKey + 'ai' + ai + 'appId' + appId + 'bizId' + bizId + 'timestamps' + timestamps;
let sha256 = crypto.createHash('sha256');
sha256.update(signStr, 'utf8');
let sign = sha256.digest('hex');
let queryResult = '';
let options = {
hostname: host,
path: queryPath + '?' + getData,
method: 'GET',
headers: {
'appId': appId,
'bizId': bizId,
'timestamps': timestamps,
'sign': sign
}
};
let req = https.request(options, (res) => {
res.setEncoding('utf-8');
if (res.statusCode === 200) {
res.on('data', (chunk) => {
queryResult += chunk;
});
res.on('end', () => {
logger.info('get identity result : %j, getData: %j', queryResult, getData);
let result = JSON.parse(queryResult);
if (0 === result.errcode) {
if (0 === result.data.result.status) {
cb();
return;
} else if (1 === result.data.result.status) {
cb();
return;
}
} else if (2004 === result.errcode) {
cb();
return;
}
cb();
});
} else {
cb();
}
});
req.on('error', (e) => {
cb();
});
req.end();
};
Authentication.prototype.loginout = function (si, bt, state, pi, cb) {
let idJson = {
"no": 1,
"si": si,
"bt": bt,
"ot": Math.floor(Date.now() / 1000)
};
if (state === 1) {
idJson.ct = 2;
idJson.di = pi;
} else {
idJson.ct = 0;
idJson.pi = pi;
}
let postData = JSON.stringify({"collections": [idJson]});
let encr = encrypt(postData);
let encrData = JSON.stringify({
'data': encr
});
let timestamps = Date.now();
let signStr = signPreStr + timestamps + encrData;
let sha256 = crypto.createHash('sha256');
sha256.update(signStr, 'utf8');
let sign = sha256.digest('hex');
let identityResult = '';
let options = {
hostname: host2,
path: loginoutPath,
method: method,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': encrData.length,
'appId': appId,
'bizId': bizId,
'timestamps': timestamps,
'sign': sign
}
};
let req = http.request(options, (res) => {
res.setEncoding('utf-8');
if (res.statusCode === 200) {
res.on('data', (chunk) => {
identityResult += chunk;
});
res.on('end', () => {
logger.info('idJson: %j, loginout result : %j', idJson, identityResult);
let result = JSON.parse(identityResult);
if (0 === result.errcode) {
cb && cb(null, 200);
return;
}
cb && cb("error", 500);
});
} else {
logger.error('Authentication loginout err code : %j, idJson: %j', res.statusCode, idJson);
cb && cb("error", 500);
}
});
req.write(encrData);
req.on('error', (e) => {
logger.error('Authentication loginout 请求遇到问题: %j, idJson: %j', e.message, idJson);
cb && cb("error", 500);
});
req.end();
};