/** * 检查是否为小程序环境 * @returns 是否为小程序环境 */ export const isMp = (): boolean => { // #ifdef MP return true; // #endif return false; }; /** * 检查是否为App环境 * @returns 是否为App环境 */ export const isApp = (): boolean => { // #ifdef APP return true; // #endif return false; }; /** * 检查是否为App-IOS环境 * @returns 是否为App-IOS环境 */ export const isAppIOS = (): boolean => { // #ifdef APP-IOS return true; // #endif return false; }; /** * 检查是否为App-Android环境 * @returns 是否为App-Android环境 */ export const isAppAndroid = (): boolean => { // #ifdef APP-ANDROID return true; // #endif return false; }; /** * 检查是否为H5环境 * @returns 是否为H5环境 */ export const isH5 = (): boolean => { // #ifdef H5 return true; // #endif return false; }; /** * 检查是否为鸿蒙环境 * @returns 是否为鸿蒙环境 */ export const isHarmony = (): boolean => { // #ifdef APP-HARMONY return true; // #endif return false; }; /** * 获取设备像素比 * @returns 设备像素比 */ export const getDevicePixelRatio = (): number => { const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1; // #ifdef MP // 微信小程序高清处理 return 3; // #endif return dpr; }; /** * 判断是否为平板(修复华为MatePad识别为phone问题) * @returns true=平板 false=手机 */ export const isTabletDevice = (): boolean => { // 1. 获取设备基础信息 const devInfo = uni.getDeviceInfo(); const sysInfo = uni.getSystemInfoSync(); const model = devInfo.deviceModel.toLowerCase(); console.log(sysInfo) // 2. 华为平板专属关键词(MatePad / MediaPad) const isHuaweiPad = /matepad|mediapad/.test(model); // 过滤华为折叠屏(Mate X/Xs/Xt等,避免大屏误判平板) const isHuaweiFold = /mate x|mate xs|mate xt/.test(model); // 3. 计算屏幕物理对角线英寸 const pxW = sysInfo.screenWidth; const pxH = sysInfo.screenHeight; const dpr = sysInfo.pixelRatio; const inch = Math.sqrt(pxW ** 2 + pxH ** 2) / 160; // 4. 复合判定规则 // 满足任意一条且不是折叠屏 → 平板 return !isHuaweiFold && ( devInfo.deviceType === 'pad' || isHuaweiPad || inch >= 7 ); }