平台判断辅助工具类:PlatformHelper.ts

This commit is contained in:
chen wei bo
2025-09-25 11:24:56 +08:00
parent b5de68d76b
commit 7896dbacb2
4 changed files with 107 additions and 5 deletions
@@ -0,0 +1,91 @@
import { sys } from "cc";
/**
* 跨平台辅助工具类
* 统一封装常见平台的判断逻辑
*/
export class PlatformHelper {
/** 是否微信小游戏 */
public static isWechat(): boolean {
return sys.platform === sys.Platform.WECHAT_GAME && typeof window["wx"] !== "undefined";
}
/** 是否字节跳动(抖音/头条)小游戏 */
public static isByteDance(): boolean {
return sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && typeof window["tt"] !== "undefined";
}
/** 是否快手小游戏 */
public static isKuaishou(): boolean {
return typeof window["ks"] !== 'undefined';
}
/** 是否百度小游戏 */
public static isBaidu(): boolean {
return sys.platform === sys.Platform.BAIDU_MINI_GAME;
}
/** 是否支付宝/阿里小游戏 */
public static isAlipay(): boolean {
return sys.platform === sys.Platform.ALIPAY_MINI_GAME;
}
/** 是否 Oppo 小游戏 */
public static isOppo(): boolean {
return sys.platform === sys.Platform.OPPO_MINI_GAME;
}
/** 是否 Vivo 小游戏 */
public static isVivo(): boolean {
return sys.platform === sys.Platform.VIVO_MINI_GAME;
}
/** 是否华为快应用 */
public static isHuawei(): boolean {
return sys.platform === sys.Platform.HUAWEI_QUICK_GAME;
}
/** 是否Windows桌面 */
public static isWinDesktop(): boolean {
return sys.platform === sys.Platform.WIN32;
}
/** 是否MacOS桌面 */
public static isMacOSDesktop(): boolean {
return sys.platform === sys.Platform.MACOS;
}
/** 是否桌面(Windows 或 MacOS */
public static isDesktop(): boolean {
return sys.platform === sys.Platform.WIN32 || sys.platform === sys.Platform.MACOS;
}
/** 是否原生 Android */
public static isAndroid(): boolean {
return sys.isNative && sys.os === sys.OS.ANDROID;
}
/** 是否原生 iOS */
public static isIOS(): boolean {
return sys.isNative && sys.os === sys.OS.IOS;
}
/** 获取当前平台名称(调试用) */
public static getPlatformName(): string {
if (this.isWechat()) return "微信小游戏";
if (this.isByteDance()) return "字节跳动小游戏";
if (this.isKuaishou()) return "快手小游戏";
if (this.isBaidu()) return "百度小游戏";
if (this.isAlipay()) return "支付宝小游戏";
if (this.isOppo()) return "Oppo 小游戏";
if (this.isVivo()) return "Vivo 小游戏";
if (this.isHuawei()) return "华为快应用";
if (this.isWinDesktop()) return "Windows 桌面";
if (this.isMacOSDesktop()) return "MacOS 桌面";
if (this.isAndroid()) return "原生 Android";
if (this.isIOS()) return "原生 iOS";
// 兜底
return "未知平台";
}
}