|
@@ -10,56 +10,61 @@ function A3code() {
|
|
|
|
|
|
const [tit, setTit] = useState('正在尝试识别....')
|
|
|
|
|
|
+ // 异常扫码情况的处理
|
|
|
+ const errTitFu = useCallback((val: string) => {
|
|
|
+ codeReaderRef.current.reset()
|
|
|
+ setCodeErr(true)
|
|
|
+ setTit(val)
|
|
|
+ }, [])
|
|
|
+
|
|
|
const didiRef = useRef<HTMLAudioElement>(null)
|
|
|
|
|
|
const codeReaderRef = useRef(new BrowserMultiFormatReader())
|
|
|
|
|
|
- const decodeFromInputVideoFunc = useCallback((firstDeviceId: string) => {
|
|
|
- codeReaderRef.current.reset()
|
|
|
- codeReaderRef.current.decodeFromInputVideoDeviceContinuously(
|
|
|
- firstDeviceId,
|
|
|
- 'myVideo',
|
|
|
- (result: any, err) => {
|
|
|
- setTit('正在尝试识别...')
|
|
|
- if (result && result.text) {
|
|
|
- const txt: string = result.text
|
|
|
- console.log('txt', txt)
|
|
|
- if (txt) {
|
|
|
- // 识别成功了,播放音频
|
|
|
- if (didiRef.current) {
|
|
|
- // 音频在播放中为 false
|
|
|
- let flag = didiRef.current.paused
|
|
|
-
|
|
|
- console.log('00', flag)
|
|
|
-
|
|
|
- if (!flag) return
|
|
|
- else {
|
|
|
- didiRef.current.play()
|
|
|
- didiRef.current.addEventListener('ended', () => {
|
|
|
- console.log('11播放完毕')
|
|
|
-
|
|
|
- if (txt.includes('HQ_CODE/')) {
|
|
|
- codeReaderRef.current.reset()
|
|
|
- const id = txt.split('/')[1]
|
|
|
- history.replace(`/codeSucc/${id}`)
|
|
|
- } else {
|
|
|
- codeReaderRef.current.reset()
|
|
|
- setCodeErr(true)
|
|
|
- setTit('二维码格式错误,请重试')
|
|
|
- }
|
|
|
- })
|
|
|
+ const decodeFromInputVideoFunc = useCallback(
|
|
|
+ (firstDeviceId: string) => {
|
|
|
+ codeReaderRef.current.reset()
|
|
|
+ codeReaderRef.current.decodeFromInputVideoDeviceContinuously(
|
|
|
+ firstDeviceId,
|
|
|
+ 'myVideo',
|
|
|
+ (result: any, err) => {
|
|
|
+ setTit('正在尝试识别...')
|
|
|
+ if (result && result.text) {
|
|
|
+ const txt: string = result.text
|
|
|
+ console.log('txt', txt)
|
|
|
+ if (txt) {
|
|
|
+ // 识别成功了,播放音频
|
|
|
+ if (didiRef.current) {
|
|
|
+ // 音频在播放中为 false
|
|
|
+ let flag = didiRef.current.paused
|
|
|
+
|
|
|
+ console.log('00', flag)
|
|
|
+
|
|
|
+ if (!flag) return
|
|
|
+ else {
|
|
|
+ didiRef.current.play()
|
|
|
+ didiRef.current.addEventListener('ended', () => {
|
|
|
+ console.log('11播放完毕')
|
|
|
+
|
|
|
+ if (txt.includes('HQ_CODE/')) {
|
|
|
+ codeReaderRef.current.reset()
|
|
|
+ const id = txt.split('/')[1]
|
|
|
+ if (id) history.replace(`/codeSucc/${id}`)
|
|
|
+ else errTitFu('参数错误')
|
|
|
+ } else errTitFu('二维码格式错误,请重试')
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if (err && !err) {
|
|
|
+ errTitFu('识别失败,请重试')
|
|
|
+ }
|
|
|
}
|
|
|
- if (err && !err) {
|
|
|
- codeReaderRef.current.reset()
|
|
|
- setCodeErr(true)
|
|
|
- setTit('识别失败,请重试')
|
|
|
- }
|
|
|
- }
|
|
|
- )
|
|
|
- }, [])
|
|
|
+ )
|
|
|
+ },
|
|
|
+ [errTitFu]
|
|
|
+ )
|
|
|
|
|
|
const openScan = useCallback(async () => {
|
|
|
codeReaderRef.current
|
|
@@ -81,17 +86,15 @@ function A3code() {
|
|
|
decodeFromInputVideoFunc(firstDeviceId)
|
|
|
})
|
|
|
.catch(err => {
|
|
|
- codeReaderRef.current.reset()
|
|
|
- setCodeErr(true)
|
|
|
- setTit('识别失败,请重试')
|
|
|
+ errTitFu('识别失败,请重试')
|
|
|
})
|
|
|
- }, [decodeFromInputVideoFunc])
|
|
|
+ }, [decodeFromInputVideoFunc, errTitFu])
|
|
|
|
|
|
useEffect(() => {
|
|
|
openScan()
|
|
|
}, [openScan])
|
|
|
|
|
|
- // 重新扫描 刷牙页面
|
|
|
+ // 重新扫描 刷新页面
|
|
|
const newCode = useCallback(() => {
|
|
|
window.location.reload()
|
|
|
}, [])
|