Files
2026-06-02 15:28:25 +08:00

167 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 统一扫码模块
* 支持 安卓设备 / iData 专用 / 通用 Android 手动配置
*
* === 使用方式 ===
*
* import scan from '@/utils/scan.js'
*
* onLoad() {
* scan.setMode('uniapp') // 'uniapp' | 'idata' | 'android'(默认 uniapp
* scan.init()
* scan.register((codeStr) => {
* console.log('扫码结果:', codeStr)
* })
* },
* onUnload() {
* scan.unregister()
* }
*
* === API ===
* scan.setMode(mode) - 设置模式:'uniapp' | 'idata' | 'android'
* scan.init() - 初始化扫码模块
* scan.register(cb) - 注册回调:cb(codeStr)
* scan.unregister() - 注销回调(页面级)
* scan.startScan() - 开始扫描
* scan.stopScan() - 停止扫描
* scan.isScanning() - 是否正在扫描
* scan.destroy() - 完全销毁(应用级)
*/
// 模式常量
var MODE = {
UNIAPP: 'uniapp',
IDATA: 'idata',
ANDROID: 'android'
}
// 当前模式,默认 uniapp(安卓设备)
var currentMode = MODE.UNIAPP
// 实际实现模块(延迟加载)
var impl = null
/**
* 设置扫码模式(必须在 init() 之前调用)
* @param {'uniapp'|'idata'|'android'} mode
*/
function setMode(mode) {
var validModes = [MODE.UNIAPP, MODE.IDATA, MODE.ANDROID]
if (validModes.indexOf(mode) === -1) {
console.error('[Scan] 不支持的模式: ' + mode + ',可选: uniapp, idata, android')
return
}
// 如果已初始化,先销毁再重新设置模式
if (impl) {
destroy()
}
currentMode = mode
}
/**
* 初始化扫码模块
* 根据当前配置的模式初始化对应的实现
*/
function init() {
// 如果已初始化,先销毁再重建
if (impl) {
impl.destroy()
impl = null
}
if (currentMode === MODE.UNIAPP) {
// 安卓设备:使用 uni-app 默认扫码
var ScanUniappImpl = require('./scan-uniapp.js').default
impl = new ScanUniappImpl()
} else if (currentMode === MODE.IDATA) {
var iDataModule = uni.requireNativePlugin('iData-BarcodePlugin-BarcodeModule')
if (!iDataModule) {
throw new Error('[Scan] iData 插件加载失败,请确认已安装 iData-BarcodePlugin-BarcodeModule')
}
var ScanIdataImpl = require('./scan-idata.js').default
impl = new ScanIdataImpl(iDataModule)
} else {
// 带扫码PDA设备:使用 Android 系统扫码 API
try {
plus.android.importClass('android.device.ScanDevice')
} catch (e) {
throw new Error('[Scan] Android 系统扫码 API 加载失败: ' + e.message)
}
var ScanAndroidImpl = require('./scan-android.js').default
impl = new ScanAndroidImpl()
}
impl.init()
}
/**
* 注册扫码结果回调
* @param {Function} callback - (codeStr: string) => void
*/
function register(callback) {
if (!impl) {
console.error('[Scan] 请先调用 init() 初始化扫码模块')
return
}
impl.register(callback)
}
/**
* 注销监听(页面卸载时调用)
*/
function unregister() {
if (impl) {
impl.unregister()
}
}
/**
* 开始扫描
*/
function startScan() {
if (!impl) {
console.error('[Scan] 请先调用 init() 初始化扫码模块')
return
}
impl.startScan()
}
/**
* 停止扫描
*/
function stopScan() {
if (!impl) {
return
}
impl.stopScan()
}
/**
* 是否正在扫描
* @returns {boolean}
*/
function isScanning() {
return impl ? impl.isScanning() : false
}
/**
* 完全销毁扫码模块(应用退出时调用)
*/
function destroy() {
if (impl) {
impl.destroy()
impl = null
}
}
export default {
setMode: setMode,
init: init,
register: register,
unregister: unregister,
startScan: startScan,
stopScan: stopScan,
isScanning: isScanning,
destroy: destroy
}