feat(pda): connect online picking workflow

This commit is contained in:
2026-08-11 11:31:05 +08:00
parent fc1944fb7e
commit be776c8efa
16 changed files with 440 additions and 82 deletions
+19
View File
@@ -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)
}
+46
View File
@@ -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)
}