refactor(util-navigate): simplify platform detection logic

1. remove redundant try-catch wrapper for compile-time macros
2. add quickapp runtime detection for H5 in quickapp webview
3. add optional chaining for uni.getSystemInfoSync call
4. add comment for default return case
This commit is contained in:
2026-07-15 11:18:55 +08:00
parent 13da4f1c9f
commit 1814fffc12

View File

@@ -421,12 +421,18 @@ export function getPlatform() {
} }
// 2) 编译时变量 // 2) 编译时变量
try { /* #ifdef H5 */ return 'h5'; /* #endif */ } catch (e) { /* ignore */ } // #ifdef MP-WEIXIN
try { /* #ifdef MP-WEIXIN */ return 'mp-weixin'; /* #endif */ } catch (e) { /* ignore */ } return 'mp-weixin'
try { /* #ifdef MP-HARMONY */ return 'mp-harmony'; /* #endif */ } catch (e) { /* ignore */ } // #endif
try { /* #ifdef MP-QUICKAPP */ return 'quickapp'; /* #endif */ } catch (e) { /* ignore */ } // #ifdef MP-HARMONY
return 'mp-harmony'
// #endif
// 3) 运行时 // 3) 运行时
if (navigator?.userAgent?.includes('mode-quickapp')) { // H5网页,如何判断自己在华为快应用的Webview中
return 'quickapp'
}
if (typeof process !== 'undefined' && process.env && process.env.UNI_PLATFORM) { if (typeof process !== 'undefined' && process.env && process.env.UNI_PLATFORM) {
return process.env.UNI_PLATFORM; return process.env.UNI_PLATFORM;
} }
@@ -435,7 +441,7 @@ export function getPlatform() {
} }
if (typeof uni !== 'undefined' && typeof uni.getSystemInfoSync === 'function') { if (typeof uni !== 'undefined' && typeof uni.getSystemInfoSync === 'function') {
try { try {
const info = uni.getSystemInfoSync(); const info = uni?.getSystemInfoSync();
const p = (info && info.platform) || ''; const p = (info && info.platform) || '';
if (/harmony|ohos/i.test(p)) return 'mp-harmony'; if (/harmony|ohos/i.test(p)) return 'mp-harmony';
if (info.mp === true || /miniprogram/i.test(info.appName || '')) return 'mp-weixin'; if (info.mp === true || /miniprogram/i.test(info.appName || '')) return 'mp-weixin';
@@ -443,6 +449,8 @@ export function getPlatform() {
return 'h5'; return 'h5';
} catch (e) { /* ignore */ } } catch (e) { /* ignore */ }
} }
// 默认返回
return 'h5'; return 'h5';
} }