From f19f4b8d4152ea5a87827451b8f989f8d80e21d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E7=86=8A=E7=86=8A=E5=AD=90=E8=B7=AF?= Date: Fri, 31 Jul 2026 11:31:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E6=B7=BB=E5=8A=A0=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E4=BF=A1=E6=81=AF=E5=B7=A5=E5=85=B7=E5=B9=B6=E5=B0=86?= =?UTF-8?q?=E5=85=B6=E9=9B=86=E6=88=90=E5=88=B0=E5=BA=94=E7=94=A8=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 utils/platform.ts 以提取并规范化设备/操作系统信息 - 将 globalData 中的普通 `system` 字符串替换为结构化的 `platformInfo` 对象 - 推导硬件/软件详情、设备类别和平台标志 - 更新 TypeScript 类型定义以支持新的平台数据类型 --- miniprogram/app.ts | 36 +++++++++++++--- miniprogram/utils/platform.ts | 81 +++++++++++++++++++++++++++++++++++ typings/index.d.ts | 39 ++++++++++++++++- 3 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 miniprogram/utils/platform.ts 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 }