微信小程序身份证识别

微信小程序项目中用户注册到了证件识别,获取身份证号码和姓名等,一开始想直接有第三方的证件识别,后面发现微信就有自带的证件识别,免费一天100次(免费使用的也必须0元购买),注册用户没有那么频繁使用,已经足够,就研究了一番。

逻辑如下,先用小程序的appId和APP_SECRET获取accesstoken,然后再用accesstoken和证件图片获取证件信息。

                wx.request({
                    url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${APP_SECRET}`,
                    success: (res) => {
                        console.log('access_token')
                        uni.setStorage({
                            key: "accesstoken",
                            data: res.data.access_token
                        })
                        console.log(res);
                        console.log(cadeex);
                    }
                })
            // 微信小程序的证件识别调用事件方法,上传图片到微信服务器/线上后不能直接在前端直接上传到微信服务器
            async upload(FilePath) {
                return new Promise((resolve, reject) => {
                    wx.uploadFile({
                        url: "https://api.weixin.qq.com/cv/ocr/idcard?access_token="+this.accesstoken+"",
                        header: {
                            'content-type': 'application/json; encoding=utf-8'
                          },
                        filePath: FilePath,
                        name: 'file',
                        success: (res) => {
                            var data = JSON.parse(res.data)
                            resolve(data)
                        }
                    })
                })
            },

返回的json

{
    "errcode": 0,
    "errmsg": "ok",
    "type": "Front",
    "name": "苏海峰",
    "id": "341181198809150011",
    "addr": "安徽省天长市千秋新村三巷10号",
    "gender": "",
    "nationality": "",
    "birth": "1988-09-15",
    "card_property": 1
}

注意:微信小程序的密钥APP_SECRET是比较私密和关键的,所以不会放在前端暴露。建议放后端返回。

OCR证件识别购买链接https://fuwu.weixin.qq.com/service/detail/000ce4cec24ca026d37900ed551415

posted @ 2024-09-18 10:36  蓬鹏  阅读(29)  评论(0编辑  收藏  举报