Skip to content

Commit 7e8f420

Browse files
committed
fix: 修复部分导入问题
1 parent 812969e commit 7e8f420

File tree

9 files changed

+183
-154
lines changed

9 files changed

+183
-154
lines changed

packages/core/src/merge.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import lodash from 'lodash'
77
* @param newObj
88
* @returns {{}|*}
99
*/
10-
function doDiff (oldObj, newObj) {
10+
export function doDiff(oldObj, newObj) {
1111
if (newObj == null) {
1212
return oldObj
1313
}
@@ -63,7 +63,7 @@ function doDiff (oldObj, newObj) {
6363
return diffObj
6464
}
6565

66-
function deleteNullItems (target) {
66+
export function deleteNullItems(target) {
6767
lodash.forEach(target, (item, key) => {
6868
if (item == null || item === '[delete]') {
6969
delete target[key]
@@ -74,14 +74,16 @@ function deleteNullItems (target) {
7474
})
7575
}
7676

77+
export function doMerge(oldObj, newObj) {
78+
return lodash.mergeWith(oldObj, newObj, (objValue, srcValue) => {
79+
if (lodash.isArray(objValue)) {
80+
return srcValue
81+
}
82+
})
83+
}
84+
7785
export default {
78-
doMerge (oldObj, newObj) {
79-
return lodash.mergeWith(oldObj, newObj, (objValue, srcValue) => {
80-
if (lodash.isArray(objValue)) {
81-
return srcValue
82-
}
83-
})
84-
},
86+
doMerge,
8587
doDiff,
8688
deleteNullItems,
8789
}

packages/core/src/shell/scripts/enable-loopback.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import Shell from '../shell.js'
2-
import extraPath from './extra-path'
2+
import extraPath from './extra-path/index.js'
33
import sudo from '../sudo.js'
44
const execute = Shell.execute
55

66
const executor = {
7-
windows (exec) {
7+
windows(exec) {
88
const loopbackPath = extraPath.getEnableLoopbackPath()
99
const sudoCommand = `"${loopbackPath}"`
1010

1111
return sudo(sudoCommand, { name: 'EnableLoopback' })
1212
},
13-
async linux (exec, { port }) {
13+
async linux(exec, { port }) {
1414
throw new Error('不支持此操作')
1515
},
16-
async mac (exec, { port }) {
16+
async mac(exec, { port }) {
1717
throw new Error('不支持此操作')
1818
},
1919
}

packages/core/src/utils/util.logger.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import path from 'node:path'
22
import log4js from 'log4js'
33
import logOrConsole from './util.log-or-console.js'
4-
import defaultConfig from '../config'
4+
import defaultConfig from '../config/index.js'
55
const configFromFiles = defaultConfig.configFromFiles
66

77
// 日志级别
88
const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
99

10-
function getDefaultConfigBasePath () {
10+
function getDefaultConfigBasePath() {
1111
if (configFromFiles.app.logFileSavePath) {
1212
let logFileSavePath = configFromFiles.app.logFileSavePath
1313
if (logFileSavePath.endsWith('/') || logFileSavePath.endsWith('\\')) {
@@ -36,7 +36,7 @@ const appenderConfig = {
3636
let log = null
3737

3838
// 设置一组日志配置
39-
function log4jsConfigure (categories) {
39+
function log4jsConfigure(categories) {
4040
if (log != null) {
4141
log.error('当前进程已经设置过日志配置,无法再设置更多日志配置:', categories)
4242
return
@@ -65,30 +65,32 @@ function log4jsConfigure (categories) {
6565
log.info(`设置日志配置完成,进程ID: ${process.pid},categories:[${categories}],config:`, JSON.stringify(config))
6666
}
6767

68-
export default {
69-
getLogger (category) {
70-
if (!category) {
71-
if (log) {
72-
log.error('未指定日志类型,无法配置并获取日志对象!!!')
73-
}
74-
throw new Error('未指定日志类型,无法配置并获取日志对象!!!')
68+
export function getLogger(category) {
69+
if (!category) {
70+
if (log) {
71+
log.error('未指定日志类型,无法配置并获取日志对象!!!')
7572
}
73+
throw new Error('未指定日志类型,无法配置并获取日志对象!!!')
74+
}
7675

77-
if (category === 'core' || category === 'gui') {
78-
// core 和 gui 的日志配置,因为它们在同一进程中,所以一起配置,且只能配置一次
79-
if (log == null) {
80-
log4jsConfigure(['core', 'gui'])
81-
}
82-
83-
return log4js.getLogger(category)
84-
} else {
85-
if (log == null) {
86-
log4jsConfigure([category])
87-
} else if (category !== log.category) {
88-
log.error(`当前进程已经设置过日志配置,无法再设置 "${category}" 的配置,先临时返回 "${log.category}" 的 log 进行日志记录。如果与其他类型的日志在同一进程中写入,请参照 core 和 gui 一起配置`)
89-
}
90-
91-
return log
76+
if (category === 'core' || category === 'gui') {
77+
// core 和 gui 的日志配置,因为它们在同一进程中,所以一起配置,且只能配置一次
78+
if (log == null) {
79+
log4jsConfigure(['core', 'gui'])
80+
}
81+
82+
return log4js.getLogger(category)
83+
} else {
84+
if (log == null) {
85+
log4jsConfigure([category])
86+
} else if (category !== log.category) {
87+
log.error(`当前进程已经设置过日志配置,无法再设置 "${category}" 的配置,先临时返回 "${log.category}" 的 log 进行日志记录。如果与其他类型的日志在同一进程中写入,请参照 core 和 gui 一起配置`)
9288
}
93-
},
89+
90+
return log
91+
}
92+
}
93+
94+
export default {
95+
getLogger,
9496
}
Lines changed: 82 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

2629
const AipOcrClientMap = {}
2730
const apis = [
@@ -31,7 +34,7 @@ const apis = [
3134
]
3235
const 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

4447
let 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

Comments
 (0)