feat(app): 添加平台信息工具并将其集成到应用初始化中
- 创建 utils/platform.ts 以提取并规范化设备/操作系统信息 - 将 globalData 中的普通 `system` 字符串替换为结构化的 `platformInfo` 对象 - 推导硬件/软件详情、设备类别和平台标志 - 更新 TypeScript 类型定义以支持新的平台数据类型
This commit is contained in:
1 parent
58cd96aaa3
commit
f19f4b8d41
3 files changed
+150
-6
No files matched your search
+31
-5
@@ -1,4 +1,6 @@
|
|||||||
// app.ts
|
// app.ts
|
||||||
|
import { getPlatformInfo } from './utils/platform'
|
||||||
|
|
||||||
App<IAppOption>({
|
App<IAppOption>({
|
||||||
globalData: {
|
globalData: {
|
||||||
navBarHeight: 0, // 导航栏总高度
|
navBarHeight: 0, // 导航栏总高度
|
||||||
@@ -10,19 +12,43 @@ App<IAppOption>({
|
|||||||
tabbarHeight: 0, // 底部导航栏高度
|
tabbarHeight: 0, // 底部导航栏高度
|
||||||
safeAreaBottom: 0, // 底部安全区域坐标
|
safeAreaBottom: 0, // 底部安全区域坐标
|
||||||
safeAreaInsetBottom: 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: '',
|
token: '',
|
||||||
},
|
},
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
// 1. 获取胶囊按钮位置信息
|
// 获取小程序宿主环境信息
|
||||||
|
const platformInfo = getPlatformInfo()
|
||||||
|
this.globalData.platformInfo = platformInfo
|
||||||
|
|
||||||
|
// 获取胶囊按钮位置信息
|
||||||
const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
|
const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
|
||||||
|
|
||||||
// 2. 获取系统窗口信息
|
// 获取系统窗口信息
|
||||||
const windowInfo = wx.getWindowInfo()
|
const windowInfo = wx.getWindowInfo()
|
||||||
const deviceInfo = wx.getDeviceInfo()
|
|
||||||
|
|
||||||
// 赋值基础信息
|
// 赋值基础信息
|
||||||
this.globalData.system = deviceInfo.system
|
|
||||||
this.globalData.menuRight = windowInfo.screenWidth - menuButtonInfo.right
|
this.globalData.menuRight = windowInfo.screenWidth - menuButtonInfo.right
|
||||||
this.globalData.menuTop = menuButtonInfo.top
|
this.globalData.menuTop = menuButtonInfo.top
|
||||||
this.globalData.menuHeight = menuButtonInfo.height
|
this.globalData.menuHeight = menuButtonInfo.height
|
||||||
|
|||||||
@@ -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',
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+38
-1
@@ -1,6 +1,43 @@
|
|||||||
/// <reference path="./types/index.d.ts" />
|
/// <reference path="./types/index.d.ts" />
|
||||||
/// <reference path="./npm.d.ts" />
|
/// <reference path="./npm.d.ts" />
|
||||||
|
|
||||||
|
/** 微信小程序当前运行的客户端平台,对应 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 {
|
interface IAppOption {
|
||||||
globalData: {
|
globalData: {
|
||||||
navBarHeight: number,
|
navBarHeight: number,
|
||||||
@@ -12,7 +49,7 @@ interface IAppOption {
|
|||||||
tabbarHeight: number,
|
tabbarHeight: number,
|
||||||
safeAreaBottom: number,
|
safeAreaBottom: number,
|
||||||
safeAreaInsetBottom: number
|
safeAreaInsetBottom: number
|
||||||
system: string,
|
platformInfo: PlatformInfoType
|
||||||
token: string
|
token: string
|
||||||
userInfo?: WechatMiniprogram.UserInfo
|
userInfo?: WechatMiniprogram.UserInfo
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user