index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * 百度API
  3. */
  4. var baidu = {};
  5. var vk = {};
  6. var baiduOpenApiAccessTokenCacheName = "vk_openapi_baidu_open_token";
  7. baidu.init = function(util={}) {
  8. vk = util.vk;
  9. };
  10. // 百度开放平台api
  11. baidu.open = {};
  12. // 文字识别
  13. baidu.open.ocr = {};
  14. /**
  15. * 营业执照识别
  16. * 以下data参数三选一即可
  17. * @param {File} file 文件对象
  18. * @param {String} image 图像base64编码后进行urlencode
  19. * @param {String} url 图片完整URL
  20. *
  21. vk.openapi.baidu.open.ocr.business_license({
  22. title:"识别中...",
  23. data: {
  24. file: res.tempFiles[0]
  25. },
  26. success: (res) => {
  27. that.data = res.data;
  28. },
  29. });
  30. */
  31. baidu.open.ocr.business_license = function(obj={}){
  32. obj.action = "ocr/v1/business_license";
  33. let file = obj.data.file;
  34. if(file){
  35. vk.pubfn.fileToBase64({ file }).then(base64 => {
  36. obj.data.image = base64;
  37. delete obj.data.file;
  38. baidu.request(obj);
  39. });
  40. }else{
  41. baidu.request(obj);
  42. }
  43. }
  44. /**
  45. * 身份证识别
  46. * 以下data参数三选一即可
  47. * @param {File} file 文件对象
  48. * @param {String} image 图像base64编码后进行urlencode
  49. * @param {String} url 图片完整URL
  50. vk.openapi.baidu.open.ocr.idcard({
  51. title:"识别中...",
  52. data: {
  53. image:base64,
  54. id_card_side:"front", // front:身份证含照片的一面 back:身份证带国徽的一面
  55. },
  56. success: (res) => {
  57. that.data = res.data;
  58. },
  59. });
  60. */
  61. baidu.open.ocr.idcard = function(obj={}){
  62. obj.action = "ocr/v1/idcard";
  63. let file = obj.data.file;
  64. if(!obj.data.id_card_side) obj.data.id_card_side = "front";
  65. if(file){
  66. vk.pubfn.fileToBase64({ file }).then(base64 => {
  67. obj.data.image = base64;
  68. delete obj.data.file;
  69. baidu.request(obj);
  70. });
  71. }else{
  72. baidu.request(obj);
  73. }
  74. }
  75. /**
  76. * 百度开放平台通用请求接口
  77. * @param {String} action 接口名称
  78. * @param {String} actionVersion 接口版本名称 默认2.0
  79. * @param {String} title loading文字
  80. * @param {object} data 请求参数
  81. * @param {String} success 成功回调
  82. * @param {String} fail 失败回调
  83. * @param {String} complete 完成回调
  84. * 使用示例
  85. vk.openapi.baidu.request({
  86. action: 'ocr/v1/business_license',
  87. title:"识别中...",
  88. data: {
  89. image:base64
  90. },
  91. success: (data) => {
  92. that.data = data;
  93. },
  94. });
  95. */
  96. baidu.request = function(obj={}){
  97. let {
  98. title
  99. } = obj;
  100. if(title) vk.showLoading(title);
  101. let baiduApiAccessToken = vk.getStorageSync(baiduOpenApiAccessTokenCacheName);
  102. if(baiduApiAccessToken && baiduApiAccessToken.expTime > new Date().getTime()){
  103. // 获取缓存中的token
  104. obj.accessToken = baiduApiAccessToken.accessToken;
  105. request(obj);
  106. }else{
  107. // 发起请求获取token
  108. vk.callFunction({
  109. url: 'plugs/baidu/client/pub/getAccessToken',
  110. success:function(tokenRes) {
  111. vk.setStorageSync(baiduOpenApiAccessTokenCacheName,{
  112. accessToken : tokenRes.access_token,
  113. expTime : 2590000000 + new Date().getTime(),
  114. });
  115. obj.accessToken = tokenRes.access_token;
  116. request(obj);
  117. },
  118. fail:function(res){
  119. if(title) vk.hideLoading();
  120. if(typeof obj.fail === "function") obj.fail(res);
  121. if(typeof obj.complete === "function") obj.complete(res);
  122. }
  123. });
  124. }
  125. }
  126. function request(obj={}){
  127. let {
  128. action,
  129. actionVersion = "2.0",
  130. accessToken,
  131. header = { "content-type": "application/x-www-form-urlencoded" },
  132. data,
  133. title
  134. } = obj;
  135. vk.request({
  136. url: `https://aip.baidubce.com/rest/${actionVersion}/${action}?access_token=${accessToken}`,
  137. method:"POST",
  138. header:{
  139. "content-type": "application/x-www-form-urlencoded",
  140. },
  141. errorCodeName:"error_code",
  142. errorMsgName:"error_msg",
  143. data,
  144. needAlert:true,
  145. success: function(data){
  146. if(title) vk.hideLoading();
  147. if(data.code){
  148. if(typeof obj.fail === "function") obj.fail(data);
  149. }else{
  150. if(typeof obj.success === "function") obj.success(data);
  151. }
  152. },
  153. fail: function(data){
  154. if(title) vk.hideLoading();
  155. // 如果是因为token失效报的错误,则清空下本地token缓存
  156. if(data && data.code === 110) vk.removeStorageSync(baiduOpenApiAccessTokenCacheName);
  157. if(typeof obj.fail === "function") obj.fail(data);
  158. },
  159. complete: function(data){
  160. if(typeof obj.complete === "function") obj.complete(data);
  161. }
  162. });
  163. }
  164. export default baidu;