npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zmxy

v2.1.2

Published

Zhima credit unofficial SDK

Downloads

7

Readme

芝麻信用 NodeJS SDK (非官方)

NPM version Build Status Dependencies Status codecov npm License

接入流程

1. 注册芝麻信用商家服务平台并签约

2. 在芝麻信用商家服务平台创建商家应用并交换公钥

与芝麻信用交换公钥的步骤:

  1. 进入openssl
  2. 运行genrsa -out app_private_key.pem 1024生成一个私钥
  3. 运行rsa -in app_private_key.pem -pubout -out app_public_key.pem生成公钥
  4. 进入芝麻信用后台,将生成的公钥 去除 首行 / 尾行 / 换行符 粘贴进应用的公钥一栏
  5. 应用创建后,点击应用的芝麻公钥一栏,得到芝麻信用的公钥(原始格式)
  6. 将其整理为RSA公钥形式, 即加入首行,尾行,以及每64字符就换行, 并保存为 zmxy_public_key.pem

至此我们一共有三个秘钥文件,分别是:

  • app_private_key.pem 应用私钥
  • zmxy_public_key.pem 芝麻信用公钥
  • app_public_key.pem 应用公钥(仅用于调试)

3. 使用SDK

在任意Node项目中可以通过npm安装

npm install zmxy --save

在保存秘钥的文件夹下创建app.js文件

const fs = require('fs');
const ZmxyClient = require('zmxy').default;

const zmxy = new ZmxyClient({
  appId: '123456',   //芝麻应用App ID
  appPrivateKey: fs.readFileSync(`${__dirname}/app_private_key.pem`),  //App私钥
  zmxyPublicKey: fs.readFileSync(`${__dirname}/zmxy_public_key.pem`)   //芝麻公钥
});
zmxy.verifyIvs({
  name: '张三',
  mobile: '12345678901'
}).then(({ result }) => {
  console.log(result);
}).catch((err) => {
  console.error(err);
});

请求成功后可以看到控制台打印芝麻信用的返回

{ success: true,
  biz_no: 'ZM2016102030000000XXXXXXXXX',
  ivs_detail:
   [ { code: 'PHONE_Match_Recency_Bad',
       description: '电话号码与其他信息匹配,但匹配后的信息近期不活跃' },
     { code: 'NAME_Match_Reliability_Bad',
       description: '姓名与其他信息匹配,但匹配后的信息未经认证' } ],
  ivs_score: 72 }

SDK功能

获得欺诈评分

zmxy.getIvsScore({
  name: '张三',
  cert_no: '532926200804058748'
}).then(({ result }) => {
  console.log(result.score); //output is risk score such as 89
});

反欺诈信息验证

zmxy.verifyIvs({
  name: '张三',
  cert_no: '532926200804058748'
}).then(({ result }) => {
  console.log(result.verify_code); //output is an Array contains risk codes such as ['V_CN_NA', 'V_PH_NA']
});

欺诈关注名单

zmxy.getIvsWatchList({
  name: '张三',
  cert_no: '532926200804058748'
}).then(({ result }) => {
  console.log(result.riskCode); //output is an Array contains risk codes such as ['R_CN_002']
});

查询芝麻评分

由于芝麻评分需要用户授权,因此首先需要生成授权URL

生成授权URL

授权URL可以通过姓名+身份证或者手机号得到, 同时又支持PC网页和H5页面,排列组合共4种情况如下

手机号+PC

const { url } = zmxyClient.getAuthorizeUrl({
  mobile: '12345678901'
});

手机号+H5

const { url } = zmxyClient.getAuthorizeUrl({
  mobile: '12345678901'
}, 'h5');

身份证+PC

const { url } = zmxyClient.getAuthorizeUrl({
  name: '张三',
  certNo: '111111111111111111'
});

身份证+H5

const { url } = zmxyClient.getAuthorizeUrl({
  name: '张三',
  certNo: '111111111111111111'
}, 'h5');

获得OpenId

获得授权URL后, 可以在浏览器中打开此URL,根据提示授权后会被重定向到芝麻应用的回调地址, 地址Query中的params参数会携带OpenId, 可以通过如下方式获得

const { open_id } = zmxyClient.getOpenId(params);

查询芝麻信用分

有了OpenID后就可以查询芝麻信用分

zmxyClient.getCreditScore('openid').then((r) => {
  console.log(r.result.zm_score)
});

查询行业关注名单

同上获得OpenId后可做如下查询

zmxyClient.verifyWatchlist('openid').then((r) => {
  console.log(r.result.is_matched)
});

芝麻认证

流程与芝麻认证文档一致

1. 初始化并生成biz_no

let bizNo = null;
zmxyClient.initCertification('张三', '310105912123123412').then((r) => {
  bizNo = r.result.biz_no; //output is as ZM201703093000000727200705771480
});

2. 通过biz_no生成芝麻认证URL

const url = zmxyClient.getCertificationCertifyUrl(bizNo, 'http://localhost');

3. 设备访问芝麻认证URL,再使用bizNo查询芝麻认证结果

zmxyClient.queryCertification(bizNo).then((r) => {
  console.log(r.result.passed); //output is true or false
});

开启调试

由于SDK使用了request, 所以可以直接在命令行中通过环境变量开启Debug模式

NODE_DEBUG=request node app.js