my-uniapp-tools
Version:
一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能
122 lines (121 loc) • 4.49 kB
TypeScript
/**
* 系统信息相关工具函数
* 核心原则:简单、清晰、零破坏性
*/
/**
* 平台类型定义
*/
export type PlatformType = "weixin" | "web" | "app" | "alipay" | "h5" | "unknown";
/**
* 顶部区域高度度量
* @description
* - `statusBarHeight`: 状态栏高度
* - `navigationBarHeight`: 导航栏高度(不包含状态栏)
* - `totalTopHeight`: statusBarHeight + navigationBarHeight
*/
export interface TopBarMetrics {
statusBarHeight: number;
navigationBarHeight: number;
totalTopHeight: number;
platform: PlatformType;
}
export interface CurrentEnvInfo {
appId: string;
version: string;
envVersion: string;
accountInfo: unknown;
}
export interface TopNavBarHeight {
statusBarHeight: number;
navHeight: number;
}
/**
* 清除系统缓存(横竖屏切换时调用)
*/
export declare const clearSystemCache: () => void;
/**
* 获取当前运行平台
* @returns 平台类型字符串 ('weixin' | 'web' | 'app' | 'alipay' | 'h5' | 'unknown')
* @description 通过运行时检测判断当前代码运行的平台环境(支持npm包引入)
*/
export declare const getPlatform: () => PlatformType;
/**
* 获取窗口信息
* @param useCache 是否使用缓存,默认true
* @returns 窗口信息对象,包含窗口尺寸、像素比等信息
* @description 调用 uni.getWindowInfo() 获取当前设备的窗口相关信息,支持缓存
*/
export declare const useWindowInfo: (useCache?: boolean) => UniNamespace.GetWindowInfoResult | null;
/**
* 获取小程序账户信息
* @returns 小程序账户信息对象,包含appId、版本、环境等信息
* @description 调用 uni.getAccountInfoSync() 获取当前小程序的账户相关信息
*/
export declare const getCurrentEnv: () => CurrentEnvInfo;
/**
* 检查小程序版本更新
* @description 检查小程序是否有新版本,如果有则提示用户更新并重启应用
* @returns void
* @example
* // 在App.vue的onLaunch或onShow中调用
* onCheckForUpdate()
*/
export declare const onCheckForUpdate: () => void;
/**
* 获取状态栏高度
* @returns 状态栏高度(单位:px)
* @description 获取设备状态栏的高度,用于适配不同设备的状态栏
*/
export declare const getStatusBarHeight: () => number;
/**
* 获取菜单按钮边界信息
* @returns 菜单按钮边界信息对象或null
* @description 获取小程序右上角菜单按钮的边界信息,仅在小程序平台有效
*/
export declare const getMenuButtonBoundingClientRect: () => UniNamespace.GetMenuButtonBoundingClientRectRes | null;
/**
* 获取导航栏高度(不含状态栏)
* @returns 导航栏高度(单位:px)
* @description 获取导航栏的高度,不包含状态栏高度
*/
export declare const getNavigationBarHeight: () => number;
/**
* 获取顶部区域高度度量(推荐使用)
* @description 返回结构化的顶部区域高度信息,避免重复计算
*/
export declare const getTopBarMetrics: () => TopBarMetrics;
/**
* @deprecated 请使用 getTopBarMetrics() 获取结构化数据,或使用 getStatusBarHeight() + getNavigationBarHeight()
* @returns 包含状态栏高度和导航栏总高度的配置对象
* @description 一次性获取应用所需的状态栏和导航栏高度配置信息
*/
export declare const getTopNavBarHeight: () => TopNavBarHeight;
/**
* @deprecated 请使用 getTopBarMetrics().totalTopHeight
* @returns 导航栏总高度(包含状态栏,单位:px)
* @description 获取导航栏的总高度,包含状态栏高度
*/
export declare const getNavHeight: () => number;
/**
* 修改页面标题
* @param title 新的页面标题
* @description 动态修改页面标题,统一使用uni-app API,支持所有平台
* @example
* // 修改页面标题
* setPageTitle('新的页面标题')
*
* @returns Promise<boolean> 返回设置结果
*/
export declare const setPageTitle: (title: string) => Promise<boolean>;
/**
* 修改浏览器图标(favicon)
* @param iconUrl 新的图标URL地址
* @param iconType 图标类型,默认为 'image/x-icon'
* @description 动态修改浏览器标签页显示的图标,仅在H5/Web平台有效
* @example
* // 修改页面图标
* setPageIcon('https://example.com/new-icon.ico')
* // 或使用PNG格式
* setPageIcon('https://example.com/new-icon.png', 'image/png')
*/
export declare const setPageIcon: (iconUrl: string, iconType?: string) => boolean;