提交 2b555ad4 编写于 作者: DCloud-yyl's avatar DCloud-yyl

开源:uni.getAppBaseInfo

上级 05f988a0
{
"id": "uni-getAppBaseInfo",
"displayName": "uni-getAppBaseInfo",
"version": "1.0.0",
"description": "uni-getAppBaseInfo",
"keywords": [
"uni-getAppBaseInfo"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"uni-ext-api": {
"uni": {
"getAppBaseInfo": {
"name": "getAppBaseInfo",
"app": {
"js": false,
"kotlin": true,
"swift": true
}
}
}
},
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uni-getAppBaseInfo
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS 原生插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts/-/tree/dev)
\ No newline at end of file
import { UTSAndroid as Device} from "io.dcloud.uts";
import PackageManager from 'android.content.pm.PackageManager';
import Context from 'android.content.Context';
import PackageInfo from 'android.content.pm.PackageInfo';
import UiModeManager from 'android.app.UiModeManager';
import Activity from 'android.app.Activity';
export class AppBaseInfoDeviceUtil{
public static getAppID(): string {
return Device.getAppId();
}
public static getAppName(context: Context): string {
let packageManager = context.getPackageManager();
return packageManager.getApplicationLabel(context.getApplicationInfo()).toString()
}
public static getPackageName(context: Context): string {
return context.getPackageName();
}
public static getAppVersionName(): string {
return Device.getAppVersion()["name"].toString();
}
public static getAppVersionCode(): string {
return Device.getAppVersion()["code"].toString();
}
public static getHostVersion(context: Context): string {
let packageManager = context.getPackageManager();
let applicationInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
return applicationInfo.versionName;
}
public static getHostCode(context: Context): string {
let packageManager = context.getPackageManager();
let applicationInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
return applicationInfo.versionCode+"";
}
public static isSystemNightMode(activity: Activity): boolean {
let uiModeManager = activity.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager;
return uiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES;
}
public static getOsLanguage(context: Context): string {
return Device.getLanguageInfo(context)["osLanguage"].toString();
}
public static getOsLanguageNormal(context: Context): string {
const LOCALE_ZH_HANS = 'zh-Hans'
const LOCALE_ZH_HANT = 'zh-Hant'
let locale = Device.getLanguageInfo(context)["appLanguage"].toString();
if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS;
}
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT;
}
if (locale.includes("-tw") || locale.includes("-hk") || locale.includes("-mo") || locale.includes("-cht")) {
return LOCALE_ZH_HANT;
}
return LOCALE_ZH_HANS;
} else {
return locale;
}
}
public static getAppInnerVersion(): string {
return Device.getInnerVersion();
}
}
\ No newline at end of file
import UTSAndroid from 'io.dcloud.uts.UTSAndroid';
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",
"uniPlatform",
"uniRuntimeVersion",
"uniCompileVersionCode",
"uniRuntimeVersionCode"
];
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();
}
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());
}
if (filterArray.indexOf("uniRuntimeVersionCode") != -1) {
result.uniRuntimeVersionCode = convertVersionCode(UTSAndroid.getUniRuntimeVersion());
}
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);
}
\ No newline at end of file
import { UTSiOS } from "DCloudUTSFoundation";
export class AppBaseInfoDeviceUtil {
public static getAppID() : string {
return UTSiOS.getAppId();
}
public static getAppName() : string {
return UTSiOS.getAppName();
}
public static getHostName() : string {
return UTSiOS.getHostName();
}
public static getHostTheme() : string {
return UTSiOS.getHostTheme();
}
public static getHostLanguage() : string {
return UTSiOS.getHostLanguage();
}
public static getHostVersion() : string {
return UTSiOS.getHostVersion();
}
public static getHostPackageName() : string {
return UTSiOS.getHostPackageName();
}
public static getAppVersion() : string {
return UTSiOS.getAppVersion();
}
public static getAppVersionCode() : string {
return UTSiOS.getAppVersionCode();
}
public static getAppWgtVersion() : string {
return UTSiOS.getAppWgtVersion();
}
public static getOsLanguage() : string {
return UTSiOS.getOsLanguage();
}
public static getOsLanguageNormal() : string {
const LOCALE_ZH_HANS = 'zh-Hans'
const LOCALE_ZH_HANT = 'zh-Hant'
let locale = UTSiOS.getOsLanguage();
if (locale.indexOf('zh') == 0) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS;
}
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT;
}
if (locale.includes("-tw") || locale.includes("-hk") || locale.includes("-mo") || locale.includes("-cht")) {
return LOCALE_ZH_HANT;
}
return LOCALE_ZH_HANS;
} else {
return locale;
}
}
public static getAppInnerVersion() : string {
return UTSiOS.getInnerVersion();
}
}
\ No newline at end of file
import { AppBaseInfoDeviceUtil } from './device/AppBaseInfoDeviceUtil.uts';
import { UTSiOS } from "DCloudUTSFoundation";
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",
];
filter = defaultFilter;
}
return getBaseInfo(filter);
}
function getBaseInfo(filterArray : Array<string>) : GetAppBaseInfoResult {
let result : GetAppBaseInfoResult = {};
if (filterArray.indexOf("appId") != -1) {
result.appId = AppBaseInfoDeviceUtil.getAppID();
}
if (filterArray.indexOf("appName") != -1) {
result.appName = AppBaseInfoDeviceUtil.getAppName();
}
if (UTSiOS.isUniMp()) {
if (filterArray.indexOf("hostPackageName") != -1) {
result.hostPackageName = AppBaseInfoDeviceUtil.getHostPackageName();
}
if (filterArray.indexOf("hostVersion") != -1) {
result.hostVersion = AppBaseInfoDeviceUtil.getHostVersion();
}
if (filterArray.indexOf("hostName") != -1) {
result.hostName = AppBaseInfoDeviceUtil.getHostName();
}
if (filterArray.indexOf("hostTheme") != -1) {
result.hostTheme = AppBaseInfoDeviceUtil.getHostTheme();
}
if (filterArray.indexOf("hostLanguage") != -1) {
result.hostLanguage = AppBaseInfoDeviceUtil.getHostLanguage();
}
}
if (filterArray.indexOf("appVersion") != -1) {
result.appVersion = AppBaseInfoDeviceUtil.getAppVersion();
}
if (filterArray.indexOf("appVersionCode") != -1) {
result.appVersionCode = AppBaseInfoDeviceUtil.getAppVersionCode();
}
if (filterArray.indexOf("appLanguage") != -1) {
result.appLanguage = AppBaseInfoDeviceUtil.getOsLanguageNormal();
}
if (filterArray.indexOf("language") != -1) {
result.language = AppBaseInfoDeviceUtil.getOsLanguage();
}
if (filterArray.indexOf("version") != -1) {
result.version = AppBaseInfoDeviceUtil.getAppInnerVersion();
}
if (filterArray.indexOf("appWgtVersion") != -1) {
result.appWgtVersion = AppBaseInfoDeviceUtil.getAppWgtVersion();
}
return result;
}
\ No newline at end of file
export type GetAppBaseInfoOptions = {
/**
* @description 过滤字段的字符串数组,假如要获取指定字段,传入此数组。
*/
filter: Array<string>
};
export type GetAppBaseInfoResult = {
/**
* manifest.json 中应用appid,即DCloud appid。
*/
appId?: string,
/**
* `manifest.json` 中应用名称。
*/
appName?: string,
/**
* `manifest.json` 中应用版本名称。
*/
appVersion?: string,
/**
* `manifest.json` 中应用版本名号。
*/
appVersionCode?: string,
/**
* 应用设置的语言en、zh-Hans、zh-Hant、fr、es
*/
appLanguage?: string,
/**
* 应用设置的语言
*/
language?: string,
/**
* 引擎版本号。已废弃,仅为了向下兼容保留
* @deprecated 已废弃,仅为了向下兼容保留
*/
version?: string,
/**
* 应用资源(wgt)的版本名称。
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
appWgtVersion?: string,
/**
* 小程序宿主语言
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
hostLanguage?: string,
/**
* App、小程序宿主版本。
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
hostVersion?: string,
/**
* 小程序宿主名称
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
hostName?: string,
/**
* 小程序宿主包名
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
hostPackageName?: string,
/**
* uni小程序SDK版本、小程序客户端基础库版本
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
hostSDKVersion?: string,
/**
* 系统当前主题,取值为light或dark。微信小程序全局配置"darkmode":true时才能获取,否则为 undefined (不支持小游戏)
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "5.0",
* "uniVer": "√",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
hostTheme?: string,
/**
* 是否uni-app x
*/
isUniAppX ?: boolean,
/**
* uni 编译器版本
*/
uniCompileVersion ?: string,
/**
* uni-app 运行平台。如:`app`、`mp-weixin`、`web`
*/
uniPlatform ?: string,
/**
* uni 运行时版本
*/
uniRuntimeVersion ?: string,
/**
* uni 编译器版本号
*/
uniCompileVersionCode?: number,
/**
* uni 运行时版本号
*/
uniRuntimeVersionCode?: number,
}
/**
* @param{GetAppBaseInfoOptions} [options=包含所有字段的过滤对象] 过滤的字段对象, 不传参数默认为获取全部字段。
*/
export type GetAppBaseInfo = (options?: GetAppBaseInfoOptions | null) => GetAppBaseInfoResult;
export interface Uni {
/**
* GetAppBaseInfo(Object object)
* @description
* 获取app基本信息
* @param {GetAppBaseInfoOptions} options [options=包含所有字段的过滤对象] 过滤的字段对象, 不传参数默认为获取全部字段。
* @return {object}
* @tutorial https://uniapp.dcloud.net.cn/api/system/getAppBaseInfo.html
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "4.4",
* "uniVer": "√",
* "unixVer": "3.9+"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "3.9+"
* }
* }
* }
* @example
```typescript
uni.getAppBaseInfo({
filter:[]
})
```
*/
getAppBaseInfo(options?: GetAppBaseInfoOptions | null): GetAppBaseInfoResult;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册