deviceInfo.ts
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
export interface DeviceIdentity {
model: string
brand: string
manufacturer: string
product: string
device: string
platform: string
}
let cachedDeviceIdentity: DeviceIdentity | null = null
function safeText (value: unknown): string {
return String(value || '').trim()
}
export function getDeviceIdentity (): DeviceIdentity {
if (cachedDeviceIdentity) return cachedDeviceIdentity
const info = (() => {
try {
return uni.getSystemInfoSync()
} catch (_) {
return {} as UniApp.GetSystemInfoResult
}
})()
const identity: DeviceIdentity = {
model: safeText((info as any).model),
brand: safeText((info as any).brand),
manufacturer: '',
product: '',
device: '',
platform: safeText((info as any).platform),
}
// #ifdef APP-PLUS
try {
const Build = plus.android.importClass('android.os.Build')
identity.model = safeText((Build as any).MODEL) || identity.model
identity.brand = safeText((Build as any).BRAND) || identity.brand
identity.manufacturer = safeText((Build as any).MANUFACTURER)
identity.product = safeText((Build as any).PRODUCT)
identity.device = safeText((Build as any).DEVICE)
} catch (_) {}
// #endif
cachedDeviceIdentity = identity
return identity
}
export function getDeviceFingerprint (): string {
const identity = getDeviceIdentity()
return [
identity.brand,
identity.manufacturer,
identity.model,
identity.product,
identity.device,
]
.filter(Boolean)
.join(' | ')
.toLowerCase()
}