platform.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * 平台和环境检测工具
  3. * 统一管理所有平台相关的判断逻辑
  4. */
  5. export enum Platform {
  6. ELECTRON = 'electron',
  7. CORDOVA = 'cordova',
  8. BROWSER = 'browser',
  9. UNKNOWN = 'unknown'
  10. }
  11. export interface PlatformInfo {
  12. platform: Platform;
  13. isElectron: boolean;
  14. isCordova: boolean;
  15. isBrowser: boolean;
  16. isRenderer: boolean;
  17. isMain: boolean;
  18. }
  19. /**
  20. * 环境检测函数
  21. */
  22. const env = {
  23. isElectron: () => {
  24. // 多个条件检测,确保在不同环境下都能正确识别
  25. return (
  26. // 渲染进程检测
  27. (typeof window !== 'undefined' &&
  28. typeof window.process === 'object' &&
  29. (window.process as any).type === 'renderer') ||
  30. // 主进程检测
  31. (typeof process !== 'undefined' &&
  32. typeof process.versions === 'object' &&
  33. !!(process.versions as any).electron) ||
  34. // User Agent 检测(兜底方案)
  35. (typeof navigator !== 'undefined' &&
  36. navigator.userAgent.toLowerCase().includes('electron'))
  37. );
  38. },
  39. isCordova: () => {
  40. return typeof window !== 'undefined' && !!window.cordova;
  41. },
  42. isAndroid: () => {
  43. if (env.isCordova()) {
  44. // Cordova 环境使用 device.platform
  45. return typeof device !== 'undefined' && device.platform === 'Android';
  46. }
  47. // 浏览器环境使用 navigator.userAgent
  48. return typeof navigator !== 'undefined' && /Android/i.test(navigator.userAgent);
  49. },
  50. isIOS: () => {
  51. if (env.isCordova()) {
  52. // Cordova 环境使用 device.platform
  53. return typeof device !== 'undefined' && /iOS/i.test(device.platform);
  54. }
  55. // 浏览器环境使用 navigator.userAgent
  56. return typeof navigator !== 'undefined' && /iPhone|iPad|iPod/i.test(navigator.userAgent);
  57. },
  58. isBrowser: () => {
  59. return !env.isElectron() && !env.isCordova();
  60. }
  61. };
  62. /**
  63. * 获取当前平台信息
  64. */
  65. export function getPlatformInfo(): PlatformInfo {
  66. const isElectron = env.isElectron();
  67. const isCordova = env.isCordova();
  68. const isBrowser = env.isBrowser();
  69. // 主进程/渲染进程检测(Electron)
  70. const isRenderer = isElectron && typeof window !== 'undefined' &&
  71. typeof window.process === 'object' &&
  72. (window.process as any).type === 'renderer';
  73. const isMain = typeof process !== 'undefined' &&
  74. (process as any).type === 'browser';
  75. // 确定主要平台
  76. let platform: Platform;
  77. if (isElectron) {
  78. platform = Platform.ELECTRON;
  79. } else if (isCordova) {
  80. platform = Platform.CORDOVA;
  81. } else if (isBrowser) {
  82. platform = Platform.BROWSER;
  83. } else {
  84. platform = Platform.UNKNOWN;
  85. }
  86. return {
  87. platform,
  88. isElectron,
  89. isCordova,
  90. isBrowser,
  91. isRenderer,
  92. isMain
  93. };
  94. }
  95. /**
  96. * 便捷的平台检测函数
  97. * 导出一个单例,避免重复计算
  98. */
  99. export const platform = getPlatformInfo();
  100. /**
  101. * 导出环境检测函数,供其他地方使用
  102. */
  103. export const { isElectron, isCordova, isAndroid, isIOS, isBrowser } = env;
  104. /**
  105. * 是否支持持久化存储
  106. * Electron 和 Cordova 支持文件系统存储,浏览器不支持
  107. */
  108. export function supportsPersistentStorage(): boolean {
  109. return platform.isElectron || platform.isCordova;
  110. }
  111. /**
  112. * 是否支持动态配置
  113. * 所有平台都支持动态配置,但存储方式不同
  114. */
  115. export function supportsDynamicConfig(): boolean {
  116. return true; // 所有平台都支持
  117. }
  118. /**
  119. * 获取平台名称(用于显示)
  120. */
  121. export function getPlatformDisplayName(): string {
  122. switch (platform.platform) {
  123. case Platform.ELECTRON:
  124. return 'Electron 桌面应用';
  125. case Platform.CORDOVA:
  126. return 'Cordova 移动应用';
  127. case Platform.BROWSER:
  128. return '浏览器';
  129. default:
  130. return '未知平台';
  131. }
  132. }