0602
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* iData 扫码插件实现
|
||||
* 使用 iData-BarcodePlugin-BarcodeModule + globalEvent
|
||||
*/
|
||||
|
||||
class ScanIdataImpl {
|
||||
constructor(iDataModule) {
|
||||
this.barcodeModule = iDataModule
|
||||
this.globalEvent = null
|
||||
this._callback = null
|
||||
this._isScanning = false
|
||||
this._isInitialized = false
|
||||
this._lastScanTime = 0
|
||||
this._debounceTime = 500 // 防抖间隔 ms
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化扫码模块
|
||||
*/
|
||||
init() {
|
||||
if (!this.barcodeModule) {
|
||||
throw new Error('[Scan-iData] 扫码模块不可用')
|
||||
}
|
||||
|
||||
this.globalEvent = uni.requireNativePlugin('globalEvent')
|
||||
|
||||
var self = this
|
||||
this.barcodeModule.initScan(function (res) {
|
||||
console.log('[Scan-iData] 初始化结果:', res)
|
||||
self._isInitialized = true
|
||||
self.barcodeModule.setScanKeyEnable(true, function () {})
|
||||
self._setupListener()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置扫码结果监听
|
||||
*/
|
||||
_setupListener() {
|
||||
var self = this
|
||||
this.globalEvent.addEventListener('iDataBarcodeEvent', function (res) {
|
||||
console.log('[Scan-iData] 扫码事件:', res)
|
||||
|
||||
if (res && res.barcode) {
|
||||
// 防抖处理
|
||||
var now = Date.now()
|
||||
if (now - self._lastScanTime < self._debounceTime) {
|
||||
console.log('[Scan-iData] 防抖过滤')
|
||||
return
|
||||
}
|
||||
self._lastScanTime = now
|
||||
|
||||
self._isScanning = false
|
||||
if (self._callback) {
|
||||
self._callback(res.barcode)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册扫码结果回调
|
||||
* @param {Function} callback - (codeStr: string) => void
|
||||
*/
|
||||
register(callback) {
|
||||
this._callback = callback
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销监听(页面卸载时调用)
|
||||
* 注意:iData 模式下不注销 globalEvent,只清除回调
|
||||
*/
|
||||
unregister() {
|
||||
this._callback = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始扫描
|
||||
*/
|
||||
startScan() {
|
||||
if (!this._isInitialized || !this.barcodeModule) return
|
||||
|
||||
var self = this
|
||||
this.barcodeModule.setScanKeyEnable(true, function () {
|
||||
self.barcodeModule.scanStart(function () {
|
||||
self._isScanning = true
|
||||
console.log('[Scan-iData] 扫码已启动')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止扫描
|
||||
*/
|
||||
stopScan() {
|
||||
if (!this.barcodeModule) return
|
||||
|
||||
var self = this
|
||||
this.barcodeModule.setScanKeyEnable(false, function () {})
|
||||
this.barcodeModule.scanStop(function () {
|
||||
self._isScanning = false
|
||||
console.log('[Scan-iData] 扫码已停止')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否正在扫描
|
||||
*/
|
||||
isScanning() {
|
||||
return this._isScanning
|
||||
}
|
||||
|
||||
/**
|
||||
* 完全销毁(应用退出时调用)
|
||||
*/
|
||||
destroy() {
|
||||
this.unregister()
|
||||
if (this.barcodeModule) {
|
||||
this.barcodeModule.setScanKeyEnable(false, function () {})
|
||||
this.barcodeModule.scanStop(function () {})
|
||||
this.barcodeModule.closeScan(function () {})
|
||||
this.barcodeModule = null
|
||||
}
|
||||
if (this.globalEvent) {
|
||||
this.globalEvent.removeEventListener('iDataBarcodeEvent')
|
||||
this.globalEvent = null
|
||||
}
|
||||
this._isInitialized = false
|
||||
}
|
||||
}
|
||||
|
||||
export default ScanIdataImpl
|
||||
Reference in New Issue
Block a user