index.uts 4.8 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
import { AppBaseInfoDeviceUtil } from './device/AppBaseInfoDeviceUtil.uts';

import {
	GetAppBaseInfoOptions,
	GetAppBaseInfo,
	GetAppBaseInfoResult
} from '../interface.uts'



export const getAppBaseInfo : GetAppBaseInfo = (config : GetAppBaseInfoOptions | null) : GetAppBaseInfoResult => {
	let filter : Array<string> = [];
	if (config != null && config.filter != null) {
		filter = config.filter;
	}

	if (config == null || filter.length == 0) {
		const defaultFilter = [
			"appId",
			"appName",
			"appVersion",
			"appVersionCode",
			"appLanguage",
			"language",
			"version",
			"appWgtVersion",
			"hostLanguage",
			"hostVersion",
			"hostName",
			"hostPackageName",
			"hostSDKVersion",
			"hostTheme",
			"isUniAppX",
			"uniCompileVersion",
DCloud-yyl's avatar
DCloud-yyl 已提交
35
			"uniCompilerVersion",
DCloud-yyl's avatar
DCloud-yyl 已提交
36 37 38
			"uniPlatform",
			"uniRuntimeVersion",
			"uniCompileVersionCode",
DCloud-yyl's avatar
DCloud-yyl 已提交
39 40 41 42
			"uniCompilerVersionCode",
			"uniRuntimeVersionCode",
			"packageName",
			"signature"
DCloud-yyl's avatar
DCloud-yyl 已提交
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
		];
		filter = defaultFilter;
	}
	return getBaseInfo(filter);
}

function getBaseInfo(filterArray : Array<string>) : GetAppBaseInfoResult {
	const activity = UTSAndroid.getUniActivity()!;
	let result : GetAppBaseInfoResult = {};
	if (filterArray.indexOf("appId") != -1) {
		result.appId = AppBaseInfoDeviceUtil.getAppID();
	}
	if (filterArray.indexOf("appName") != -1) {
		result.appName = UTSAndroid.getAppName();
	}
	if (UTSAndroid.isUniMp()) {
		if (filterArray.indexOf("hostPackageName") != -1) {
			result.hostPackageName = AppBaseInfoDeviceUtil.getPackageName(activity);
		}
		if (filterArray.indexOf("hostVersion") != -1) {
			result.hostVersion = AppBaseInfoDeviceUtil.getHostVersion(activity);
		}
		if (filterArray.indexOf("hostName") != -1) {
			result.hostName = AppBaseInfoDeviceUtil.getAppName(activity);
		}
		if (filterArray.indexOf("hostTheme") != -1) {
			result.hostTheme = AppBaseInfoDeviceUtil.isSystemNightMode(activity) ? "dark" : "light";
		}
		if (filterArray.indexOf("hostLanguage") != -1) {
			result.hostLanguage = AppBaseInfoDeviceUtil.getOsLanguage(activity);
		}
		if (filterArray.indexOf("appVersion") != -1) {
			result.appVersion = AppBaseInfoDeviceUtil.getAppVersionName();
		}
		if (filterArray.indexOf("appVersionCode") != -1) {
			result.appVersionCode = AppBaseInfoDeviceUtil.getAppVersionCode();
		}
	} else {
		if (filterArray.indexOf("appVersion") != -1) {
			result.appVersion = UTSAndroid.getAppVersion()["name"].toString();
		}
		if (filterArray.indexOf("appVersionCode") != -1) {
			result.appVersionCode = UTSAndroid.getAppVersion()["code"].toString();
		}
	}
	if (filterArray.indexOf("appLanguage") != -1) {
		result.appLanguage = AppBaseInfoDeviceUtil.getOsLanguageNormal(activity);
	}
	if (filterArray.indexOf("language") != -1) {
		result.language = AppBaseInfoDeviceUtil.getOsLanguage(activity);
	}
	if (filterArray.indexOf("version") != -1) {
		result.version = AppBaseInfoDeviceUtil.getAppInnerVersion();
	}
	if (filterArray.indexOf("appWgtVersion") != -1) {
		result.appWgtVersion = AppBaseInfoDeviceUtil.getAppVersionName();
	}
	if (filterArray.indexOf("isUniAppX") != -1) {
		result.isUniAppX = UTSAndroid.isUniAppX();
	}
	
	if (filterArray.indexOf("uniCompileVersion") != -1) {
		result.uniCompileVersion = UTSAndroid.getUniCompileVersion();
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
107 108 109 110
	if (filterArray.indexOf("uniCompilerVersion") != -1) {
		result.uniCompilerVersion = UTSAndroid.getUniCompileVersion();
	}
	
DCloud-yyl's avatar
DCloud-yyl 已提交
111 112 113 114 115 116 117 118 119 120 121 122
	
	if (filterArray.indexOf("uniPlatform") != -1) {
		result.uniPlatform = "app";
	}
	
	if (filterArray.indexOf("uniRuntimeVersion") != -1) {
		result.uniRuntimeVersion = UTSAndroid.getUniRuntimeVersion();
	}
	
	if (filterArray.indexOf("uniCompileVersionCode") != -1) {
		result.uniCompileVersionCode = convertVersionCode(UTSAndroid.getUniCompileVersion());
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
123 124 125
	if (filterArray.indexOf("uniCompilerVersionCode") != -1) {
		result.uniCompilerVersionCode = convertVersionCode(UTSAndroid.getUniCompileVersion());
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
126 127 128
	
	if (filterArray.indexOf("uniRuntimeVersionCode") != -1) {
		result.uniRuntimeVersionCode = convertVersionCode(UTSAndroid.getUniRuntimeVersion());
DCloud-yyl's avatar
DCloud-yyl 已提交
129 130 131 132 133 134 135 136
	}
	
	if (filterArray.indexOf("packageName") != -1) {
		result.packageName =  AppBaseInfoDeviceUtil.getPackageName(activity);
	}
	
	if (filterArray.indexOf("signature") != -1) {
		result.signature = AppBaseInfoDeviceUtil.getAppSignatureSHA1(activity);
DCloud-yyl's avatar
DCloud-yyl 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	}

	return result;
}

const convertVersionCode = function(version: string): number {
	let str = "";
	let radixLength = 2;
	let findDot = false;
	const dotChar = ".".get(0);
	for (let i = 0; i < version.length; i++) {
		const char = version.get(i);
		if(findDot){
			if(char.isDigit()){
				str += char;
			}
			radixLength --;
			if(radixLength == 0){
				break;
			}
		}else{
			if(char.isDigit()){
				str += char;
			}else{
				if(char == dotChar){
					findDot = true;
					str += char;
				}
			}
		}
	}
	
	return parseFloat(str);
}