127 lines
3.2 KiB
TypeScript
127 lines
3.2 KiB
TypeScript
import { RewardedVideoAdTT, UserInfoTT } from "./tt";
|
|
import { RewardedVideoAd, UserInfo } from "./core";
|
|
import { RewardedVideoAdWeApp, UserInfoWeApp } from "./weapp";
|
|
|
|
/**
|
|
* 小程序平台全局对象类型声明
|
|
*/
|
|
declare const tt: any;
|
|
declare const wx: any;
|
|
declare const my: any;
|
|
declare const swan: any;
|
|
declare const qq: any;
|
|
|
|
/**
|
|
* 支持的平台类型
|
|
* 目前支持:字节跳动小程序(tt)
|
|
*/
|
|
export type Platform = 'tt' | 'weapp' | 'alipay' | 'swan' | 'qq';
|
|
|
|
/**
|
|
* 平台适配器工厂类
|
|
* 根据不同平台创建对应的广告实例
|
|
*/
|
|
export class PlatformFactory {
|
|
private readonly platform: Platform;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param platform 目标平台类型
|
|
*/
|
|
constructor(platform: Platform) {
|
|
this.platform = platform;
|
|
}
|
|
|
|
/**
|
|
* 创建激励视频广告实例
|
|
* @param options 广告配置选项
|
|
* @returns RewardedVideoAd 广告实例
|
|
* @throws Error 当平台不支持时抛出错误
|
|
*/
|
|
createRewardedVideoAd(): RewardedVideoAd {
|
|
switch (this.platform) {
|
|
case 'tt':
|
|
return new RewardedVideoAdTT({
|
|
adUnitId: 'gncb4kr2b6kwp0uacr'
|
|
});
|
|
case 'weapp':
|
|
throw new RewardedVideoAdWeApp();
|
|
case 'alipay':
|
|
throw new Error(`支付宝小程序平台暂未实现`);
|
|
case 'swan':
|
|
throw new Error(`百度智能小程序平台暂未实现`);
|
|
case 'qq':
|
|
throw new Error(`QQ 小程序平台暂未实现`);
|
|
default:
|
|
throw new Error(`不支持的平台类型: ${this.platform}`);
|
|
}
|
|
}
|
|
|
|
|
|
createUserInfo(): UserInfo {
|
|
switch (this.platform) {
|
|
case 'tt':
|
|
return new UserInfoTT();
|
|
case 'weapp':
|
|
throw new UserInfoWeApp();
|
|
case 'alipay':
|
|
throw new Error(`支付宝小程序平台暂未实现`);
|
|
case 'swan':
|
|
throw new Error(`百度智能小程序平台暂未实现`);
|
|
case 'qq':
|
|
throw new Error(`QQ 小程序平台暂未实现`);
|
|
default:
|
|
throw new Error(`不支持的平台类型: ${this.platform}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前平台类型
|
|
* @returns Platform 当前平台
|
|
*/
|
|
getPlatform(): Platform {
|
|
return this.platform;
|
|
}
|
|
|
|
/**
|
|
* 检查平台是否支持
|
|
* @param platform 平台类型
|
|
* @returns boolean 是否支持
|
|
*/
|
|
static isSupportedPlatform(platform: string): platform is Platform {
|
|
return ['tt', 'weapp', 'alipay', 'swan', 'qq'].includes(platform);
|
|
}
|
|
|
|
/**
|
|
* 自动检测当前运行环境平台
|
|
* @returns Platform 检测到的平台类型
|
|
* @throws Error 当无法检测到平台时抛出错误
|
|
*/
|
|
static detectPlatform(): Platform {
|
|
if (typeof tt !== 'undefined') {
|
|
return 'tt';
|
|
}
|
|
if (typeof wx !== 'undefined') {
|
|
return 'weapp';
|
|
}
|
|
if (typeof my !== 'undefined') {
|
|
return 'alipay';
|
|
}
|
|
if (typeof swan !== 'undefined') {
|
|
return 'swan';
|
|
}
|
|
if (typeof qq !== 'undefined') {
|
|
return 'qq';
|
|
}
|
|
throw new Error('无法检测到支持的小程序平台环境');
|
|
}
|
|
|
|
/**
|
|
* 创建自动检测平台的工厂实例
|
|
* @returns PlatformFactory 工厂实例
|
|
*/
|
|
static createAutoDetect(): PlatformFactory {
|
|
const platform = PlatformFactory.detectPlatform();
|
|
return new PlatformFactory(platform);
|
|
}
|
|
} |