123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- //TODO 未完成,需要查看Api文档
- import { Node, Vec2 } from "cc";
- import { MiniGameApiBase, getUserInfoParams, toastParams, loadParams, ModalParams, authorizeParams, loginParams, recordedGameScreenParams } from "./MiniGameApiBase";
- import { GameCfg } from "../Config/GameCfg";
- const huaWei: any = window['tt'];
- export default class HuaWeiApi extends MiniGameApiBase {
- showBannerAD(node: Node, successCB?: () => void, errorCB?: (error: any) => void, onCloseCB?: () => void, setBannerPos?: ({ screenWidth, screenHeight }: any) => Vec2): void {
- throw new Error("Method not implemented.");
- }
- checkScene(successCB?: Function, errorCB?: Function) {
- throw new Error("Method not implemented.");
- }
- navigateToScene(successCB?: Function, errorCB?: Function) {
- throw new Error("Method not implemented.");
- }
- getStorageSync(key?: string) {
- throw new Error("Method not implemented.");
- }
- removeStorage(key?: string) {
- throw new Error("Method not implemented.");
- }
- getDeviceInfo() {
- throw new Error("Method not implemented.");
- }
- setUnlockAllTime(time: number) {
- throw new Error("Method not implemented.");
- }
- getUnlockAllTime(call: Function) {
- throw new Error("Method not implemented.");
- }
- setUnitState(state?: string) {
- throw new Error("Method not implemented.");
- }
- reportAnalytics(eventName: string, data?: { [key: string]: string | number | boolean; }) {
- throw new Error("Method not implemented.");
- }
- setSoundInfo(sound?: boolean, music?: boolean) {
- throw new Error("Method not implemented.");
- }
- protected m_AppId: string = "";
- protected m_Secret: string = "";
- protected m_VideoAdIdIndex: number = 0;
- protected m_VideoAdIdList: string[] = [];
- protected m_BannerAdIdIndex: number = 0;
- protected m_BannerAdId: string[] = [];
- protected m_InsertAdIdIndex: number = 0;
- protected m_InsertAdId: string[] = [];
- protected m_videoAd: any = undefined;
- protected m_BannerAd: any = undefined;
- validAppId(): void {
- if (GameCfg.SYSTEM_LOG) console.log("huawei-api-validAppId");
- }
- ready(): void {
- if (GameCfg.SYSTEM_LOG) console.log("huaWei-api-ready");
- }
- getUserInfo(params: getUserInfoParams): void {
- huaWei.getUserInfo(params);
- }
- showToast(params: toastParams): void {
- huaWei.showToast(params);
- }
- showLoading(params: loadParams): void {
- huaWei.showLoading(params);
- }
- hideLoading(): void {
- huaWei.hideLoading();
- }
- showModal(params: ModalParams): void {
- huaWei.showModal(params);
- }
- authorize(params: authorizeParams): void {
- params.success && params.success();
- }
- login(params: loginParams): void {
- huaWei.login(params);
- }
- getVideoAdId(): string {
- const id: string = this.m_VideoAdIdList[this.m_VideoAdIdIndex];
- this.m_VideoAdIdIndex++;
- if (this.m_VideoAdIdIndex >= this.m_VideoAdIdList.length) {
- this.m_VideoAdIdIndex = 0;
- }
- return id;
- }
- loadAndShowVideoAd(successCB: () => void, errorCB: (error: any) => void, completeCB?: () => void, reportAnalyticsExplain?: string): void {
- this.showLoading({ title: '请求中', mask: true });
- const videoAdId = this.getVideoAdId();
- console.log('tt 视频广告', videoAdId, reportAnalyticsExplain);
- //创建 视频
- const videoAd: any = huaWei.createRewardedVideoAd({
- adUnitId: videoAdId,
- });
- //监听 视频关闭
- let onCloseVideoAd = (res) => {
- //
- videoAd.offClose(onCloseVideoAd);
- console.log('tt 视频广告关闭', res);
- if (res && res.isEnded) {
- // 给予奖励
- successCB();
- } else {
- errorCB('视频中途退出');
- }
- completeCB && completeCB();
- };
- //重试
- let onTryAgain = (e: any) => {
- videoAd.load().then(() => {
- videoAd.show().then(() => {
- this.hideLoading();
- videoAd.onClose(onCloseVideoAd);
- }).catch((e) => {//展示异常
- errorCB && errorCB('视频展示异常');
- completeCB && completeCB();
- });
- }).catch((e) => {//加载异常
- errorCB && errorCB('视频拉取异常02');
- completeCB && completeCB();
- });
- }
- console.log('展示一次');
- videoAd.show().then(() => {
- this.hideLoading();
- videoAd.onClose(onCloseVideoAd);
- //展示异常
- }).catch((e) => {
- console.log('重试一次');
- onTryAgain(e);
- })
- }
- getInsertAdId(): string {
- const id: string = this.m_InsertAdId[this.m_InsertAdIdIndex];
- this.m_InsertAdIdIndex++;
- if (this.m_InsertAdIdIndex >= this.m_InsertAdId.length) {
- this.m_InsertAdIdIndex = 0;
- }
- return id;
- }
- showInsertAd(onAdClose: () => void, onFailed: () => void): void {
- console.log("加抖音 插屏 广告");
- const adId = this.getInsertAdId();
- let insertAd = huaWei.createInterstitialAd({
- adUnitId: adId,
- });
- insertAd.load();
- insertAd.onLoad(() => {
- console.warn('插屏广告 加载完成');
- insertAd.show().catch((err) => {
- console.warn('插屏广告 显示失败 :' + JSON.stringify(err));
- insertAd.destroy();
- insertAd = null;
- if (onFailed) {
- onFailed();
- }
- });
- });
- insertAd.onError((err) => {
- console.warn('插屏广告 加载失败' + JSON.stringify(err));
- insertAd.destroy();
- insertAd = null;
- if (onFailed) {
- onFailed();
- }
- });
- insertAd.onClose(() => {
- console.warn('插屏广告 关闭');
- if (insertAd != null) {
- insertAd.destroy();
- insertAd = null;
- }
- if (onAdClose) {
- onAdClose();
- }
- });
- }
- getBannerAdId(): string {
- const id: string = this.m_BannerAdId[this.m_BannerAdIdIndex];
- this.m_BannerAdIdIndex++;
- if (this.m_BannerAdIdIndex >= this.m_BannerAdId.length) {
- this.m_BannerAdIdIndex = 0;
- }
- return id;
- }
- hideBanner() {
- // if (this.m_BannerAd != null) {
- // this.m_BannerAd.hide();
- // }
- }
- recordedGameScreenStart(params: recordedGameScreenParams, onStart?: (res: any) => void): void {
- console.log("huaWei-api-recordedGameScreenStart");
- onStart && onStart(null);
- }
- recordedGameScreenStop() {
- console.log("huaWei-api-recordedGameScreenStop");
- return true;
- }
- shareRecordedGameScreen(successCb?: () => void, failCb?: (e?: any) => void): void {
- console.log("huaWei-api-shareRecordedGameScreen");
- successCb && successCb();
- }
- isHaveRecordedGameScreen(): boolean {
- return true;
- }
- }
|