index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const urlencode = require('urlencode');
  4. const Insurance_reg_host = "https://aip.baidubce.com/rest/2.0/ocr/v1/insurance_documents";
  5. const client_id = "ijsEjPQODRIZjdrh3Q3YvhpO";
  6. const client_secret = "ZM1Bq2UH4NuEllsV07LLbL5XTqtqcWr2";
  7. const TokenCacheName = "baidu#token";
  8. function meargeObj(result, src)
  9. {
  10. if( !src ) return;
  11. for( let k in src) {
  12. let v = src[k];
  13. if( !v ) continue;
  14. if( Array.isArray(v) ) {
  15. if( !result[k] || result[k].length < 1) {
  16. let validArr = [];
  17. v.forEach(el=>{
  18. let ok = false;
  19. for(let k2 in el) {
  20. if( el[k2] ) ok = true;
  21. }
  22. validArr.push( el );
  23. })
  24. if( validArr.length > 0 ) {
  25. result[k] = validArr;
  26. }
  27. }
  28. } else if( !result[k] ) {
  29. result[k] = src[k];
  30. }
  31. }
  32. return result;
  33. }
  34. class BaiduController extends Controller {
  35. async getBaiduToken(){
  36. let {ctx, app} = this;
  37. let token = await ctx.service.cache.getString(TokenCacheName);
  38. if( token ) return token;
  39. 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"});
  40. // {
  41. // "refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074",
  42. // "expires_in": 2592000,
  43. // "scope": "public wise_adapt",
  44. // "session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI",
  45. // "access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074",
  46. // "session_secret": "dfac94a3489fe9fca7c3221cbf7525ff"
  47. // }
  48. console.log("token", ret.data);
  49. if( ret.status == 200 ) {
  50. let data = ret.data;
  51. await ctx.service.cache.setString(TokenCacheName, data.access_token, data.expires_in);
  52. return data.access_token;
  53. }
  54. throw ret.data;
  55. }
  56. async picRecognition(){
  57. let {ctx, app} = this;
  58. let { pictures } = ctx.request.body;
  59. if( !pictures || pictures.length < 1 ) {
  60. ctx.resultFail("图片数据有误");
  61. return;
  62. }
  63. let result = {
  64. BenPerLst: [], //array[]受益人信息
  65. BenCltNa: "", //string 受益人姓名
  66. BenPerPro:"", //string 受益比例
  67. BenPerOrd:"", //string 受益顺序
  68. BenPerTyp:"", //string 受益人类型
  69. InsBilCom:"", //string 公司名称
  70. InsBilNo:"", //string 保险单号码
  71. InsBilTim:"", //string 保单生效日期
  72. InsCltGd1:"", //string 投保人性别
  73. InsCltNa1:"", //string 投保人
  74. InsIdcNb1:"", //string 投保人证件号码
  75. InsIdcTy1:"", //string 投保人证件类型
  76. InsPerLst:[], //array[] 被保人信息
  77. InsCltGd2:"", //string 被保人性别
  78. InsCltNa2:"", //string 被保险人
  79. InsBthDa2:"", //string 被保险人出生日期
  80. InsIdcNb2:"", //string 被保险人证件号码
  81. InsIdcTy2:"", //string 被保险人证件类型
  82. InsPrdList:[], //array[] 保险信息
  83. InsCovDur:"", //string 保险期限
  84. InsIcvAmt:"", //string 基本保险金额
  85. InsPayDur:"", //string 交费期间
  86. InsPayFeq:"", //string 缴费频率
  87. InsPerAmt:"", //string 每期交费金额
  88. InsPrdNam:"", //string 产品名称
  89. };
  90. let n = pictures.length;
  91. while( n-- ) {
  92. let ret = await app.curl(pictures[n]);
  93. console.log(pictures[n], ret.status);
  94. if( ret.status >= 200 && ret.status < 300 ) {
  95. // let type = ret.headers["content-type"];
  96. // let prefix = "data:" + type + ";base64,";
  97. let base64 = Buffer.from( ret.data ).toString("base64");
  98. let image = urlencode(base64, 'utf8');
  99. let token = await this.getBaiduToken();
  100. console.log(base64.length / 1024 );
  101. try {
  102. ret = await app.curl(Insurance_reg_host+"?access_token=" + token, {method: "POST", dataType:'json', data:"image="+image, headers:{
  103. "content-type":"application/x-www-form-urlencoded"
  104. }});
  105. let words_result = ret.data.words_result;
  106. meargeObj(result, words_result);
  107. } catch (error) {
  108. console.warn(error.message);
  109. }
  110. }
  111. }
  112. console.log( result );
  113. //todo 把result 转为我们自己的格式
  114. ctx.resultOK( result );
  115. }
  116. }
  117. module.exports = BaiduController;