device.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * 检查是否为小程序环境
  3. * @returns 是否为小程序环境
  4. */
  5. export const isMp = (): boolean => {
  6. // #ifdef MP
  7. return true;
  8. // #endif
  9. return false;
  10. };
  11. /**
  12. * 检查是否为App环境
  13. * @returns 是否为App环境
  14. */
  15. export const isApp = (): boolean => {
  16. // #ifdef APP
  17. return true;
  18. // #endif
  19. return false;
  20. };
  21. /**
  22. * 检查是否为App-IOS环境
  23. * @returns 是否为App-IOS环境
  24. */
  25. export const isAppIOS = (): boolean => {
  26. // #ifdef APP-IOS
  27. return true;
  28. // #endif
  29. return false;
  30. };
  31. /**
  32. * 检查是否为App-Android环境
  33. * @returns 是否为App-Android环境
  34. */
  35. export const isAppAndroid = (): boolean => {
  36. // #ifdef APP-ANDROID
  37. return true;
  38. // #endif
  39. return false;
  40. };
  41. /**
  42. * 检查是否为H5环境
  43. * @returns 是否为H5环境
  44. */
  45. export const isH5 = (): boolean => {
  46. // #ifdef H5
  47. return true;
  48. // #endif
  49. return false;
  50. };
  51. /**
  52. * 检查是否为鸿蒙环境
  53. * @returns 是否为鸿蒙环境
  54. */
  55. export const isHarmony = (): boolean => {
  56. // #ifdef APP-HARMONY
  57. return true;
  58. // #endif
  59. return false;
  60. };
  61. /**
  62. * 获取设备像素比
  63. * @returns 设备像素比
  64. */
  65. export const getDevicePixelRatio = (): number => {
  66. const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
  67. // #ifdef MP
  68. // 微信小程序高清处理
  69. return 3;
  70. // #endif
  71. return dpr;
  72. };
  73. /**
  74. * 判断是否为平板(修复华为MatePad识别为phone问题)
  75. * @returns true=平板 false=手机
  76. */
  77. export const isTabletDevice = (): boolean => {
  78. // 1. 获取设备基础信息
  79. const devInfo = uni.getDeviceInfo();
  80. const sysInfo = uni.getSystemInfoSync();
  81. const model = devInfo.deviceModel.toLowerCase();
  82. console.log(sysInfo)
  83. // 2. 华为平板专属关键词(MatePad / MediaPad)
  84. const isHuaweiPad = /matepad|mediapad/.test(model);
  85. // 过滤华为折叠屏(Mate X/Xs/Xt等,避免大屏误判平板)
  86. const isHuaweiFold = /mate x|mate xs|mate xt/.test(model);
  87. // 3. 计算屏幕物理对角线英寸
  88. const pxW = sysInfo.screenWidth;
  89. const pxH = sysInfo.screenHeight;
  90. const dpr = sysInfo.pixelRatio;
  91. const inch = Math.sqrt(pxW ** 2 + pxH ** 2) / 160;
  92. // 4. 复合判定规则
  93. // 满足任意一条且不是折叠屏 → 平板
  94. return !isHuaweiFold && (
  95. devInfo.deviceType === 'pad'
  96. || isHuaweiPad
  97. || inch >= 7
  98. );
  99. }