diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 4fbb5a2..d284db8 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,4 +1,6 @@ // app.ts +import { getPlatformInfo } from './utils/platform' + App({ globalData: { navBarHeight: 0, // 导航栏总高度 @@ -10,19 +12,43 @@ App({ tabbarHeight: 0, // 底部导航栏高度 safeAreaBottom: 0, // 底部安全区域坐标 safeAreaInsetBottom: 0, // 中间区域内容的底边坐标 - system: '', // 系统信息 + platformInfo: { + hardware: { + deviceClass: 'unknown', + brand: '', + model: '', + abi: '', + deviceAbi: '', + cpuType: '', + memorySize: '', + }, + software: { + clientPlatform: 'unknown', + rawClientPlatform: '', + operatingSystem: 'unknown', + system: '', + wechatVersion: '', + baseLibraryVersion: '', + hostAppId: '', + }, + isMobile: false, + isDesktop: false, + isDevtools: false, + }, token: '', }, onLaunch() { - // 1. 获取胶囊按钮位置信息 + // 获取小程序宿主环境信息 + const platformInfo = getPlatformInfo() + this.globalData.platformInfo = platformInfo + + // 获取胶囊按钮位置信息 const menuButtonInfo = wx.getMenuButtonBoundingClientRect() - // 2. 获取系统窗口信息 + // 获取系统窗口信息 const windowInfo = wx.getWindowInfo() - const deviceInfo = wx.getDeviceInfo() // 赋值基础信息 - this.globalData.system = deviceInfo.system this.globalData.menuRight = windowInfo.screenWidth - menuButtonInfo.right this.globalData.menuTop = menuButtonInfo.top this.globalData.menuHeight = menuButtonInfo.height diff --git a/miniprogram/utils/platform.ts b/miniprogram/utils/platform.ts new file mode 100644 index 0000000..2e0b648 --- /dev/null +++ b/miniprogram/utils/platform.ts @@ -0,0 +1,81 @@ +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', + } +} diff --git a/typings/index.d.ts b/typings/index.d.ts index 1e43cfc..42105c4 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,6 +1,43 @@ /// /// +/** 微信小程序当前运行的客户端平台,对应 wx.getDeviceInfo().platform */ +type ClientPlatformType = 'ios' | 'android' | 'ohos' | 'ohos_pc' | 'windows' | 'mac' | 'devtools' | 'unknown' + +/** 根据客户端平台归一化后的操作系统类型 */ +type OperatingSystemType = 'ios' | 'android' | 'harmonyos' | 'windows' | 'macos' | 'devtools' | 'unknown' + +/** 根据客户端平台归类的硬件设备类别 */ +type HardwareDeviceClassType = 'mobile' | 'desktop' | 'devtools' | 'unknown' + +interface HardwarePlatformInfoType { + deviceClass: HardwareDeviceClassType + brand: string + model: string + abi: string + deviceAbi: string + cpuType: string + memorySize: string +} + +interface SoftwarePlatformInfoType { + clientPlatform: ClientPlatformType + rawClientPlatform: string + operatingSystem: OperatingSystemType + system: string + wechatVersion: string + baseLibraryVersion: string + hostAppId: string +} + +interface PlatformInfoType { + hardware: HardwarePlatformInfoType + software: SoftwarePlatformInfoType + isMobile: boolean + isDesktop: boolean + isDevtools: boolean +} + interface IAppOption { globalData: { navBarHeight: number, @@ -12,7 +49,7 @@ interface IAppOption { tabbarHeight: number, safeAreaBottom: number, safeAreaInsetBottom: number - system: string, + platformInfo: PlatformInfoType token: string userInfo?: WechatMiniprogram.UserInfo }