diff --git a/src/platforms/core.ts b/src/platforms/core.ts index a2d01e1..29191be 100644 --- a/src/platforms/core.ts +++ b/src/platforms/core.ts @@ -164,4 +164,9 @@ export abstract class UserInfo { abstract getUserProfile(): Promise; abstract login(): Promise; abstract checkSession(): Promise; +} + + +export abstract class Media { + abstract downloadFile(url: string): Promise; } \ No newline at end of file diff --git a/src/platforms/factory.ts b/src/platforms/factory.ts index 91e7ea8..b05692a 100644 --- a/src/platforms/factory.ts +++ b/src/platforms/factory.ts @@ -1,6 +1,6 @@ -import { RewardedVideoAdTT, UserInfoTT } from "./tt"; -import { RewardedVideoAd, UserInfo } from "./core"; -import { RewardedVideoAdWeApp, UserInfoWeApp } from "./weapp"; +import { MediaTT, RewardedVideoAdTT, UserInfoTT } from "./tt"; +import { Media, RewardedVideoAd, UserInfo } from "./core"; +import { MediaWeApp, RewardedVideoAdWeApp, UserInfoWeApp } from "./weapp"; /** * 小程序平台全局对象类型声明 @@ -58,6 +58,24 @@ export class PlatformFactory { } + createMedia(): Media { + switch (this.platform) { + case 'tt': + return new MediaTT(); + case 'weapp': + throw new MediaWeApp(); + 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': diff --git a/src/platforms/tt.ts b/src/platforms/tt.ts index 8c287c4..a4b4923 100644 --- a/src/platforms/tt.ts +++ b/src/platforms/tt.ts @@ -6,7 +6,8 @@ import { RewardedVideoAdOptions, UserInfo, IUserProfile, - ICheckSession + ICheckSession, + Media } from "./core"; /** @@ -70,7 +71,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @returns Promise 加载完成的 Promise */ async load(): Promise { - if(!this.ad) return; + if (!this.ad) return; try { await this.ad.load(); this._isReady = true; @@ -85,7 +86,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @returns Promise 展示完成的 Promise */ async show(): Promise { - if(!this.ad) return; + if (!this.ad) return; if (!this._isReady) { throw new Error('广告尚未加载完成,请先调用 load() 方法或等待广告加载完成'); } @@ -97,7 +98,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @returns Promise 销毁完成的 Promise */ async destroy(): Promise { - if(!this.ad) return; + if (!this.ad) return; this._isReady = false; if (this.ad.destroy) { return this.ad.destroy(); @@ -114,7 +115,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @param cb 关闭事件回调函数 */ onClose(cb: RewardedVideoCloseCb): void { - if(!this.ad) return; + if (!this.ad) return; this.ad.onClose(cb); } @@ -123,7 +124,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @param cb 需要移除的关闭事件回调函数 */ offClose(cb: RewardedVideoCloseCb): void { - if(!this.ad) return; + if (!this.ad) return; this.ad.offClose(cb); } @@ -132,7 +133,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @param cb 错误事件回调函数 */ onError(cb: RewardedVideoErrorCb): void { - if(!this.ad) return; + if (!this.ad) return; this.ad.onError(cb); } @@ -141,7 +142,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @param cb 需要移除的错误事件回调函数 */ offError(cb: RewardedVideoErrorCb): void { - if(!this.ad) return; + if (!this.ad) return; this.ad.offError(cb); } @@ -150,7 +151,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @param cb 加载成功事件回调函数 */ onLoad(cb: RewardedVideoLoadCb): void { - if(!this.ad) return; + if (!this.ad) return; this.ad.onLoad(cb); } @@ -159,7 +160,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd { * @param cb 需要移除的加载成功事件回调函数 */ offLoad(cb: RewardedVideoLoadCb): void { - if(!this.ad) return; + if (!this.ad) return; this.ad.offLoad(cb); } @@ -239,4 +240,19 @@ export class UserInfoTT extends UserInfo { }) }) } +} + + +export class MediaTT extends Media { + downloadFile(url: string): Promise { + return new Promise((resolve, reject) => { + tt.downloadFile({ + url, success: (res: any) => { + resolve(res) + }, fail: (res: any) => { + reject(res) + } + }) + }) + } } \ No newline at end of file diff --git a/src/platforms/weapp.ts b/src/platforms/weapp.ts index 21f3db7..a41a238 100644 --- a/src/platforms/weapp.ts +++ b/src/platforms/weapp.ts @@ -6,7 +6,8 @@ import { RewardedVideoAdOptions, UserInfo, IUserProfile, - ICheckSession + ICheckSession, + Media } from "./core"; /** @@ -238,4 +239,19 @@ export class UserInfoWeApp extends UserInfo { }) }) } +} + + +export class MediaWeApp extends Media { + downloadFile(url: string): Promise { + return new Promise((resolve, reject) => { + wx.downloadFile({ + url, success: (res: any) => { + resolve(res) + }, fail: (res: any) => { + reject(res) + } + }) + }) + } } \ No newline at end of file