rapid-ztx
Version:
Rapid ZTX module
119 lines (103 loc) • 3.25 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('ztx');
const request = require('request-promise-native');
const rapid_db = require('rapid-db');
const smtp = require('rapid-smtp');
/**
* 验证身份证号码是否有效
*/
const verify_idno = (idno) => {
if (!idno || idno.length != 18) {
return false;
}
const arr = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
let sum = 0;
for(let i = 0; i < 17; i++) {
sum += idno.substr(i, 1) * arr[i];
}
const residue = arrCh[sum % 11];
return residue == idno.substr(17, 1).toUpperCase();
}
/**
* 通过卡号查询归属地
*
* 调用第三方接口查询,接口文档地址:
* https://market.aliyun.com/products/57000002/cmapi016679.html?
* spm=5176.2020520132.101.6.7Hfl6a#sku=yuncode1067900000
*/
const query_card_city = async (card_no) => {
const serve_url = 'http://api43.market.alicloudapi.com/api/c43';
try {
const resp = await request.get(`${serve_url}?bankcard=${card_no}`, {
headers: {
Authorization: 'APPCODE 327932f9616d48deb5260bcdb3e0c5cc'
}
});
let resp_json = JSON.parse(resp);
if (resp_json.error_code != 0) {
throw new Error(`查询银行卡归属地错: ${resp_json.reason || '未知'}`);
}
if (!resp_json.result) {
throw new Error(`查询银行卡归属地错: 无结果`);
}
if (!resp_json.result.city) {
resp_json.result.city = '上海';
return resp_json.result;
}
resp_json.result.city = resp_json.result.city.replace(/市$/, '');
// 确保城市名称是有效的
const record = await unique_db.one(`
select count(*) as count from rapid_cities where city_name = $1
`, [
resp_json.result.city
]);
if (record.count == 0) {
resp_json.result.city = '上海';
}
return resp_json.result;
} catch (err) {
throw new Error(`查询银行卡归属地错: ${err.message}`);
}
}
/**
* 给代理商发送邮件通知
*/
const mail_to_agent = async (agent_uuid, subject, html) => {
const agent = await unique_db.oneOrNone(`
select shortname, fullname, contact, email, mail_notify
from rapid_ctod_agents where uuid = $1
`, [
agent_uuid
]);
if (!agent) {
return await rapid_db.notify.error(
`查询代理商信息错: ${err.message},发送邮件失败`
);
}
if (!agent.mail_notify) {
return;
}
if (!agent.email) {
return await rapid_db.notify.warn(`
代理商[${agent.shortname}]未登记联系邮箱,不能发送邮件`
);
}
const content = `
<p>${agent.contact},您好:</p>
${html}
<p></p>
<p>${agent.fullname}</p>
<p></p>
`;
await smtp.send({
subject: subject, to: agent.email, html: content,
});
}
module.exports = {
verify_idno: verify_idno,
query_card_city: query_card_city,
mail_to_agent: mail_to_agent,
}