1- function getTomorrow ( ) {
1+ function getTomorrow ( ) {
22 const now = new Date ( )
33 const tomorrow = new Date ( now )
44
@@ -21,7 +21,10 @@ function getTomorrow () {
2121// return new Date(nextYear, nextMonth, 1, 0, 0, 0, 0).getTime()
2222// }
2323
24- const AipOcrClient = require ( 'baidu-aip-sdk' ) . ocr
24+ import { createRequire } from 'node:module'
25+
26+ const require = createRequire ( import . meta. url )
27+ const { ocr : AipOcrClient } = require ( 'baidu-aip-sdk' )
2528
2629const AipOcrClientMap = { }
2730const apis = [
@@ -31,7 +34,7 @@ const apis = [
3134]
3235const limitMap = { }
3336
34- function createBaiduOcrClient ( config ) {
37+ function createBaiduOcrClient ( config ) {
3538 const key = config . id
3639 if ( AipOcrClientMap [ key ] ) {
3740 return AipOcrClientMap [ key ]
@@ -43,7 +46,7 @@ function createBaiduOcrClient (config) {
4346
4447let count = 0
4548
46- function getConfig ( interceptOpt , tryCount , log ) {
49+ function getConfig ( interceptOpt , tryCount , log ) {
4750 tryCount = tryCount || 1
4851
4952 let config
@@ -96,99 +99,97 @@ function getConfig (interceptOpt, tryCount, log) {
9699 return config
97100}
98101
99- function limitConfig ( id , api ) {
102+ function limitConfig ( id , api ) {
100103 const key = `${ id } _${ api } `
101104 limitMap [ key ] = getTomorrow ( )
102105 // limitMap[key] = Date.now() + 5000 // 测试用,5秒后解禁
103106}
104107
105- function checkIsLimitConfig ( id , api ) {
108+ function checkIsLimitConfig ( id , api ) {
106109 const key = `${ id } _${ api } `
107110 const limitTime = limitMap [ key ]
108111 return limitTime && limitTime > Date . now ( )
109112}
110113
111- module . exports = {
112- name : 'baiduOcr' ,
113- priority : 131 ,
114- requestIntercept ( context , interceptOpt , req , res , ssl , next ) {
115- const { rOptions, log } = context
114+ export const name = 'baiduOcr'
115+ export const priority = 131
116+ export function requestIntercept ( context , interceptOpt , req , res , ssl , next ) {
117+ const { rOptions, log } = context
116118
117- const headers = {
118- 'Content-Type' : 'application/json; charset=utf-8' ,
119- 'Access-Control-Allow-Origin' : '*' ,
120- }
119+ const headers = {
120+ 'Content-Type' : 'application/json; charset=utf-8' ,
121+ 'Access-Control-Allow-Origin' : '*' ,
122+ }
121123
122- // 获取配置
123- const config = getConfig ( interceptOpt , null , log )
124- if ( ! config ) {
125- res . writeHead ( 200 , headers )
126- res . write ( '{"error_code": 99917, "error_msg": "dev-sidecar中,未配置百度云账号,或所有百度云账号的免费额度都已用完!!!"}' )
127- res . end ( )
128- return true
129- }
130- if ( ! config . id || ! config . ak || ! config . sk ) {
131- res . writeHead ( 200 , headers )
132- res . write ( '{"error_code": 999500, "error_msg": "dev-sidecar中,baiduOcr的 id 或 ak 或 sk 配置为空"}' )
133- res . end ( )
134- return true
135- }
124+ // 获取配置
125+ const config = getConfig ( interceptOpt , null , log )
126+ if ( ! config ) {
127+ res . writeHead ( 200 , headers )
128+ res . write ( '{"error_code": 99917, "error_msg": "dev-sidecar中,未配置百度云账号,或所有百度云账号的免费额度都已用完!!!"}' )
129+ res . end ( )
130+ return true
131+ }
132+ if ( ! config . id || ! config . ak || ! config . sk ) {
133+ res . writeHead ( 200 , headers )
134+ res . write ( '{"error_code": 999500, "error_msg": "dev-sidecar中,baiduOcr的 id 或 ak 或 sk 配置为空"}' )
135+ res . end ( )
136+ return true
137+ }
136138
137- headers [ 'DS-Interceptor' ] = `baiduOcr: id=${ config . id } , api=${ config . api || apis [ 0 ] } , account=${ config . account } `
139+ headers [ 'DS-Interceptor' ] = `baiduOcr: id=${ config . id } , api=${ config . api || apis [ 0 ] } , account=${ config . account } `
138140
139- // 获取图片的base64编码
140- let imageBase64 = rOptions . path . substring ( rOptions . path . indexOf ( '?' ) + 1 )
141- if ( ! imageBase64 ) {
142- res . writeHead ( 200 , headers )
143- res . write ( '{"error_code": 999400, "error_msg": "图片Base64参数为空"}' )
144- res . end ( )
145- return true
146- }
147- imageBase64 = decodeURIComponent ( imageBase64 )
148-
149- // 调用百度云 “文字识别” 相关接口,根据 `config.api` 调用不同的接口
150- const client = createBaiduOcrClient ( config )
151- const options = {
152- recognize_granularity : 'big' ,
153- detect_direction : 'false' ,
154- paragraph : 'false' ,
155- probability : 'false' ,
156- ...( config . options || { } ) ,
157- }
158- log . info ( '发起百度ocr请求' , req . hostname )
159- client [ config . api || apis [ 0 ] ] ( imageBase64 , options ) . then ( ( result ) => {
160- if ( result . error_code != null ) {
161- log . error ( 'baiduOcr error:' , result )
162- if ( result . error_code === 17 ) {
163- // 当前百度云账号,达到当日调用次数上限
164- limitConfig ( config . id , config . api )
165- log . error ( `当前百度云账号的接口 ${ config . api } ,已达到当日调用次数上限,暂时禁用它,明天会自动放开:` , config )
166- }
167- } else {
168- log . info ( 'baiduOcr success:' , result )
141+ // 获取图片的base64编码
142+ let imageBase64 = rOptions . path . substring ( rOptions . path . indexOf ( '?' ) + 1 )
143+ if ( ! imageBase64 ) {
144+ res . writeHead ( 200 , headers )
145+ res . write ( '{"error_code": 999400, "error_msg": "图片Base64参数为空"}' )
146+ res . end ( )
147+ return true
148+ }
149+ imageBase64 = decodeURIComponent ( imageBase64 )
150+
151+ // 调用百度云 “文字识别” 相关接口,根据 `config.api` 调用不同的接口
152+ const client = createBaiduOcrClient ( config )
153+ const options = {
154+ recognize_granularity : 'big' ,
155+ detect_direction : 'false' ,
156+ paragraph : 'false' ,
157+ probability : 'false' ,
158+ ...( config . options || { } ) ,
159+ }
160+ log . info ( '发起百度ocr请求' , req . hostname )
161+ client [ config . api || apis [ 0 ] ] ( imageBase64 , options ) . then ( ( result ) => {
162+ if ( result . error_code != null ) {
163+ log . error ( 'baiduOcr error:' , result )
164+ if ( result . error_code === 17 ) {
165+ // 当前百度云账号,达到当日调用次数上限
166+ limitConfig ( config . id , config . api )
167+ log . error ( `当前百度云账号的接口 ${ config . api } ,已达到当日调用次数上限,暂时禁用它,明天会自动放开:` , config )
169168 }
169+ } else {
170+ log . info ( 'baiduOcr success:' , result )
171+ }
170172
171- res . writeHead ( 200 , headers )
172- res . write ( JSON . stringify ( result ) ) // 格式如:{"words_result":[{"words":"6525"}],"words_result_num":1,"log_id":1818877093747960000}
173- res . end ( )
174- if ( next ) {
175- next ( ) // 异步执行完继续next
176- }
177- } ) . catch ( ( err ) => {
178- log . error ( 'baiduOcr error:' , err )
179- res . writeHead ( 200 , headers )
180- res . write ( `{"error_code": 999500, "error_msg": "${ err } "}` ) // 格式如:{"words_result":[{"words":"6525"}],"words_result_num":1,"log_id":1818877093747960000}
181- res . end ( )
182- if ( next ) {
183- next ( ) // 异步执行完继续next
184- }
185- } )
173+ res . writeHead ( 200 , headers )
174+ res . write ( JSON . stringify ( result ) ) // 格式如:{"words_result":[{"words":"6525"}],"words_result_num":1,"log_id":1818877093747960000}
175+ res . end ( )
176+ if ( next ) {
177+ next ( ) // 异步执行完继续next
178+ }
179+ } ) . catch ( ( err ) => {
180+ log . error ( 'baiduOcr error:' , err )
181+ res . writeHead ( 200 , headers )
182+ res . write ( `{"error_code": 999500, "error_msg": "${ err } "}` ) // 格式如:{"words_result":[{"words":"6525"}],"words_result_num":1,"log_id":1818877093747960000}
183+ res . end ( )
184+ if ( next ) {
185+ next ( ) // 异步执行完继续next
186+ }
187+ } )
186188
187- log . info ( 'proxy baiduOcr: hostname:' , req . hostname )
189+ log . info ( 'proxy baiduOcr: hostname:' , req . hostname )
188190
189- return 'no-next'
190- } ,
191- is ( interceptOpt ) {
192- return ! ! interceptOpt . baiduOcr
193- } ,
191+ return 'no-next'
192+ }
193+ export function is ( interceptOpt ) {
194+ return ! ! interceptOpt . baiduOcr
194195}
0 commit comments