提交 fd023536 作者: 方治民

feat: useRuntime 新增 getDeviceInfo 方法实现,兼容 H5/APP

上级 7b72906f
import { APP_UUID_KEY } from '/@/enums/cacheEnum'
import { nanoid } from 'nanoid'
import md5 from 'crypto-js/md5'
interface DeviceInfo {
uuid: string
vendor: string
model: string
}
export interface Runtime { export interface Runtime {
app: Ref<PlusRuntimeWidgetInfo> app: Ref<PlusRuntimeWidgetInfo>
getDeviceInfo: () => Promise<DeviceInfo>
} }
export function useRuntime(): Runtime { export function useRuntime(): Runtime {
...@@ -14,7 +25,48 @@ export function useRuntime(): Runtime { ...@@ -14,7 +25,48 @@ export function useRuntime(): Runtime {
// #endif // #endif
}) })
/**
* 获取设备信息
* 注意: 需要在页面的 onReady 后使用
* @returns 设备信息
*/
function getDeviceInfo(): Promise<DeviceInfo> {
return new Promise((resolve, reject) => {
// #ifdef APP-PLUS
plus.device.getInfo({
success: resolve,
fail: reject,
})
// #endif
// #ifndef APP-PLUS
let uuid = uni.getStorageSync(APP_UUID_KEY)
if (!uuid) {
uuid = md5(nanoid()).toString()
uni.setStorageSync(APP_UUID_KEY, uuid)
}
const result = { uuid, vendor: 'unknown', model: 'unknown' }
const userAgent = navigator.userAgent
const deviceInfo = userAgent.match(/\((.*?)\)/)
if (deviceInfo && deviceInfo.length > 1) {
const info = deviceInfo[1].split(';')
if (/android/i.test(userAgent)) {
if (info.length >= 3) {
result.vendor = info[1].trim()
result.model = info[2].trim()
}
} else {
result.vendor = info[0].trim()
result.model = info[1].trim()
}
}
resolve(result)
// #endif
})
}
return { return {
app, app,
getDeviceInfo,
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论