2025-07-17 17:18:21 +08:00
|
|
|
import { _decorator, Component, sys, utils } from "cc";
|
2025-09-21 20:05:36 +08:00
|
|
|
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
2025-09-25 11:24:19 +08:00
|
|
|
import { PlatformHelper } from "./PlatformHelper";
|
2025-09-26 17:33:48 +08:00
|
|
|
import { IAdSDK } from "./core/IAdSDK";
|
|
|
|
|
import { IWalletSDK } from "./core/IWalletSDK";
|
|
|
|
|
import { IPlatformSDK } from "./core/IPlatformSDK";
|
|
|
|
|
import { IAnalyticsSDK } from "./core/IAnalyticsSDK";
|
|
|
|
|
import { WxPlatformSDK } from "./platform/WxPlatformSDK";
|
|
|
|
|
import { WxAdSDK } from "./ads/WxAdSDK";
|
|
|
|
|
import { LuffaWalletSDK } from "./wallet/LuffaWalletSDK";
|
|
|
|
|
import { UmaAnalyticsSDK } from "./analytics/UmaAnalyticsSDK";
|
2025-09-27 14:03:55 +08:00
|
|
|
import { ISignCap } from "./core/ISignCap";
|
|
|
|
|
import { ITransactionCap } from "./core/ITransactionCap";
|
2025-10-13 15:39:35 +08:00
|
|
|
import { WebSDK } from "./platform/WebSDK";
|
2025-07-17 17:18:21 +08:00
|
|
|
|
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
2025-09-26 17:33:48 +08:00
|
|
|
// 渠道管理器
|
2025-10-13 15:39:35 +08:00
|
|
|
@ccclass("SDKManager")
|
|
|
|
|
export class SDKManager {
|
|
|
|
|
private static _I: SDKManager = null;
|
|
|
|
|
public static get I(): SDKManager {
|
|
|
|
|
if (!SDKManager._I) {
|
|
|
|
|
SDKManager._I = new SDKManager();
|
2025-07-17 17:18:21 +08:00
|
|
|
}
|
2025-10-13 15:39:35 +08:00
|
|
|
return SDKManager._I;
|
|
|
|
|
}
|
|
|
|
|
// 广告,分享
|
|
|
|
|
private adSDK: IAdSDK | null = null;
|
|
|
|
|
// 钱包
|
|
|
|
|
private walletSDK: IWalletSDK | null = null;
|
|
|
|
|
// 平台基础
|
|
|
|
|
private platformSDK: IPlatformSDK | null = null;
|
|
|
|
|
// 分析统计
|
|
|
|
|
private analyticsSDK: IAnalyticsSDK | null = null;
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
private constructor() {}
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 初始化
|
|
|
|
|
public init(cb: Function = null) {
|
|
|
|
|
logger.info("SDKManager 初始化...");
|
|
|
|
|
logger.info("当前平台" + sys.platform);
|
|
|
|
|
// 检测平台
|
|
|
|
|
if (PlatformHelper.isKuaishou()) {
|
|
|
|
|
// 快手
|
|
|
|
|
} else if (PlatformHelper.isWechat()) {
|
|
|
|
|
// 微信
|
|
|
|
|
this.adSDK = new WxAdSDK();
|
|
|
|
|
this.platformSDK = new WxPlatformSDK();
|
|
|
|
|
} else if (PlatformHelper.isLuffa()) {
|
|
|
|
|
// Luffa
|
|
|
|
|
this.walletSDK = new LuffaWalletSDK();
|
|
|
|
|
} else if (PlatformHelper.isByteDance()) {
|
|
|
|
|
// 字节跳动
|
|
|
|
|
} else {
|
|
|
|
|
//默认web平台
|
|
|
|
|
this.platformSDK = new WebSDK();
|
|
|
|
|
}
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 默认加载 UMA 分析
|
|
|
|
|
this.analyticsSDK = new UmaAnalyticsSDK();
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 初始化各模块
|
|
|
|
|
this.adSDK?.init();
|
|
|
|
|
this.walletSDK?.init();
|
|
|
|
|
this.platformSDK?.init();
|
|
|
|
|
this.analyticsSDK?.init();
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 回调
|
|
|
|
|
cb && cb();
|
|
|
|
|
}
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
/**
|
|
|
|
|
* 销毁所有 SDK
|
|
|
|
|
*
|
|
|
|
|
* - 逐个调用各 SDK 的 `destroy` 方法
|
|
|
|
|
* - 用于游戏退出、切换账号、切换平台时释放资源
|
|
|
|
|
* - 调用时机:游戏关闭 / 重新进入大厅 / SDK 切换
|
|
|
|
|
*/
|
|
|
|
|
public destroyAll() {
|
|
|
|
|
this.adSDK?.destroy?.();
|
|
|
|
|
this.adSDK = null;
|
|
|
|
|
this.walletSDK?.destroy?.();
|
|
|
|
|
this.walletSDK = null;
|
|
|
|
|
this.platformSDK?.destroy?.();
|
|
|
|
|
this.platformSDK = null;
|
|
|
|
|
this.analyticsSDK?.destroy?.();
|
|
|
|
|
this.analyticsSDK = null;
|
|
|
|
|
logger.log("所有 SDK 已销毁并清空引用");
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
/** --------------------------------------------------------- 广告,分享 --------------------------------------------------------- */
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 展示激励视频广告
|
|
|
|
|
public showRewardVideo(cb: (ok: boolean) => void): void {
|
|
|
|
|
if (!this.adSDK) return;
|
|
|
|
|
this.adSDK.showRewardVideo(cb);
|
|
|
|
|
}
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 展示插屏广告
|
|
|
|
|
public showInterstitial(): void {
|
|
|
|
|
if (!this.adSDK) return;
|
|
|
|
|
this.adSDK.showInterstitial();
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 展示 Banner 广告
|
|
|
|
|
public showBanner(): void {
|
|
|
|
|
if (!this.adSDK) return;
|
|
|
|
|
this.adSDK.showBanner();
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 隐藏 Banner 广告
|
|
|
|
|
public hideBanner(): void {
|
|
|
|
|
if (!this.adSDK) return;
|
|
|
|
|
this.adSDK.hideBanner();
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 主动拉起分享
|
|
|
|
|
public showShare(cb?: Function): void {
|
|
|
|
|
if (!this.adSDK) return;
|
|
|
|
|
this.adSDK.showShare(cb);
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
/** --------------------------------------------------------- 钱包 --------------------------------------------------------- */
|
2025-09-27 14:03:55 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 连接钱包
|
|
|
|
|
public connectWallet(
|
|
|
|
|
cb: (address: string, publicKey?: string) => void
|
|
|
|
|
): void {
|
|
|
|
|
if (!this.walletSDK) return;
|
|
|
|
|
this.walletSDK.connectWallet(cb);
|
|
|
|
|
}
|
2025-09-27 14:03:55 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 断开钱包连接
|
|
|
|
|
public disconnectWallet(): void {
|
|
|
|
|
if (!this.walletSDK) return;
|
|
|
|
|
this.walletSDK.disconnectWallet();
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 获取当前钱包地址
|
|
|
|
|
public getWalletAddress(): string | null {
|
|
|
|
|
if (!this.walletSDK) return null;
|
|
|
|
|
return this.walletSDK.getWalletAddress();
|
|
|
|
|
}
|
2025-09-27 14:03:55 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 签名消息
|
|
|
|
|
public async signMessage(
|
|
|
|
|
message: string,
|
|
|
|
|
options?: any
|
|
|
|
|
): Promise<{ signature: string; fullMessage?: string }> {
|
|
|
|
|
if (!this.walletSDK) return;
|
|
|
|
|
// 判断是否实现签名能力
|
|
|
|
|
if ("signMessage" in this.walletSDK) {
|
|
|
|
|
const signer = this.walletSDK as unknown as ISignCap;
|
|
|
|
|
return signer.signMessage(message, options);
|
|
|
|
|
}
|
|
|
|
|
throw new Error("当前钱包 SDK 不支持 signMessage 功能");
|
|
|
|
|
}
|
2025-09-27 14:03:55 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 签名交易
|
|
|
|
|
public async signTransaction(rawTx: any): Promise<{ signedData: string }> {
|
|
|
|
|
if (!this.walletSDK) return;
|
|
|
|
|
// 判断是否实现交易能力
|
|
|
|
|
if ("signTransaction" in this.walletSDK) {
|
|
|
|
|
const txCap = this.walletSDK as unknown as ITransactionCap;
|
|
|
|
|
return txCap.signTransaction(rawTx);
|
|
|
|
|
}
|
|
|
|
|
throw new Error("当前钱包 SDK 不支持 signTransaction 功能");
|
|
|
|
|
}
|
2025-09-27 14:03:55 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 发送交易(广播到链上)
|
|
|
|
|
public async sendTransaction(signedData: string): Promise<{ hash: string }> {
|
|
|
|
|
if (!this.walletSDK) return;
|
|
|
|
|
// 判断是否实现交易能力
|
|
|
|
|
if ("sendTransaction" in this.walletSDK) {
|
|
|
|
|
const txCap = this.walletSDK as unknown as ITransactionCap;
|
|
|
|
|
return txCap.sendTransaction(signedData);
|
|
|
|
|
}
|
|
|
|
|
throw new Error("当前钱包 SDK 不支持 sendTransaction 功能");
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
/** --------------------------------------------------------- 平台基础 --------------------------------------------------------- */
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 登录
|
|
|
|
|
public login(cb?: Function): void {
|
|
|
|
|
if (!this.platformSDK) return;
|
|
|
|
|
this.platformSDK.login(cb);
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 获取用户信息
|
|
|
|
|
public getUserInfo(
|
|
|
|
|
cb: (info: { nickName: string; avatarUrl: string }) => void
|
|
|
|
|
): void {
|
|
|
|
|
if (!this.platformSDK) return;
|
|
|
|
|
this.platformSDK.getUserInfo(cb);
|
|
|
|
|
}
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 检查平台权限
|
|
|
|
|
public checkPermission(scope: string, cb: (granted: boolean) => void): void {
|
|
|
|
|
if (!this.platformSDK) return;
|
|
|
|
|
this.platformSDK.checkPermission(scope, cb);
|
|
|
|
|
}
|
2025-07-17 17:18:21 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 复制文本到剪贴板
|
|
|
|
|
public copyToClipboard(text: string, cb?: Function): void {
|
|
|
|
|
if (!this.platformSDK) return;
|
|
|
|
|
this.platformSDK.copyToClipboard(text, cb);
|
|
|
|
|
}
|
2025-09-18 23:33:06 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 客服功能是否可用
|
|
|
|
|
public kefuSupport(): boolean {
|
|
|
|
|
if (!this.platformSDK) return false;
|
|
|
|
|
return this.platformSDK.kefuSupport();
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 打开客服会话
|
|
|
|
|
public openKefu(): void {
|
|
|
|
|
if (!this.platformSDK) return;
|
|
|
|
|
this.platformSDK.openKefu();
|
|
|
|
|
}
|
2025-09-26 17:33:48 +08:00
|
|
|
|
2025-10-13 15:39:35 +08:00
|
|
|
// 获取设备分辨率
|
|
|
|
|
public getWindowSize(): { width: number; height: number } {
|
|
|
|
|
if (!this.platformSDK) return { width: 720, height: 1280 };
|
|
|
|
|
return this.platformSDK.getWindowSize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** --------------------------------------------------------- 分析统计 --------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
// 判断统计功能是否可用
|
|
|
|
|
public isAnalyticsEnabled(): boolean {
|
|
|
|
|
if (!this.analyticsSDK) return false;
|
|
|
|
|
return this.analyticsSDK.isAnalyticsEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上报事件
|
|
|
|
|
public trackEvent(eventName: string, value?: any): void {
|
|
|
|
|
if (!this.analyticsSDK) return;
|
|
|
|
|
return this.analyticsSDK.trackEvent(eventName, value);
|
|
|
|
|
}
|
|
|
|
|
}
|