index.uts 5.2 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
import { GetSystemInfoOptions, GetSystemInfo, GetSystemInfoResult, SafeArea, SafeAreaInsets, GetSystemInfoSync, GetWindowInfo, GetWindowInfoResult } from "../interface.uts";
import { UTSiOS } from "DCloudUTSFoundation";
import { UIScreen , UIDevice ,UIApplication } from 'UIKit';
export const getSystemInfo : GetSystemInfo = function (options : GetSystemInfoOptions) {
	const result = getSystemInfoSync();
	const success = options.success;
	const complete = options.complete;
	success?.(result);
	complete?.(result);
}

export const getSystemInfoSync : GetSystemInfoSync = function () : GetSystemInfoResult {
	const windowInfo : GetWindowInfoResult = getWindowInfoResult()
	
	const deviceType = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad ? "pad" : "phone"
	
	const osVersion = UIDevice.current.systemVersion
	
	let osTheme = 'light'
	if(UTSiOS.available("iOS 13, *")){
		let currentTraitCollection = UIApplication.shared.keyWindow?.traitCollection
		osTheme = currentTraitCollection?.userInterfaceStyle == UIUserInterfaceStyle.dark ? "dark" : "light"
	}
	
	let deviceOrientation = 'portrait'
	const orient = UIApplication.shared.statusBarOrientation;
	if (orient == UIInterfaceOrientation.landscapeLeft || orient == UIInterfaceOrientation.landscapeRight) {
	    deviceOrientation = "landscape";
	 }
	
	const result : GetSystemInfoResult = {
		SDKVersion: "",
		appId: UTSiOS.getAppId(),
		appLanguage: UTSiOS.getOsLanguage(),
		appName: UTSiOS.getAppName(),
		appVersion: UTSiOS.getAppVersion(),
		appVersionCode: UTSiOS.getAppVersionCode(),
		appWgtVersion: UTSiOS.getAppWgtVersion(),
		brand: "apple",
		browserName: "wkwebview",
		browserVersion: osVersion,
		deviceId: UTSiOS.getDeviceId(),
		deviceBrand: "apple",
		deviceModel: UTSiOS.getModel(),
		deviceType: deviceType,
		devicePixelRatio: Number.from(UIScreen.main.scale),
		deviceOrientation: deviceOrientation,
		language: UTSiOS.getOsLanguage(),
		model: UTSiOS.getModel(),
		osName: 'ios',
		osVersion: osVersion,
		osLanguage: UTSiOS.getOsLanguage(),
		osTheme: osTheme,
		pixelRatio: windowInfo.pixelRatio,
		platform: 'ios',
		screenWidth: windowInfo.screenWidth,
		screenHeight: windowInfo.screenHeight,
		statusBarHeight: windowInfo.statusBarHeight,
		system: String(format = "iOS %@", osVersion),
		ua: UTSiOS.getUserAgent(),
		uniCompileVersion: UTSiOS.getCompileVersion(),
		uniCompilerVersion: UTSiOS.getCompileVersion(),
		uniPlatform: "app",
		uniRuntimeVersion: UTSiOS.getRuntimeVersion(),
		uniCompileVersionCode: systemInfoConvertVersionCode(UTSiOS.getCompileVersion()),
		uniCompilerVersionCode: systemInfoConvertVersionCode(UTSiOS.getCompileVersion()),
		uniRuntimeVersionCode: systemInfoConvertVersionCode(UTSiOS.getRuntimeVersion()),
		version: UTSiOS.getInnerVersion(),
		romName: "ios",
		romVersion: osVersion,
		windowWidth: windowInfo.windowWidth,
		windowHeight: windowInfo.windowHeight,
		windowTop: windowInfo.windowTop,
		windowBottom: windowInfo.windowBottom,
		safeAreaInsets: windowInfo.safeAreaInsets,
		safeArea: windowInfo.safeArea,
	};
	return result;
}

const systemInfoConvertVersionCode = function(version: string): number {
	if (version.length > 0){
		const components = version.components(separatedBy= '.')
		var resultString = ""
		for (let i = 0; i < components.length; i++) {
		    resultString = (i == 0) ? (resultString + components[i] + '.') : (resultString + components[i])
		}
		return parseFloat(resultString)
	}
	return 0
}

function getWindowInfoResult() : GetWindowInfoResult {
	let insets : Map<string, any> = UTSiOS.getSafeAreaInsets()
	let window : Map<string, any> = UTSiOS.getWindowInfo()
	
	const windowWidth = window.get("windowWidth") as number
	const windowHeight = window.get("windowHeight") as number
	const windowTop = window.get("windowTop") as number
	const windowBottom = window.get("windowBottom") as number
	
	
	const screenWidth = Number.from(UIScreen.main.bounds.width)
	const screenHeight = Number.from(UIScreen.main.bounds.height)
	
	const insetLeft = insets.get("left") as number
	const insetRight = insets.get("right") as number
	const insetTop = insets.get("top") as number
	const insetBottom = insets.get("bottom") as number
	
	const safeAreaInsets : SafeAreaInsets = {
		left: insetLeft,
		top: insetTop,
		right: insetRight,
		bottom: insetBottom
	}
	
	const safeAreaLeft = insetLeft
	const safeAreaRight = windowWidth - insetRight
	const safeAreaTop = insetTop
	const safeAreaBottom = windowHeight - insetBottom
	const safeAreaWidth = windowWidth - insetLeft - insetRight
	const safeAreaHeight = windowHeight - insetTop - insetBottom
	
	const safeArea : SafeArea = {
		left: safeAreaLeft,
		top: safeAreaTop,
		right: safeAreaRight,
		bottom: safeAreaBottom,
		width: safeAreaWidth,
		height: safeAreaHeight
	}

	const screenTop = screenHeight - windowHeight
	
	const result : GetWindowInfoResult = {
		pixelRatio: Number.from(UIScreen.main.scale),
		screenWidth: screenWidth,
		screenHeight: screenHeight,
		windowWidth: windowWidth,
		windowHeight: windowHeight,
		statusBarHeight: UTSiOS.getStatusBarHeight(),
		windowTop: windowTop,
		windowBottom: windowBottom,
		safeArea: safeArea,
		safeAreaInsets: safeAreaInsets,
		screenTop: screenTop
	};
	
	return result;
}

export const getWindowInfo: GetWindowInfo = function(): GetWindowInfoResult {
	
	return getWindowInfoResult();
}