93 lines
4.1 KiB
JavaScript
93 lines
4.1 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const Service = require('../core/base_service');
|
||
|
|
|
||
|
|
const fileName = "CNTEVService";
|
||
|
|
|
||
|
|
const Dytnsapi20200217 = require('@alicloud/dytnsapi20200217');
|
||
|
|
const OpenApi = require('@alicloud/openapi-client');
|
||
|
|
const Util = require('@alicloud/tea-util');
|
||
|
|
|
||
|
|
class Client {
|
||
|
|
static createClient() {
|
||
|
|
let config = new OpenApi.Config({
|
||
|
|
// 凭证类型。
|
||
|
|
// type: 'access_key',
|
||
|
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||
|
|
accessKeyId: "LTAI5tM3QC6kY4r23sWnsvuY",
|
||
|
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||
|
|
accessKeySecret: "Lj9QTWVMJSTAffscjykhaKZt7RN0Xh",
|
||
|
|
// 访问的地域
|
||
|
|
// regionId: 'cn-beijing'
|
||
|
|
});
|
||
|
|
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
||
|
|
// 访问的地址
|
||
|
|
config.endpoint = `dytnsapi.aliyuncs.com`;
|
||
|
|
return new Dytnsapi20200217.default(config);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class CNTEVServiceService extends Service {
|
||
|
|
// 身份证二要素核验
|
||
|
|
async CNTEV(user_name, identity_card) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
let client = Client.createClient();
|
||
|
|
// 创建请求对象并设置入参
|
||
|
|
let certNoTwoElementVerificationRequest = new Dytnsapi20200217.CertNoTwoElementVerificationRequest({
|
||
|
|
authCode: "3V2qgr9a2W",
|
||
|
|
certName: user_name,
|
||
|
|
certNo: identity_card,
|
||
|
|
});
|
||
|
|
// 创建运行时配置对象
|
||
|
|
let runtime = new Util.RuntimeOptions({});
|
||
|
|
runtime.readTimeout = 5000;
|
||
|
|
runtime.connectTimeout = 5000;
|
||
|
|
// 发起请求
|
||
|
|
const response = await client.certNoTwoElementVerificationWithOptions(certNoTwoElementVerificationRequest, runtime);
|
||
|
|
const { code, data } = response.body;
|
||
|
|
if (code == "OK") {
|
||
|
|
await ctx.app.redis.get('db3').set(identity_card, JSON.stringify({ user_name, real_check: data.isConsistent }), 'EX', 600);
|
||
|
|
if (data.isConsistent == "1") return { status: 1, msg: "姓名与身份证信息核验一致" };
|
||
|
|
if (data.isConsistent == "0") return { status: 0, msg: "姓名与身份证信息核验不一致" };
|
||
|
|
if (data.isConsistent == "2") return { status: 2, msg: "查无此人" };
|
||
|
|
} else {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||
|
|
// 错误 message
|
||
|
|
// console.log('1===========================================')
|
||
|
|
// console.log(error.message);
|
||
|
|
if (error.message.includes("身份证号参数非法")) return { status: 400, msg: "身份证号参数非法" };
|
||
|
|
// 若捕捉到 ConnectTimeout 错误,则尝试重发
|
||
|
|
if (error.message.includes("ConnectTimeout")) return { status: 600, msg: "尝试重新核验" };
|
||
|
|
// console.log('2===========================================')
|
||
|
|
// 诊断地址
|
||
|
|
console.log(error);
|
||
|
|
// console.log('3===========================================')
|
||
|
|
// console.log(error.data);
|
||
|
|
// console.log('4===========================================')
|
||
|
|
// console.log(error.data["Recommend"]);
|
||
|
|
// Util.default.assertAsString(error.message);
|
||
|
|
|
||
|
|
const blockName = fileName + "--" + "CNTEV";
|
||
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 身份证二要素核验结果标记
|
||
|
|
async realMark(user_name, identity_card, real_check) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
await ctx.model5.Examinee.update({ real_check: real_check ? 1 : 2 }, { where: { user_name, identity_card } });
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "realMark";
|
||
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = CNTEVServiceService;
|