import { Message } from 'element-ui' // 蓝牙 export const BLUETOOTH = 'BLUETOOTH' // 扫码 export const SCAN_CODE = 'SCAN_CODE' // 调用相册 export const PHOTO = 'PHOTO' // 相机 export const CAMERA = 'CAMERA' // 当前是否在app中 export const isInApp = !!window.android // 调用js-bridge function JsBridge() { this.onShowCallback = null this.onHideCallback = null this.bluetoothCallback = [] window.callJSAPPStatus = (result) => { // 如果当前是展示状态,则展示onshow方法 if (result) { this.onShowCallback && this.onShowCallback() } else { this.onHideCallback && this.onHideCallback() } } /** * 监听报错信息 * @param errorMessage 报错信息 */ window.callJSErrorInfo = errorMessage => { errorMessage && Message.error(errorMessage.msg) } } /** * 将base64转换成文件 * @param dataurl base64地址 * @param filename 文件名 */ export const base64toFile = (dataurl, filename = 'file') => { const arr = dataurl.split(',') const mime = arr[0].match(/:(.*?);/)[1] const suffix = mime.split('/')[1] const bstr = atob(arr[1]) let n = bstr.length const u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new File([u8arr], `${filename}.${suffix}`, { type: mime }) } /** * 调用相机 * @param callback 回调方法 */ JsBridge.prototype.getCamera = function(callback) { // 如果不在app中 if (!isInApp) { return } // 回调函数 window.callJSImages = async(result) => { try { // 执行回调 callback(base64toFile(`data:image/jpeg;base64,${result.imageBase64}`)) // 清空回调函数 window.callJSImages = null } catch (error) { console.error(error) } } // 调用bridge方法 window.android.getAndroidCamera() } /** * uvc的方式调用相机 */ JsBridge.prototype.getUvcCamera = function(callback) { if (!isInApp) { return } // 执行回调方法 window.callJSUvcCameras = ({ type, file }) => { // 文件类型 const fileType = type === 'MODE_CAPTURE_VIDEO' ? 'video' : 'image' console.log('当前文件类型:', fileType) try { callback( file ? base64toFile(`${fileType === 'video' ? 'data:video/mp4;base64,' : 'data:image/jpeg;base64,'}${file}`) : null, fileType ) window.callJSUvcCameras = null } catch (error) { console.error('callJSUvcCameras: ', error) } } // 调用bridge方法 window.android.openUvcCamera() } /** * 获取设备列表 */ JsBridge.prototype.getUvcCameraList = function(callback) { // 设备列表 let cameraList = [] try { cameraList = JSON.parse(window.android.getUvcCameraList()) } catch (error) { console.error(error) } callback(cameraList) } /** * 打开指定的相机 */ JsBridge.prototype.openTargetUvcCamera = function(device, callback) { // 执行回调方法 window.callJSUvcCameras = ({ type, file }) => { // 文件类型 const fileType = type === 'MODE_CAPTURE_VIDEO' ? 'video' : 'image' try { callback( file ? base64toFile(`${fileType === 'video' ? 'data:video/mp4;base64,' : 'data:image/jpeg;base64,'}${file}`) : null, fileType ) window.callJSUvcCameras = null } catch (error) { console.error('callJSUvcCameras: ', error) } } window.android.openTargetUvcCamera(JSON.stringify(device)) } /** * 调用相册 * @param callback 回调方法 */ JsBridge.prototype.getPhoto = function(callback) { // 如果不在app中 if (!isInApp) { return } // 回调函数 window.callJSImages = async(result) => { // 执行回调 callback(base64toFile(`data:image/jpeg;base64,${result.imageBase64}`)) // 清空回调函数 window.callJSImages = null } // 调用bridge方法 window.android.getAndroidPhoto(1) } /** * 调用相册 * @param isFace 是否是人脸面 * @param callback 回调方法 */ JsBridge.prototype.takeIDCardPhoto = function(isFace = true, callback) { // 如果不在app中 if (!isInApp) { return } // 回调函数 window.callJSImages = async(result) => { // 执行回调 callback(base64toFile(`data:image/jpeg;base64,${result.imageBase64}`)) // 清空回调函数 window.callJSImages = null } // 调用bridge方法 window.android.takeIDCardPhoto(isFace) } /** * 调用相机,扫条形码二维码 * @param callback 回调方法 */ JsBridge.prototype.scanCode = function(isBarcode, callback) { // 如果不在app中 if (!isInApp) { return } // 先创建回调 window.callJSCodeScan = result => { callback(result) window.callJSCodeScan = null } // 调用bridge方法 window.android.getAndroidCodeScan(true, -1) } /** * 从设备获取数据 * @param callback 回调方法 */ JsBridge.prototype.getDeviceData = function(callback) { // 如果不在app中 if (!isInApp) { return } // 设备链接状态 window.callJSBluetooth = (result) => { // 执行回调 callback(result) // 清空回调 window.callJSBluetooth = null } // 调用bridge方法 window.android.getAndroidBluetooth() } /** * 查看设备链接状态 * @param callback 回调方法 */ JsBridge.prototype.deviceConnectStatus = function(callback) { // 如果不在app中 if (!isInApp) { return } // 设备链接状态 window.callJSDeviceStatus = (appstatus) => { // 执行回调 callback(appstatus) // 清空回调 window.callJSDeviceStatus = null } // 校验设备状态 window.android.getAndroidDeviceStatus() } /** * 页面处于展示状态 * @param callback 回调方法 */ JsBridge.prototype.onShow = function(callback) { // 如果不在app中 if (!isInApp) { return } this.onShowCallback = callback // 默认先执行一次onshow事件 this.onShowCallback() } /** * 页面处于隐藏状态 * @param callback 回调方法 */ JsBridge.prototype.onHide = function(callback) { // 如果不在app中 if (!isInApp) { return } // 绑定页面离开方法 this.onHideCallback = callback } /** * 获取当前nfc设备的设备数据 */ JsBridge.prototype.readIDCardByOTG = function(callback) { // 如果不在app中 if (!isInApp) { return } // 设备链接状态 window.callJSBluetooth = (result) => { // 执行回调 callback(result) // 清空回调 window.callJSBluetooth = null } // 调用bridge方法 window.android.readIDCardByOTG() } /** * 获取当前nfc设备的设备数据 */ JsBridge.prototype.geOtgReadType = function(callback) { // 如果不在app中 if (!isInApp) { return } // 调用bridge方法 callback(window.android.geOtgReadType()) } /** * 选择蓝牙设备 */ JsBridge.prototype.chooseBluetoothDevice = function(callback) { // 如果不在app中 if (!isInApp) { return } // 设备链接状态 window.callJSBluetooth = (result) => { // 执行回调 callback(result) // 清空回调 window.callJSBluetooth = null } // 调用bridge方法 window.android.choiceBluetoothDevice() } JsBridge.prototype.takeVideo = function(callback) { // 如果不在app中 if (!isInApp) { return } // 回调函数 window.callJSVideos = (result) => { try { // 执行回调 callback(result.file ? base64toFile(`data:video/mp4;base64,${result.file}`) : null) // 清空回调函数 window.callJSVideos = null } catch (error) { console.error(error) } } // 调用bridge方法 window.android.getAndroidVideo() } /** * 将base64的文件保存到本地 * @param base64 文件的base64编码 * @param fileName 文件名 */ JsBridge.prototype.saveFileByBase64 = function(base64, fileName) { // 如果不在app中 if (!isInApp) { return } window.android.saveFile(base64, fileName) } export default new JsBridge()