feat: 添加跨平台媒体下载功能
- 新增Media抽象类定义文件下载接口 - 在工厂类中添加createMedia方法 - 实现MediaTT类支持字节跳动小程序文件下载 - 实现MediaWeApp类支持微信小程序文件下载 - 统一代码格式,修复空格和缩进问题
This commit is contained in:
parent
2994b68063
commit
353e0ee6dc
|
|
@ -164,4 +164,9 @@ export abstract class UserInfo {
|
|||
abstract getUserProfile(): Promise<IUserProfile>;
|
||||
abstract login(): Promise<ILogin>;
|
||||
abstract checkSession(): Promise<ICheckSession>;
|
||||
}
|
||||
|
||||
|
||||
export abstract class Media {
|
||||
abstract downloadFile(url: string): Promise<void>;
|
||||
}
|
||||
|
|
@ -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':
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ import {
|
|||
RewardedVideoAdOptions,
|
||||
UserInfo,
|
||||
IUserProfile,
|
||||
ICheckSession
|
||||
ICheckSession,
|
||||
Media
|
||||
} from "./core";
|
||||
|
||||
/**
|
||||
|
|
@ -70,7 +71,7 @@ export class RewardedVideoAdTT extends RewardedVideoAd {
|
|||
* @returns Promise<void> 加载完成的 Promise
|
||||
*/
|
||||
async load(): Promise<void> {
|
||||
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<void> 展示完成的 Promise
|
||||
*/
|
||||
async show(): Promise<void> {
|
||||
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<void> 销毁完成的 Promise
|
||||
*/
|
||||
async destroy(): Promise<void> {
|
||||
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<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
tt.downloadFile({
|
||||
url, success: (res: any) => {
|
||||
resolve(res)
|
||||
}, fail: (res: any) => {
|
||||
reject(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -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<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.downloadFile({
|
||||
url, success: (res: any) => {
|
||||
resolve(res)
|
||||
}, fail: (res: any) => {
|
||||
reject(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue