33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
/** 是否 iOS 设备(不含微信开发者工具模拟器特殊处理) */
|
|
export function isIosDevice(): boolean {
|
|
if (typeof navigator === 'undefined') return false;
|
|
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !(window as Window & { MSStream?: unknown }).MSStream;
|
|
}
|
|
|
|
/** 是否微信开发者工具 */
|
|
export function isWechatDevTools(): boolean {
|
|
if (typeof navigator === 'undefined') return false;
|
|
return /wechatdevtools/i.test(navigator.userAgent);
|
|
}
|
|
|
|
/** 是否微信内置浏览器 */
|
|
export function isWechatBrowser(): boolean {
|
|
if (typeof navigator === 'undefined') return false;
|
|
return /MicroMessenger/i.test(navigator.userAgent);
|
|
}
|
|
|
|
/** 是否微信小程序 web-view 或独立小程序环境 */
|
|
export function isMiniProgram(): boolean {
|
|
const g = globalThis as typeof globalThis & { wx?: { requestPayment?: unknown } };
|
|
if (g.wx?.requestPayment) return true;
|
|
if (typeof window === 'undefined') return false;
|
|
const ua = navigator.userAgent.toLowerCase();
|
|
return ua.includes('miniprogram') || (window as Window & { __wxjs_environment?: string }).__wxjs_environment === 'miniprogram';
|
|
}
|
|
|
|
export function getRuntimePlatform(): 'mini' | 'wechat-h5' | 'browser' {
|
|
if (isMiniProgram()) return 'mini';
|
|
if (isWechatBrowser()) return 'wechat-h5';
|
|
return 'browser';
|
|
}
|