feat(pda): connect online picking workflow
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
function hash32(value, seed) {
|
||||
let hash = seed >>> 0
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
hash ^= value.charCodeAt(index)
|
||||
hash = Math.imul(hash, 0x01000193) >>> 0
|
||||
}
|
||||
return hash.toString(16).padStart(8, '0')
|
||||
}
|
||||
|
||||
export function shortStableHash(value) {
|
||||
const input = String(value ?? '')
|
||||
return `${hash32(input, 0x811c9dc5)}${hash32(input, 0x9e3779b9)}`
|
||||
}
|
||||
|
||||
export function stableOperationId(scope, identity, payload) {
|
||||
const safeScope = String(scope || 'operation').replace(/[^A-Za-z0-9_-]/g, '-').slice(0, 24)
|
||||
const safeIdentity = String(identity || 'unknown').replace(/[^A-Za-z0-9_-]/g, '-').slice(0, 48)
|
||||
return `${safeScope}-${safeIdentity}-${shortStableHash(JSON.stringify(payload ?? null))}`.slice(0, 120)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user