初始化柏悦物流海外版 PDA
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
export const AUDIO_FEEDBACK_SOURCES = Object.freeze({
|
||||
success: '/static/mp3/success.mp3',
|
||||
error: '/static/mp3/fail.mp3'
|
||||
})
|
||||
|
||||
let audioContext = null
|
||||
|
||||
function getAudioContext() {
|
||||
if (audioContext) return audioContext
|
||||
if (typeof uni === 'undefined' || typeof uni.createInnerAudioContext !== 'function') return null
|
||||
|
||||
audioContext = uni.createInnerAudioContext()
|
||||
audioContext.autoplay = false
|
||||
audioContext.loop = false
|
||||
audioContext.volume = 1
|
||||
return audioContext
|
||||
}
|
||||
|
||||
export function playFeedbackSound(type) {
|
||||
const source = AUDIO_FEEDBACK_SOURCES[type]
|
||||
const player = getAudioContext()
|
||||
if (!source || !player) return false
|
||||
|
||||
try {
|
||||
player.stop()
|
||||
player.src = source
|
||||
player.play()
|
||||
return true
|
||||
} catch (_) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function playSuccessSound() {
|
||||
return playFeedbackSound('success')
|
||||
}
|
||||
|
||||
export function playErrorSound() {
|
||||
return playFeedbackSound('error')
|
||||
}
|
||||
|
||||
export function stopFeedbackSound() {
|
||||
if (!audioContext) return
|
||||
try { audioContext.stop() } catch (_) {}
|
||||
}
|
||||
|
||||
export function destroyFeedbackSound() {
|
||||
if (!audioContext) return
|
||||
try { audioContext.destroy() } catch (_) {}
|
||||
audioContext = null
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
const HOME_URL = '/pages/home/home'
|
||||
|
||||
export function getCurrentRoute() {
|
||||
if (typeof getCurrentPages !== 'function') return ''
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
return currentPage ? currentPage.route || '' : ''
|
||||
}
|
||||
|
||||
export function navigateBackOr(fallbackUrl = HOME_URL) {
|
||||
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
|
||||
if (pages.length > 1) {
|
||||
uni.navigateBack({ fail: () => uni.reLaunch({ url: fallbackUrl }) })
|
||||
return
|
||||
}
|
||||
uni.reLaunch({ url: fallbackUrl })
|
||||
}
|
||||
|
||||
export function navigateToUnique(url) {
|
||||
const route = url.split('?')[0].replace(/^\//, '')
|
||||
if (getCurrentRoute() === route) return
|
||||
uni.navigateTo({ url })
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
let online = true
|
||||
let listening = false
|
||||
const listeners = new Set()
|
||||
|
||||
function notify() {
|
||||
for (const listener of listeners) listener(online)
|
||||
}
|
||||
|
||||
function ensureListening() {
|
||||
if (listening || typeof uni === 'undefined') return
|
||||
listening = true
|
||||
if (typeof uni.onNetworkStatusChange === 'function') {
|
||||
uni.onNetworkStatusChange((result) => {
|
||||
online = Boolean(result.isConnected)
|
||||
notify()
|
||||
})
|
||||
}
|
||||
if (typeof uni.getNetworkType === 'function') {
|
||||
uni.getNetworkType({
|
||||
success: (result) => {
|
||||
online = result.networkType !== 'none'
|
||||
notify()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function isOnline() {
|
||||
ensureListening()
|
||||
return online
|
||||
}
|
||||
|
||||
export function requireOnline(message = '当前网络不可用,请联网后刷新重试') {
|
||||
if (!isOnline()) {
|
||||
const error = new Error(message)
|
||||
error.network = true
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export function watchNetwork(listener) {
|
||||
ensureListening()
|
||||
listeners.add(listener)
|
||||
listener(online)
|
||||
return () => listeners.delete(listener)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
export const SCAN_BROADCAST_ACTION = 'android.intent.action.SCANRESULT'
|
||||
export const SCAN_BROADCAST_EXTRA = 'value'
|
||||
|
||||
const DUPLICATE_WINDOW_MS = 80
|
||||
|
||||
let receiver = null
|
||||
let activity = null
|
||||
let lastScan = { value: '', receivedAt: 0 }
|
||||
|
||||
export function normalizeScanValue(value) {
|
||||
if (value === null || value === undefined) return ''
|
||||
return String(value).trim()
|
||||
}
|
||||
|
||||
export function isDuplicateScan(value, receivedAt = Date.now()) {
|
||||
const duplicate = value === lastScan.value && receivedAt - lastScan.receivedAt < DUPLICATE_WINDOW_MS
|
||||
if (!duplicate) lastScan = { value, receivedAt }
|
||||
return duplicate
|
||||
}
|
||||
|
||||
export function resetScanDeduplication() {
|
||||
lastScan = { value: '', receivedAt: 0 }
|
||||
}
|
||||
|
||||
export function startScanBroadcast({ onScan, onError } = {}) {
|
||||
if (typeof onScan !== 'function') return () => {}
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
try {
|
||||
if (receiver) stopScanBroadcast()
|
||||
|
||||
activity = plus.android.runtimeMainActivity()
|
||||
const IntentFilter = plus.android.importClass('android.content.IntentFilter')
|
||||
const filter = new IntentFilter()
|
||||
filter.addAction(SCAN_BROADCAST_ACTION)
|
||||
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
|
||||
onReceive(context, intent) {
|
||||
try {
|
||||
plus.android.importClass(intent)
|
||||
const value = normalizeScanValue(intent.getStringExtra(SCAN_BROADCAST_EXTRA))
|
||||
if (!value || isDuplicateScan(value)) return
|
||||
onScan(value)
|
||||
} catch (error) {
|
||||
if (typeof onError === 'function') onError(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
const Build = plus.android.importClass('android.os.Build')
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
const Context = plus.android.importClass('android.content.Context')
|
||||
activity.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED)
|
||||
} else {
|
||||
activity.registerReceiver(receiver, filter)
|
||||
}
|
||||
} catch (error) {
|
||||
receiver = null
|
||||
activity = null
|
||||
if (typeof onError === 'function') onError(error)
|
||||
}
|
||||
// #endif
|
||||
|
||||
return stopScanBroadcast
|
||||
}
|
||||
|
||||
export function stopScanBroadcast() {
|
||||
// #ifdef APP-PLUS
|
||||
if (activity && receiver) {
|
||||
try {
|
||||
activity.unregisterReceiver(receiver)
|
||||
} catch (_) {}
|
||||
}
|
||||
// #endif
|
||||
receiver = null
|
||||
activity = null
|
||||
resetScanDeduplication()
|
||||
}
|
||||
Reference in New Issue
Block a user