import { GetWindowInfo, GetWindowInfoResult, GetSystemInfo, GetSystemInfoOptions, GetSystemInfoSync, GetSystemInfoResult, SafeAreaInsets, SafeArea, } from '../interface.uts' import { API_GET_SYSTEM_INFO, API_GET_SYSTEM_INFO_SYNC, API_GET_WINDOW_INFO, } from '../protocol.uts' import { getWindowInfo as internalGetWindowInfo, getDeviceId, } from '@dcloudio/uni-runtime' import I18n from '@ohos.i18n' import deviceInfo from '@ohos.deviceInfo'; export { GetWindowInfo, GetWindowInfoResult, GetSystemInfo, GetSystemInfoOptions, GetSystemInfoSync, GetSystemInfoResult, SafeAreaInsets, SafeArea, } /** * TODO * API 12支持融合场景化API可直接获取systemInfo, 参考文档 * - [atomicService(融合场景化API)](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/scenario-fusion-atomicservice-V5) */ function parseDeviceType(deviceType: string): 'phone' | 'pad' | 'tv' | 'watch' | 'pc' | 'unknown' | 'car' | 'vr' | 'appliance' { switch (deviceType) { case 'phone': return 'phone' case 'wearable': return 'watch' case 'tablet': return 'pad' case '2in1': return 'pc' case 'tv': return 'tv' case 'car': return 'car' case 'smartVision': return 'vr' default: return 'unknown' } } export const getWindowInfo: GetWindowInfo = defineSyncApi( API_GET_WINDOW_INFO, (): GetWindowInfoResult => { return internalGetWindowInfo() as GetWindowInfoResult } ) as GetWindowInfo interface ISystemInfoAppVersion { name: string code: string } function internalGetSystemInfo(): GetSystemInfoResult { const appVersion: ISystemInfoAppVersion = UTSHarmony.getAppVersion() const appLanguage = I18n.System.getAppPreferredLanguage() const uniCompilerVersion: string = UTSHarmony.getUniCompilerVersion() as string const uniCompilerVersionCode: number = parseFloat(uniCompilerVersion) const uniRuntimeVersion: string = UTSHarmony.getUniRuntimeVersion() const windowInfo = internalGetWindowInfo() as GetWindowInfoResult const pixelRatio = windowInfo.pixelRatio const safeArea = windowInfo.safeArea const safeAreaInsets = windowInfo.safeAreaInsets const screenHeight = windowInfo.screenHeight const screenWidth = windowInfo.screenWidth const statusBarHeight = windowInfo.statusBarHeight const windowBottom = windowInfo.windowBottom const windowHeight = windowInfo.windowHeight const windowTop = windowInfo.windowTop const windowWidth = windowInfo.windowWidth return { // appInfo appId: UTSHarmony.getAppId() as string, appLanguage, appName: UTSHarmony.getAppName() as string, appTheme: UTSHarmony.getAppTheme() as string, appVersion: appVersion.name, appVersionCode: appVersion.code, appWgtVersion: appVersion.name, uniCompilerVersion: uniCompilerVersion, uniCompilerVersionCode: uniCompilerVersionCode, uniRuntimeVersion: uniRuntimeVersion, uniRuntimeVersionCode: parseFloat(uniRuntimeVersion), uniPlatform: 'app', // deviceInfo deviceBrand: deviceInfo.brand.toLowerCase(), deviceId: getDeviceId(), deviceModel: deviceInfo.productModel, deviceOrientation: 'portrait', // 暂未找到同步获取方法,可能需要监听窗口变化 devicePixelRatio: vp2px(1), deviceType: parseDeviceType(deviceInfo.deviceType), osLanguage: I18n.System.getSystemLanguage(), osTheme: UTSHarmony.getOsTheme() as string, osVersion: deviceInfo.majorVersion + '.' + deviceInfo.seniorVersion + '.' + deviceInfo.featureVersion + '.' + deviceInfo.buildVersion, osName: 'harmonyos', romName: deviceInfo.distributionOSName, // TODO 应为此值,但实际上是空字符串,待确认 romVersion: deviceInfo.distributionOSVersion, system: deviceInfo.osFullName, // windowInfo pixelRatio, safeArea, safeAreaInsets, screenHeight, screenWidth, statusBarHeight, windowBottom, windowHeight, windowTop, windowWidth, // other SDKVersion: '', browserName: '', browserVersion: '', ua: '', // 已废弃,但是为了兼容性暂不修改interface定义,随便返回一个符合类型定义的值 language: appLanguage, brand: deviceInfo.brand, model: '', platform: 'harmonyos', uniCompileVersion: uniCompilerVersion, uniCompileVersionCode: uniCompilerVersionCode, version: '' } as GetSystemInfoResult } export const getSystemInfoSync: GetSystemInfoSync = defineSyncApi( API_GET_SYSTEM_INFO_SYNC, (): GetSystemInfoResult => { return internalGetSystemInfo() } ) as GetSystemInfoSync export const getSystemInfo: GetSystemInfo = defineAsyncApi( API_GET_SYSTEM_INFO, (options: GetSystemInfoOptions, exec: ApiExecutor) => { try { exec.resolve(internalGetSystemInfo()) } catch (error) { exec.reject((error as Error).message) } } ) as GetSystemInfo