index.uts 3.0 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5

import { UTSiOS } from "DCloudUTSFoundation";
import { DeviceUtil } from './device/DeviceUtil.uts';

import { GetDeviceInfo, GetDeviceInfoOptions, GetDeviceInfoResult } from '../interface.uts'
DCloud-yyl's avatar
DCloud-yyl 已提交
6
import { UIScreen , UIDevice ,UIApplication } from 'UIKit';
DCloud-yyl's avatar
DCloud-yyl 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

export const getDeviceInfo : GetDeviceInfo = (config : GetDeviceInfoOptions | null) : GetDeviceInfoResult => {
	let filter : Array<string> = [];
	if (config != null && config?.filter != null) {
		let temp = config?.filter;
		filter = temp!;
	}

	if (config == null || filter.length == 0) {
		const defaultFilter = [
			"brand",
			"deviceBrand",
			"deviceId",
			"model",
			"deviceModel",
			"deviceType",
			"deviceOrientation",
			"devicePixelRatio",
			"system",
			"platform",
			"isRoot",
DCloud-yyl's avatar
DCloud-yyl 已提交
28 29 30 31 32 33 34
			"isSimulator",
			"osName",
			"osVersion",
			"osLanguage",
			"osTheme",
			"romName",
			"romVersion"
DCloud-yyl's avatar
DCloud-yyl 已提交
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
		];
		filter = defaultFilter;
	}
	return getBaseInfo(filter);
}


function getBaseInfo(filterArray : Array<string>) : GetDeviceInfoResult {
	const osVersion = DeviceUtil.getOSVersion();
	let result : GetDeviceInfoResult = {};
	if (filterArray.indexOf("brand") != -1) {
		result.brand = "apple";
	}
	if (filterArray.indexOf("deviceBrand") != -1) {
		result.deviceBrand = "apple";
	}
	if (filterArray.indexOf("deviceId") != -1) {
		result.deviceId = UTSiOS.getDeviceId();
	}
	if (filterArray.indexOf("model") != -1) {
		result.model = UTSiOS.getModel();
	}
	if (filterArray.indexOf("deviceModel") != -1) {
		result.deviceModel = UTSiOS.getModel();
	}
	if (filterArray.indexOf("deviceType") != -1) {
		result.deviceType = DeviceUtil.isPad() ? "pad" : "phone";
	}
	if (filterArray.indexOf("deviceOrientation") != -1) {
		result.deviceOrientation = DeviceUtil.getOrientation();
	}
	if (filterArray.indexOf("devicePixelRatio") != -1) {
		result.devicePixelRatio = DeviceUtil.getScreenScale();
	}
	if (filterArray.indexOf("system") != -1) {
		result.system = String(format = "iOS %@", osVersion);
	}
	if (filterArray.indexOf("platform") != -1) {
		result.platform = "ios";
	}
	if (filterArray.indexOf("isRoot") != -1) {
		result.isRoot = DeviceUtil.hasRootPrivilege();
	}
	if (filterArray.indexOf("isSimulator") != -1) {
		result.isSimulator = UTSiOS.isSimulator();
DCloud-yyl's avatar
DCloud-yyl 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	}
	
	if (filterArray.indexOf("osName") != -1) {
		result.osName = "ios";
	}
	if (filterArray.indexOf("osVersion") != -1) {
		result.osVersion = UIDevice.current.systemVersion;
	}
	if (filterArray.indexOf("osLanguage") != -1) {
		result.osLanguage = UTSiOS.getOsLanguage();
	}
	if (filterArray.indexOf("osTheme") != -1) {
		let osTheme = 'light'
		if(UTSiOS.available("iOS 13, *")){
			let currentTraitCollection = UIApplication.shared.keyWindow?.traitCollection
			osTheme = currentTraitCollection?.userInterfaceStyle == UIUserInterfaceStyle.dark ? "dark" : "light"
		}
		result.osTheme = osTheme;
	}
	if (filterArray.indexOf("romName") != -1) {
		result.romName = "ios"
	}
	if (filterArray.indexOf("romVersion") != -1) {
		result.romVersion = UIDevice.current.systemVersion;
DCloud-yyl's avatar
DCloud-yyl 已提交
104 105 106 107
	}

	return result;
}