123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- // import { EQAPPKEY, EQVERSION, EQAUTH } from '../../../../sdk/eqconf.js';
- // import "../../../../sdk/eq.js";
- import { Rect, sys, Node, BlockInputEvents, macro, Vec2 } from "cc";
- import { MiniGameApiBase, getUserInfoParams, toastParams, loadParams, ModalParams, authorizeParams, loginParams, recordedGameScreenParams } from "./MiniGameApiBase";
- import { LayerMgr } from "../Manager/LayerMgr";
- import { GameCfg } from "../Config/GameCfg";
- export interface WxVideoAd {
- // 通过 load 方法主动预加载广告内容。
- // 此外,在显示广告出现问题时也可以尝试主动 load 一次。
- // 该方法返回一个 Promise,如果广告已经自动拉取成功,调用该方法返回一个 resolved Promise;
- load: () => Promise<any>;
- // 广告创建后默认是隐藏的,可以通过该方法显示广告。 该方法返回一个 Promise 对象。当广告组件正常获取素材时,该 Promise 对象会是一个 resolved Promise。当广告组件发生错误时,会是一个 rejected Promise,参数与error事件监听器获得的参数相同。
- show: () => Promise<any>;
- //绑定 load 事件的监听器,用户在手动调用 load 方法后,广告组件会预先加载。 广告组件成功拉取广告素材时会触发load事件的监听器。
- //确保监听此事件时,开发者主动调用过 load 方法。
- onLoad: (listener: Function) => void;
- // 解除绑定 load 事件的监听器。
- offLoad: (listener: Function) => void;
- //绑定 error 事件的监听器。 广告组件拉取广告素材和其他情况下如果发生错误,会触发error事件的监听器。监听器会获得一个包含errCode和errMsg属性的对象参数。
- // errCode 详情
- onError: (listener: (error: any) => void) => void;
- // 解除绑定 error 事件的监听器。
- offError: (listener: Function) => void;
- // 绑定 close 事件的监听器。 当用户点击了 Video 广告上的关闭按钮时,会触发close事件的监听器。监听器会获得一个包含isEnded属性的对象参数,表征用户是否完整观看了视频。
- onClose: (listener: (res: { isEnded: boolean }) => void) => void;
- //解除绑定 close 事件的监听器。
- offClose: (listener: Function) => void;
- //
- destroy(): () => void;
- }
- // export enum wx_VIDEO_TYPE {
- // general,
- // level_ulock,
- // get_power,
- // skin_unlock,
- // }
- const wx: any = window['wx'];
- export default class WxApi extends MiniGameApiBase {
- checkScene(successCB?: Function, errorCB?: Function) {
- console.log("Method not implemented.");
- }
- navigateToScene(successCB?: Function, errorCB?: Function) {
- console.log("Method not implemented.");
- }
- // protected m_AppId: string = "wx44456653566a7f94";
- protected m_AppId: string = "wx186d8263bd479a17";
- protected m_Secret: string = "";
- protected m_VideoAdIdIndex: number = 0;
- // protected m_VideoAdIdList: string[] = ["adunit-0120ab5292f6496a"];
- protected m_VideoAdIdList: string[] = ["adunit-3f18073324ae586f"];
- protected m_BannerAdIdIndex: number = 0;
- protected m_BannerAdId: string[] = [""];
- protected m_InsertAdIdIndex: number = 0;
- protected m_InsertAdId: string[] = [""];
- protected m_videoAd: WxVideoAd = undefined;
- protected m_BannerAd: any = undefined;
- /** 录屏管理器 */
- protected m_gameRecorder: any = null;
- validAppId(): void {
- console.log("wx-api-validAppId");
- }
- ready(): void {
- console.log("wx-api-ready");
- this.m_gameRecorder = wx.getGameRecorder();
- console.log('是否支持录制游戏画面', this.m_gameRecorder.isFrameSupported());
- // 目前还未实现,后期可能会支持到
- console.log('是否支持录制游戏画面的同时也录制音频', this.m_gameRecorder.isSoundSupported());
- }
- getUserInfo(params: getUserInfoParams): void {
- wx.getUserInfo(params);
- }
- showToast(params: toastParams): void {
- wx.showToast(params);
- }
- showLoading(params: loadParams): void {
- wx.showLoading(params);
- }
- hideLoading(): void {
- wx.hideLoading();
- }
- showModal(params: ModalParams): void {
- }
- authorize(params: authorizeParams): void {
- params.success && params.success();
- }
- login(params: loginParams): void {
- wx.login(params);
- }
- // vibrateShort(): void {
- // if (!gameConfig.VIDRATION_ENABLE) { return; }
- // wx.vibrateShort({
- // success: (errMsg) => {
- // console.log(errMsg);
- // },
- // fail: (errMsg) => {
- // console.log(errMsg);
- // },
- // complete: () => { }
- // });
- // }
- 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;
- }
- private reportAnalyticsExplain: string = "null";
- loadAndShowVideoAd(successCB: () => void, errorCB: (error: any) => void, completeCB?: () => void, reportAnalyticsExplain?: string): void {
- if (this.reportAnalyticsExplain == reportAnalyticsExplain) {
- errorCB({ error: "重复播放视频" });
- completeCB && completeCB();
- return;
- }
- this.reportAnalyticsExplain = reportAnalyticsExplain;
- let videoAdId = this.getVideoAdId();
- if (this.m_videoAd == null) {
- this.m_videoAd = wx.createRewardedVideoAd({ adUnitId: videoAdId });
- }
- this.m_videoAd.load().then(() => {
- this.m_videoAd.show().then(() => { //播放视频广告完成
- }).catch((err) => { //播放视频广告错误
- // errorCB('视频显示错误 :' + JSON.stringify(err));
- this.m_videoAd.offClose(onClose);
- errorCB(err);
- completeCB && completeCB();
- console.error('视频显示错误', err);
- });
- });
- let onClose = (res) => {
- if (res && res.isEnded || res === undefined) {
- // 正常播放结束,可以下发游戏奖励
- successCB();
- console.debug('正常播放结束,可以下发游戏奖励');
- } else {
- // 播放中途退出,不下发游戏奖励
- errorCB('视频中途退出');
- console.error('视频中途退出');
- }
- this.m_videoAd.offClose(onClose);
- completeCB && completeCB();
- this.reportAnalyticsExplain = "null";
- };
- this.m_videoAd.onClose(onClose);
- this.m_videoAd.onError((err) => {
- this.m_videoAd.offClose(onClose);
- errorCB(err);
- completeCB && completeCB();
- console.error('视频错误', err);
- this.reportAnalyticsExplain = "null";
- });
- }
- 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 = wx.createInterstitialAd({
- adUnitId: adId,
- });
- insertAd.load();
- insertAd.onLoad(() => {
- console.warn('插屏广告 加载完成');
- insertAd.show()
- .then(() => {
- })
- .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();
- }
- });
- }
- protected isShowBanner: boolean = false;
- 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;
- }
- protected screenSize: { screenWidth: number, screenHeight: number } = null;
- private getBannerType(type: number) {
- if (!this.screenSize) {
- const { screenWidth, screenHeight } = wx.getSystemInfoSync();
- this.screenSize = { screenWidth: screenWidth, screenHeight: screenHeight };
- }
- let rect: Rect = new Rect()
- switch (type) {
- case 0:
- default: {
- rect.width = 300;
- rect.height = 100;
- rect.x = (this.screenSize.screenWidth - rect.width) / 2;
- rect.y = this.screenSize.screenHeight - rect.height;
- break;
- }
- }
- return rect;
- }
- showBannerAD(node?: Node, successCB?: () => void, errorCB?: (error: any) => void, onCloseCB?: () => void, setBannerPos?: ({ screenWidth, screenHeight }) => Vec2): void {
- if (this.isShowBanner) {
- successCB && successCB();
- return;
- }
- if (this.m_BannerAd == null) {
- let rect = this.getBannerType(0);
- this.m_BannerAd = wx.createBannerAd({
- adUnitId: this.m_BannerAdId[0],
- adIntervals: 30,
- style: {
- top: rect.y,
- left: rect.x,
- width: rect.width,
- // height: rect.height,
- }
- });
- this.m_BannerAd.onError(err => {
- console.log("banner广告加载失败", JSON.stringify(err));
- errorCB && errorCB(err);
- this.isShowBanner = false;
- });
- this.m_BannerAd.onClose(() => {
- // onCloseCB && onCloseCB();
- });
- }
- this.m_BannerAd.show().then(() => {
- console.log('banner广告展示完成');
- successCB && successCB();
- this.isShowBanner = true;
- }).catch((err) => {
- console.log('banner广告展示失败', JSON.stringify(err));
- errorCB && errorCB(err);
- this.isShowBanner = false;
- });
- this.m_BannerAd.onResize((res) => {
- console.log("微信 banner >>> onResize :", JSON.stringify(res));
- });
- }
- hideBanner() {
- if (this.m_BannerAd != null) {
- this.m_BannerAd.hide();
- this.isShowBanner = false;
- }
- }
- destroyBanner() {
- if (this.m_BannerAd != null) {
- this.m_BannerAd.destroy();
- this.m_BannerAd = null;
- this.isShowBanner = false;
- }
- }
- addIcon(): void {
- wx.hasShortcutInstalled({
- success: (status) => {
- if (status) {
- console.log('已创建');
- // LayerMgr.instance.show("已添加成功");
- } else {
- console.log('未创建');
- wx.installShortcut({
- success: () => {
- console.log('创建成功');
- }
- });
- }
- }
- });
- }
- protected m_NativeAdId: string = "adunit-3ce3a5e5b209c3b0";
- protected m_NativeAd: any = null;
- loadAndShowNativeAd(success: () => void, fail?: () => void): void {
- if (!wx.createCustomAd) { return; }
- this.hideBanner();
- // this.hideHomeNative();
- const { screenWidth, screenHeight } = wx.getSystemInfoSync();
- let banTouchNode: Node = new Node();
- banTouchNode.addComponent(BlockInputEvents);
- // banTouchNode. = screenWidth;
- // banTouchNode.height = screenHeight;
- banTouchNode.setSiblingIndex(9999);
- this.m_NativeAd = wx.createCustomAd({
- adUnitId: this.m_NativeAdId,
- adIntervals: 30,
- style: {
- left: (screenWidth - 360 * 0.9) / 2,
- top: (screenHeight - 352 * 0.9) / 2,
- width: 360 * 0.9,
- fixed: true,
- },
- });
- this.m_NativeAd.onError(err => {
- console.log("原生模板广告加载失败", JSON.stringify(err));
- fail && fail();
- banTouchNode.destroy();
- this.showBannerAD();
- // this.loadAndShowHomeNativeAd();
- });
- this.m_NativeAd.onClose(() => {
- this.m_NativeAd = null;
- banTouchNode.destroy();
- this.showBannerAD();
- // this.loadAndShowHomeNativeAd();
- });
- this.m_NativeAd.show().then(() => {
- console.log('原生模板广告展示完成');
- success && success();
- }).catch((err) => {
- console.log('原生模板广告展示失败', JSON.stringify(err));
- });
- }
- hideNativeAd(): void {
- if (this.m_NativeAd) {
- this.m_NativeAd.hide();
- this.m_NativeAd.destroy();
- this.m_NativeAd = null;
- }
- }
- protected m_HomeNativeAdId: string = "adunit-d66dd70787191558";
- protected m_HomeNativeAd: any = null;
- loadAndShowHomeNativeAd(success?: () => void, fail?: () => void) {
- if (!wx.createCustomAd) { return; }
- const { screenWidth, screenHeight } = wx.getSystemInfoSync();
- if (this.m_HomeNativeAd != null && !this.m_HomeNativeAd.isShow()) {
- this.m_HomeNativeAd.show().then(() => {
- console.log('首页原生模板广告展示完成 have');
- success && success();
- this.hideBanner();
- }).catch((err) => {
- console.log('首页原生模板广告展示失败 have', JSON.stringify(err));
- });
- return;
- }
- this.m_HomeNativeAd = wx.createCustomAd({
- adUnitId: this.m_HomeNativeAdId,
- adIntervals: 30,
- style: {
- left: (screenWidth - 360 * 0.8) / 2,
- top: (screenHeight - 106 * 0.8),
- fixed: true,
- },
- });
- this.m_HomeNativeAd.onError(err => {
- console.log("首页原生模板广告加载失败", JSON.stringify(err));
- fail && fail();
- });
- this.m_HomeNativeAd.onClose(() => {
- this.m_HomeNativeAd = null;
- });
- this.m_HomeNativeAd.show().then(() => {
- console.log('首页原生模板广告展示完成');
- success && success();
- }).catch((err) => {
- console.log('首页原生模板广告展示失败', JSON.stringify(err));
- });
- }
- hideHomeNative() {
- if (this.m_HomeNativeAd) {
- this.m_HomeNativeAd.hide();
- }
- }
- updateAdTimeLimit(): void {
- }
- protected m_gameRecordDuration: number = 0;
- protected m_gameRecordError: { code: number, message: string } = null;
- recordedGameScreenStart(params: recordedGameScreenParams, onStart?: (res: any) => void): void {
- console.log("wx-api-recordedGameScreenStart");
- if (!this.m_gameRecorder.isFrameSupported()) {
- onStart && onStart(false);
- console.log("不能录制");
- return;
- }
- this.m_gameRecorder.start(params);
- let onStartFun = () => {
- onStart && onStart(null);
- console.log("已经开始录制了");
- }
- let onErrorFun = (res: { code: number, message: string }) => {
- this.m_gameRecordError = res;
- console.log("录制出错", JSON.stringify(res));
- }
- let onStopFun = (res: { duration: number }) => {
- this.m_gameRecordDuration = res.duration;
- this.m_gameRecorder.off("start", onStartFun);
- this.m_gameRecorder.off("stop", onStopFun);
- this.m_gameRecorder.off("error", onErrorFun);
- console.log("停止录制", JSON.stringify(res));
- }
- this.m_gameRecorder.on("start", onStartFun);
- this.m_gameRecorder.on("stop", onStopFun);
- this.m_gameRecorder.on("error", onErrorFun);
- }
- recordedGameScreenStop() { return true; }
- recordedGameScreenStop_WX(cb?: (bool: boolean) => void) {
- if (!this.m_gameRecorder.isFrameSupported()) {
- cb && cb(false);
- console.log("不能录制-stop");
- return;
- }
- this.m_gameRecorder.stop().that(() => {
- cb && cb(this.m_gameRecordDuration > 0);
- });
- }
- shareRecordedGameScreen(successCb?: () => void, failCb?: (e?: any) => void): void {
- console.log("wx-api-shareRecordedGameScreen");
- successCb && successCb();
- }
- isHaveRecordedGameScreen(): boolean {
- return true;
- }
- protected m_gameRecorderShareButton: any = null;
- createGameRecorderShareButton(cb?: () => void): void {
- if (!this.m_gameRecorder.isFrameSupported()) {
- cb && cb();
- console.log("不能录制-createGameRecorderShareButton");
- return;
- }
- if (this.m_gameRecorderShareButton == null) {
- this.m_gameRecorderShareButton = wx.createGameRecorderShareButton({
- // 样式参数
- style: {
- // left: 10,
- // top: 150,
- // height: 50,
- // color: '#ffffff',
- // textAlign: 'center',
- // fontSize: 16,
- // borderRadius: 4,
- // iconMarginRight: 16,
- // paddingLeft: 1,
- // paddingRight: 30,
- },
- // 按钮的背景图片
- // image: 'button.jpg',
- text: GameCfg.GAME_VIDEO_SHARE_DESC,
- // icon: 'icon.jpg',
- // 分享参数
- share: {
- // query: 'a=1&b=2',
- // 背景音乐的路径
- bgm: 'soundBundle/bgm.mp3',
- timeRange: [[0, 1000], [2000, 3000]],
- }
- });
- }
- this.m_gameRecorderShareButton.show();
- this.m_gameRecorderShareButton.onTap(() => {
- console.log("gameRecorderShareButton >>> onTap");
- cb && cb();
- });
- }
- recordeGameScreenAbort(): void {
- this.m_gameRecorder.abort();
- }
- getStorageSync(key?: string) {
- let data = null;
- data = sys.localStorage.getItem(key)
- return data
- }
- removeStorage(key?: string) {
- if (!key) {
- sys.localStorage.clear()
- } else {
- sys.localStorage.removeItem(key);
- }
- }
- getDeviceInfo() {
- wx.getSystemInfo({
- success: function (res) {
- console.log(res);
- return res.model
- },
- fail: function (res) {
- console.log("获取设备信息失败!")
- }
- })
- let str: string = navigator.userAgent
- let res: RegExpMatchArray = str.match(/ipad/i)
- console.log("userAgent: ", str)
- if (res) return "ipad"
- if (sys.isBrowser && sys.os == sys.OS.IOS) {
- res = str.match(/iphone/i)
- if (res) return "iphone"
- if (window.screen.height > window.screen.width) return "ipad"
- return "mac"
- }
- return "unknow"
- }
- setUnlockAllTime(time: number) {
- sys.localStorage.setItem("unit_unlock_all", time)
- }
- getUnlockAllTime(call: Function) {
- if (call)
- call(sys.localStorage.getItem("unit_unlock_all"))
- }
- setUnitState(state?: string) {
- sys.localStorage.setItem("unit_state", state)
- }
- setSoundInfo(sound?: boolean, music?: boolean) {
- sys.localStorage.setItem("sound", JSON.stringify({ sound: sound, music: music }))
- }
- onKeyboardConfirm(cb: (str: any) => void) {
- // throw new Error("Method not implemented.");
- }
- showShareMenu(successCb: () => any, failCb: (res: any) => void): void {
- // throw new Error("Method not implemented.");
- console.log('微信分享未实现')
- successCb && successCb();
- }
- protected m_IsLogin: boolean;
- vibrateLong(): void {
- throw new Error("Method not implemented.");
- }
- addShortcut(successCb: () => void, failCb: (res: any) => void): void {
- throw new Error("Method not implemented.");
- }
- reportAnalytics(name: any, data: any): void {
- // throw new Error("Method not implemented.");
- }
- public m_AdCnt: number;
- loadBannerAd() {
- }
- }
|