Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 2bd674c

Browse files
committed
feat: support multiple tencent ocr regions
1 parent beac66c commit 2bd674c

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

src/pages/Background/common/ocr-client.ts renamed to src/common/ocr-client.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import defaultsDeep from 'lodash-es/defaultsDeep'
22
import { SHA256, HmacSHA256, enc } from 'crypto-js'
33
import axios from 'axios'
44

5+
export const OcrRegions = {
6+
'ap-shanghai': '华东地区(上海)',
7+
'ap-beijing': '华北地区(北京)',
8+
'ap-guangzhou': '华南地区(广州)',
9+
'ap-hongkong': '港澳台地区(中国香港)',
10+
'ap-seoul': '亚太东北(首尔)',
11+
'ap-singapore': '亚太东南(新加坡)',
12+
'na-toronto': '北美地区(多伦多)',
13+
}
14+
export type OcrRegionKeys = keyof typeof OcrRegions
15+
516
export class TcRequestError extends Error {
617
code?: string
718

@@ -16,7 +27,7 @@ export class OcrClient {
1627
private config: {
1728
secretId: string
1829
secretKey: string
19-
region: string
30+
region: OcrRegionKeys
2031
}
2132
private requestConfig = {
2233
host: 'ocr.tencentcloudapi.com',
@@ -31,7 +42,11 @@ export class OcrClient {
3142
signedHeaders: 'content-type;host',
3243
}
3344

34-
constructor(config: { secretId: string; secretKey: string; host?: string }) {
45+
constructor(config: {
46+
secretId: string
47+
secretKey: string
48+
region?: OcrRegionKeys
49+
}) {
3550
this.config = defaultsDeep({}, config, {
3651
region: 'ap-shanghai',
3752
})

src/common/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { supportedLanguages, supportedRegions } from './constant'
2+
import { OcrRegionKeys } from './ocr-client'
23

34
export interface Config {
45
token: string
56
targetLang: SupportLanguageKeys
67
region: APIRegions
78
ocrSecretId?: string
89
ocrSecretKey?: string
10+
ocrRegion?: OcrRegionKeys
911
hoverButton?: boolean
1012
}
1113

src/pages/Background/common/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createServer } from 'connect.io'
44
import Client from '../../../common/api'
55
import logger from '../../../common/logger'
66
import { Config } from '../../../common/types'
7-
import { OcrClient } from './ocr-client'
7+
import { OcrClient } from '../../../common/ocr-client'
88
import { Handler } from './types'
99
import { cropImage } from './utils'
1010

@@ -107,6 +107,7 @@ const onOCR: Handler<{
107107
const client = new OcrClient({
108108
secretId: config.ocrSecretId,
109109
secretKey: config.ocrSecretKey,
110+
region: config.ocrRegion,
110111
})
111112
const data = await client.request({ dataUrl: payload.dataUrl })
112113

src/pages/Options/Options.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useSnackbar } from 'notistack'
1111

1212
import Client from '../../common/api'
1313
import { supportedLanguages, supportedRegions } from '../../common/constant'
14+
import { OcrRegionKeys, OcrRegions } from '../../common/ocr-client'
1415
import {
1516
APIRegions,
1617
Config,
@@ -29,6 +30,7 @@ const Options: React.FC = () => {
2930
const [region, setRegion] = useState<APIRegions>('default')
3031
const [ocrSecretId, setOCRSecretId] = useState('')
3132
const [ocrSecretKey, setOCRSecretKey] = useState('')
33+
const [ocrRegion, setOCRRegion] = useState<OcrRegionKeys>('ap-shanghai')
3234
const [hoverButton, setHoverButton] = useState(true)
3335
const { enqueueSnackbar } = useSnackbar()
3436

@@ -41,6 +43,7 @@ const Options: React.FC = () => {
4143
region,
4244
ocrSecretId,
4345
ocrSecretKey,
46+
ocrRegion,
4447
hoverButton,
4548
})
4649

@@ -76,6 +79,7 @@ const Options: React.FC = () => {
7679
if (config.ocrSecretId !== undefined) setOCRSecretId(config.ocrSecretId)
7780
if (config.ocrSecretKey !== undefined)
7881
setOCRSecretKey(config.ocrSecretKey)
82+
if (config.ocrRegion !== undefined) setOCRRegion(config.ocrRegion)
7983
if (config.hoverButton !== undefined) setHoverButton(config.hoverButton)
8084
})
8185
}, [])
@@ -113,7 +117,7 @@ const Options: React.FC = () => {
113117
onSubmit={onSubmit}
114118
tw="flex flex-col justify-between flex-1 overflow-hidden">
115119
<div tw="space-y-6 p-5 overflow-auto">
116-
<OptionSection title={'目标语言'}>
120+
<OptionSection title={'默认目标语言'}>
117121
<select
118122
tw="px-4 pl-3 pr-8 rounded-md"
119123
css={css`
@@ -181,6 +185,25 @@ const Options: React.FC = () => {
181185
/>
182186
</div>
183187

188+
<div>
189+
<select
190+
tw="px-4 pl-3 pr-8 rounded-md"
191+
css={css`
192+
background-position: right 0.3rem center;
193+
`}
194+
name="ocr-region"
195+
value={ocrRegion}
196+
onChange={(e) =>
197+
setOCRRegion(e.target.value as OcrRegionKeys)
198+
}>
199+
{Object.keys(OcrRegions).map((region, index) => (
200+
<option value={region} key={index}>
201+
{OcrRegions[region as OcrRegionKeys]}
202+
</option>
203+
))}
204+
</select>
205+
</div>
206+
184207
<div tw="text-sm text-gray-600">
185208
可不填,填入后可使用 OCR 识别文字翻译。
186209
</div>

0 commit comments

Comments
 (0)