- 创建 utils/platform.ts 以提取并规范化设备/操作系统信息 - 将 globalData 中的普通 `system` 字符串替换为结构化的 `platformInfo` 对象 - 推导硬件/软件详情、设备类别和平台标志 - 更新 TypeScript 类型定义以支持新的平台数据类型
82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
const MOBILE_CLIENT_PLATFORMS: ClientPlatformType[] = ['ios', 'android', 'ohos']
|
|
const DESKTOP_CLIENT_PLATFORMS: ClientPlatformType[] = ['windows', 'mac', 'ohos_pc']
|
|
|
|
function normalizeClientPlatform(platform: string): ClientPlatformType {
|
|
switch (platform.toLowerCase()) {
|
|
case 'ios':
|
|
case 'android':
|
|
case 'ohos':
|
|
case 'ohos_pc':
|
|
case 'windows':
|
|
case 'mac':
|
|
case 'devtools':
|
|
return platform.toLowerCase() as ClientPlatformType
|
|
default:
|
|
return 'unknown'
|
|
}
|
|
}
|
|
|
|
function resolveOperatingSystem(platform: ClientPlatformType): OperatingSystemType {
|
|
switch (platform) {
|
|
case 'ios':
|
|
return 'ios'
|
|
case 'android':
|
|
return 'android'
|
|
case 'ohos':
|
|
case 'ohos_pc':
|
|
return 'harmonyos'
|
|
case 'windows':
|
|
return 'windows'
|
|
case 'mac':
|
|
return 'macos'
|
|
case 'devtools':
|
|
return 'devtools'
|
|
default:
|
|
return 'unknown'
|
|
}
|
|
}
|
|
|
|
function resolveDeviceClass(platform: ClientPlatformType): HardwareDeviceClassType {
|
|
if (MOBILE_CLIENT_PLATFORMS.includes(platform)) {
|
|
return 'mobile'
|
|
}
|
|
|
|
if (DESKTOP_CLIENT_PLATFORMS.includes(platform)) {
|
|
return 'desktop'
|
|
}
|
|
|
|
return platform === 'devtools' ? 'devtools' : 'unknown'
|
|
}
|
|
|
|
export function getPlatformInfo(): PlatformInfoType {
|
|
const deviceInfo = wx.getDeviceInfo()
|
|
const appBaseInfo = wx.getAppBaseInfo()
|
|
const rawClientPlatform = deviceInfo.platform || ''
|
|
const clientPlatform = normalizeClientPlatform(rawClientPlatform)
|
|
const deviceClass = resolveDeviceClass(clientPlatform)
|
|
|
|
return {
|
|
hardware: {
|
|
deviceClass,
|
|
brand: deviceInfo.brand || '',
|
|
model: deviceInfo.model || '',
|
|
abi: deviceInfo.abi || '',
|
|
deviceAbi: deviceInfo.deviceAbi || '',
|
|
cpuType: deviceInfo.cpuType || '',
|
|
memorySize: deviceInfo.memorySize || '',
|
|
},
|
|
software: {
|
|
clientPlatform,
|
|
rawClientPlatform,
|
|
operatingSystem: resolveOperatingSystem(clientPlatform),
|
|
system: deviceInfo.system || '',
|
|
wechatVersion: appBaseInfo.version || '',
|
|
baseLibraryVersion: appBaseInfo.SDKVersion || '',
|
|
hostAppId: appBaseInfo.host?.appId || '',
|
|
},
|
|
isMobile: deviceClass === 'mobile',
|
|
isDesktop: deviceClass === 'desktop',
|
|
isDevtools: deviceClass === 'devtools',
|
|
}
|
|
}
|