diff --git a/src/pages/devicesSearch/ocrSearch.nvue b/src/pages/devicesSearch/ocrSearch.nvue index 73088a5..5be1c9e 100644 --- a/src/pages/devicesSearch/ocrSearch.nvue +++ b/src/pages/devicesSearch/ocrSearch.nvue @@ -133,138 +133,216 @@ export default { }, codeData: {}, optionList: [], - cameraReady: false + cameraReady: false, + deviceReadyTimeout: null } }, onShow() { - // 更安全的 deviceready 监听 - if (window.cordova) { - this.onDeviceReady(); - } else { - document.addEventListener('deviceready', this.onDeviceReady, false); - } + this.initializeCordova(); }, + + onHide() { + this.cleanup(); + }, + beforeDestroy() { - if (this.onDeviceReady) { - document.removeEventListener('deviceready', this.onDeviceReady); - } + this.cleanup(); }, + methods: { - onDeviceReady() { - console.log('Cordova 插件已加载'); - this.cameraReady = true; + // 初始化Cordova + initializeCordova() { + console.log('开始初始化Cordova...'); + + // 清除之前的超时 + if (this.deviceReadyTimeout) { + clearTimeout(this.deviceReadyTimeout); + } + + // 检查是否已经准备好 + if (window.cordova && document.readyState === 'complete') { + this.onDeviceReady(); + return; + } + + // 监听deviceready事件 + document.addEventListener('deviceready', this.onDeviceReady, false); + + // 设置超时,防止无限等待 + this.deviceReadyTimeout = setTimeout(() => { + console.warn('Cordova初始化超时'); + this.cameraReady = false; + }, 10000); }, + + // 设备准备就绪 + onDeviceReady() { + console.log('Cordova设备准备就绪'); + + // 清除超时 + if (this.deviceReadyTimeout) { + clearTimeout(this.deviceReadyTimeout); + this.deviceReadyTimeout = null; + } + + // 检查相机插件是否可用 + this.checkCameraPlugin(); + }, + + // 检查相机插件 + checkCameraPlugin() { + console.log('检查相机插件...'); + + // 多种方式检查插件 + const pluginAvailable = + (window.cordova && window.cordova.plugins && window.cordova.plugins.CameraPreview) || + (window.CameraPreview) || + (window.plugins && window.plugins.CameraPreview); + + if (pluginAvailable) { + console.log('相机插件可用'); + this.cameraReady = true; + } else { + console.error('相机插件不可用'); + this.cameraReady = false; + + // 延迟重试 + setTimeout(() => { + this.checkCameraPlugin(); + }, 1000); + } + }, + + // 获取相机插件引用 + getCameraPlugin() { + return window.cordova?.plugins?.CameraPreview || + window.CameraPreview || + window.plugins?.CameraPreview; + }, + // 检查相机权限 async checkCameraPermission() { return new Promise((resolve, reject) => { - // 检查插件是否可用 - if (!window.cordova || !cordova.plugins?.permissions) { - reject(new Error('权限插件不可用')); + // 检查权限插件是否可用 + if (!window.cordova?.plugins?.permissions) { + console.warn('权限插件不可用,跳过权限检查'); + resolve(true); return; } - const permissions = cordova.plugins.permissions; + const permissions = window.cordova.plugins.permissions; permissions.checkPermission( permissions.CAMERA, (status) => { if (status.hasPermission) { - resolve(true); // 已有权限 + resolve(true); } else { // 请求权限 permissions.requestPermission( permissions.CAMERA, (status) => { - if (status.hasPermission) { - resolve(true); - } else { - reject(new Error('用户拒绝了相机权限')); - } + resolve(status.hasPermission); }, - (error) => reject(new Error('请求权限失败: ' + error)) + (error) => { + console.error('请求权限失败:', error); + reject(new Error('请求权限失败: ' + error)); + } ); } }, - (error) => reject(new Error('检查权限失败: ' + error)) + (error) => { + console.error('检查权限失败:', error); + reject(new Error('检查权限失败: ' + error)); + } ); }); }, // 打开相机 async openCamera() { + console.log('尝试打开相机...'); + if (!this.cameraReady) { - await uni.showToast({ title: '请等待插件初始化', icon: 'none' }); + uni.showToast({ + title: '相机插件未准备好,请稍后重试', + icon: 'none', + duration: 2000 + }); return; } try { - await this.$nextTick(); // 确保 UI 更新 + // 检查权限 + await this.checkCameraPermission(); + + // 显示相机界面 + this.showCamera = true; + + // 等待UI更新 + await this.$nextTick(); + + // 初始化相机 await this.initCamera(); + } catch (error) { - console.error('相机启动失败:', error); + console.error('打开相机失败:', error); + this.showCamera = false; + uni.showToast({ + title: error.message || '打开相机失败', + icon: 'none', + duration: 2000 + }); } }, // 初始化相机 async initCamera() { + return new Promise((resolve, reject) => { + console.log('初始化相机预览...'); - // 获取相机预览插件的引用 - var CameraPreview = cordova.plugins.CameraPreview; + const CameraPreview = this.getCameraPlugin(); - // 初始化相机预览,并设置预览的容器ID - CameraPreview.startCamera({ - x: 0, - y: 0, - width: window.innerWidth, - height: window.innerHeight, - camera: 'rear', // 'front' 或 'rear' - tapPhoto: true, // 点击拍照 - previewDrag: false, // 是否允许拖动预览框 - toBack: false, // 是否将相机预览置于后台(例如,在其他视图之上) - alpha: 255 // 透明度,范围0-255 - }, function(state) { - console.log(state); // 'started' 或 'failed' + if (!CameraPreview) { + reject(new Error('相机插件不可用')); + return; + } + + const options = { + x: 0, + y: 0, + width: window.innerWidth || 375, + height: window.innerHeight || 667, + camera: 'rear', + tapPhoto: false, + previewDrag: false, + toBack: true, + alpha: 1 + }; + + console.log('相机配置:', options); + + CameraPreview.startCamera( + options, + (result) => { + console.log('相机启动成功:', result); + this.cameraStarted = true; + resolve(); + }, + (error) => { + console.error('相机启动失败:', error); + this.cameraStarted = false; + reject(new Error('相机启动失败: ' + error)); + } + ); }); - - - // return new Promise((resolve, reject) => { - // // 检查插件是否可用 - // if (!window.CameraPreview) { - // reject(new Error('相机插件未加载')); - // return; - // } - // - // const options = { - // x: 0, - // y: 0, - // width: window.innerWidth || 375, - // height: window.innerHeight || 667, - // camera: 'rear', // 后置摄像头 - // tapPhoto: false, - // previewDrag: false, - // toBack: true, // 将预览放到背景 - // alpha: 1, - // colorEffect: 'none' - // }; - // - // window.CameraPreview.startCamera( - // options, - // () => { - // console.log('相机启动成功'); - // this.cameraStarted = true; - // resolve(); - // }, - // (error) => { - // console.error('相机启动失败:', error); - // reject(new Error('相机启动失败: ' + error)); - // } - // ); - // }); }, // 关闭相机 async closeCamera() { + console.log('关闭相机...'); + try { await this.stopCamera(); } catch (error) { @@ -277,12 +355,18 @@ export default { // 停止相机 async stopCamera() { - if (!this.cameraStarted || !window.CameraPreview) { + if (!this.cameraStarted) { + return Promise.resolve(); + } + + const CameraPreview = this.getCameraPlugin(); + + if (!CameraPreview) { return Promise.resolve(); } return new Promise((resolve) => { - window.CameraPreview.stopCamera( + CameraPreview.stopCamera( () => { console.log('相机已停止'); this.cameraStarted = false; @@ -303,9 +387,16 @@ export default { return; } + console.log('开始拍照...'); this.isTaking = true; try { + const CameraPreview = this.getCameraPlugin(); + + if (!CameraPreview) { + throw new Error('相机插件不可用'); + } + const imageData = await new Promise((resolve, reject) => { const options = { quality: 85, @@ -313,20 +404,23 @@ export default { targetWidth: 1080 }; - window.CameraPreview.takePicture( + CameraPreview.takePicture( options, (imageData) => { + console.log('拍照成功'); resolve(imageData); }, (error) => { + console.error('拍照失败:', error); reject(new Error('拍照失败: ' + error)); } ); }); await this.processImage(imageData); + } catch (error) { - console.error('拍照失败:', error); + console.error('拍照过程出错:', error); uni.showToast({ title: error.message || '拍照失败', icon: 'none' @@ -341,6 +435,8 @@ export default { this.isProcessing = true; try { + console.log('开始OCR识别...'); + const response = await new Promise((resolve, reject) => { uni.request({ url: '/material/app/ocr/getOcrCode', @@ -350,7 +446,7 @@ export default { jiju_type: '', auth_lic: 'xIWDlaDVdijcBB4mjhGCPYk5Kvk8tHZJbUn+vW+ih15+MYx98e/PXyBmKL5gFcWMPznLgDA15QuSAnZQSLddwdy9HkZgtuQDEEZZ351Eyb1eiDUccUnyoSGIrNimbx5TooBNNPYqU4qJeFrPJXAqjBHzRrxoBxuR2CEGKQPgHC4=' }, - timeout: 30000, // 30秒超时 + timeout: 30000, success: resolve, fail: reject }); @@ -370,6 +466,7 @@ export default { icon: 'none' }); } + } catch (error) { console.error('OCR识别失败:', error); uni.showToast({ @@ -388,7 +485,6 @@ export default { sourceType: ['album'], success: (res) => { if (res.tempFilePaths && res.tempFilePaths.length > 0) { - // 将本地图片转换为base64 this.convertImageToBase64(res.tempFilePaths[0]); } }, @@ -417,7 +513,6 @@ export default { // #endif // #ifndef APP-PLUS - // 其他平台可以直接使用文件路径 this.processImage(filePath); // #endif }, @@ -499,11 +594,22 @@ export default { } }, - // 处理OCR结果 - handleOcrResult(result) { - if (result && result.code) { - this.queryCodeParams.maCode = result.code; - this.getCode(); + // 清理资源 + cleanup() { + console.log('清理资源...'); + + // 清除超时 + if (this.deviceReadyTimeout) { + clearTimeout(this.deviceReadyTimeout); + this.deviceReadyTimeout = null; + } + + // 移除事件监听 + document.removeEventListener('deviceready', this.onDeviceReady); + + // 停止相机 + if (this.cameraStarted) { + this.stopCamera().catch(console.error); } } }