123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 'use strict';
- const Controller = require('egg').Controller;
- const urlencode = require('urlencode');
- const Insurance_reg_host = "https://aip.baidubce.com/rest/2.0/ocr/v1/insurance_documents";
- const client_id = "ijsEjPQODRIZjdrh3Q3YvhpO";
- const client_secret = "ZM1Bq2UH4NuEllsV07LLbL5XTqtqcWr2";
- const TokenCacheName = "baidu#token";
- function meargeObj(result, src)
- {
- if( !src ) return;
- for( let k in src) {
- let v = src[k];
- if( !v ) continue;
- if( Array.isArray(v) ) {
- if( !result[k] || result[k].length < 1) {
- let validArr = [];
-
- v.forEach(el=>{
- let ok = false;
- for(let k2 in el) {
- if( el[k2] ) ok = true;
- }
- validArr.push( el );
- })
- if( validArr.length > 0 ) {
- result[k] = validArr;
- }
- }
- } else if( !result[k] ) {
- result[k] = src[k];
- }
- }
- return result;
- }
- class BaiduController extends Controller {
-
- async getBaiduToken(){
- let {ctx, app} = this;
- let token = await ctx.service.cache.getString(TokenCacheName);
- if( token ) return token;
- let ret = await app.curl(`https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}`, {method:"POST", dataType:"json"});
- // {
- // "refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074",
- // "expires_in": 2592000,
- // "scope": "public wise_adapt",
- // "session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI",
- // "access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074",
- // "session_secret": "dfac94a3489fe9fca7c3221cbf7525ff"
- // }
- console.log("token", ret.data);
- if( ret.status == 200 ) {
- let data = ret.data;
- await ctx.service.cache.setString(TokenCacheName, data.access_token, data.expires_in);
- return data.access_token;
- }
-
- throw ret.data;
- }
- async picRecognition(){
- let {ctx, app} = this;
- let { pictures } = ctx.request.body;
- if( !pictures || pictures.length < 1 ) {
- ctx.resultFail("图片数据有误");
- return;
- }
- let result = {
- BenPerLst: [], //array[]受益人信息
- BenCltNa: "", //string 受益人姓名
- BenPerPro:"", //string 受益比例
- BenPerOrd:"", //string 受益顺序
- BenPerTyp:"", //string 受益人类型
- InsBilCom:"", //string 公司名称
- InsBilNo:"", //string 保险单号码
- InsBilTim:"", //string 保单生效日期
- InsCltGd1:"", //string 投保人性别
- InsCltNa1:"", //string 投保人
- InsIdcNb1:"", //string 投保人证件号码
- InsIdcTy1:"", //string 投保人证件类型
- InsPerLst:[], //array[] 被保人信息
- InsCltGd2:"", //string 被保人性别
- InsCltNa2:"", //string 被保险人
- InsBthDa2:"", //string 被保险人出生日期
- InsIdcNb2:"", //string 被保险人证件号码
- InsIdcTy2:"", //string 被保险人证件类型
- InsPrdList:[], //array[] 保险信息
- InsCovDur:"", //string 保险期限
- InsIcvAmt:"", //string 基本保险金额
- InsPayDur:"", //string 交费期间
- InsPayFeq:"", //string 缴费频率
- InsPerAmt:"", //string 每期交费金额
- InsPrdNam:"", //string 产品名称
- };
- let n = pictures.length;
- while( n-- ) {
- let ret = await app.curl(pictures[n]);
- console.log(pictures[n], ret.status);
-
- if( ret.status >= 200 && ret.status < 300 ) {
- // let type = ret.headers["content-type"];
- // let prefix = "data:" + type + ";base64,";
- let base64 = Buffer.from( ret.data ).toString("base64");
- let image = urlencode(base64, 'utf8');
- let token = await this.getBaiduToken();
- console.log(base64.length / 1024 );
-
- try {
- ret = await app.curl(Insurance_reg_host+"?access_token=" + token, {method: "POST", dataType:'json', data:"image="+image, headers:{
- "content-type":"application/x-www-form-urlencoded"
- }});
- let words_result = ret.data.words_result;
- meargeObj(result, words_result);
- } catch (error) {
- console.warn(error.message);
- }
- }
- }
- console.log( result );
- //todo 把result 转为我们自己的格式
-
- ctx.resultOK( result );
- }
- }
- module.exports = BaiduController;
|