123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- import NavtiveData from "./NavtiveData";
- import SDK, { NodeAction } from "../SDK";
- import Utils from "../tools/Utils";
- import { NativeType } from "./NativeAdComponent";
- import Banner from "./Banner";
- import Interstitial, { InterstitialType } from "./Interstitial";
- import Video from "./Video";
- import Icon from "./Icon";
- // Learn TypeScript:
- // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
- // Learn Attribute:
- // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
- const { ccclass, property } = cc._decorator;
- // {"id" : 1, "open" : true,"info" : "主界面插屏" },
- // {"id" : 2, "open" : true, "info" : "关卡插屏" },
- // {"id" : 3, "open" : true, "info" : "皮肤插屏" },
- // {"id" : 4, "open" : true, "info" : "设置插屏" },
- // {"id" : 5, "open" : true, "info" : "暂停插屏" },
- // {"id" : 6, "open" : true, "info" : "游戏插屏" },
- // {"id" : 7, "open" : true, "info" : "胜利插屏" },
- // {"id" : 8, "open" : true, "info" : "失败插屏" },
- // {"id" : 9, "open" : true, "info" : "复活插屏" }
- @ccclass
- export default class VivoAd extends cc.Component {
- private static _instance: VivoAd = null;
- private _config = null;
- private _banner: Banner;
- private _interstitial: Interstitial;
- private _video: Video;
- private _icon : Icon;
- private _vivoNativeDateList = [];
- private _hideCallback = null;
- private _showCallback = null;
- private _nativeAdInitTime = 0;
- private _utils: Utils;
- private _netType;
- private _adCreating = false;
- public static get Instance(): VivoAd {
- if (VivoAd._instance == null) {
- VivoAd._instance = new VivoAd();
- }
- return VivoAd._instance;
- }
- constructor() {
- super();
- let self = this;
- this._utils = Utils.instance;
- this._banner = new Banner();
- this._interstitial = new Interstitial();
- this._video = new Video();
- this._icon = new Icon();
- }
- public getBanner() {
- return this._banner;
- }
- public getConfig() {
- return this._config;
- }
- public onListenHome() {
- let self = this;
- let isBG = false;
- cc.game.on(cc.game.EVENT_SHOW, function (event) {
- if (isBG) {
- self._utils.log("切换前台");
- isBG = false;
- if (self._showCallback) {
- self._showCallback();
- }
- }
- });
- cc.game.on(cc.game.EVENT_HIDE, function (event) {
- if (!isBG) {
- self._utils.log("切换后台");
- isBG = true;
- if (self._hideCallback) {
- self._hideCallback();
- }
- }
- });
- }
- public initConfig(config, callBack?) {
- this._config = config;
- this._utils.log("config:------- " + JSON.stringify(config));
- if (config) {
- this._banner.init(config);
- this._interstitial.init(config);
- this._video.init(config);
- this._icon.init(config);
- this.creatorNativeData(callBack);
- this.schedule(() => {
- this.creatorNativeData(null);
- }, 15);//开启定时器,定时请求广告
- }
- }
- public onHide(callbacks) {
- this._hideCallback = callbacks;
- }
- public onShow(callback) {
- this._showCallback = callback;
- }
- public getNetType(cb) {
- qg.getNetworkType({
- success: function (data) {
- console.log(`handling success: ${data.type}`)
- if(cb){
- cb(data.type);
- }
- // VivoAd._instance ._netType = data.type;
- }
- });
- }
- //广告初始化
- public init() {
- }
- //创建广告
- public creatorNativeData(callBack?) {
- let self = this;
- if (self._adCreating) {
- this._utils.log("----有广告未完成创建 ");
- return;
- }
- this._utils.log("--------load ad----------");
- if (this._vivoNativeDateList.length >= 2) {
- this._utils.log("----广告未消耗,广告列表: " + this._vivoNativeDateList.length);
- return;
- }
- let curTime = new Date().getTime();
- let bigTime = (curTime - this._nativeAdInitTime) / 1000;
- this._utils.log("--------load ad time : " + bigTime);
- let initADgo = () => {
- self._adCreating = true;
- let navtiveData = new NavtiveData();
- let go = (success) => {
- self._adCreating = false;
- if (success) {
- self._nativeAdInitTime = new Date().getTime();
- self._vivoNativeDateList.push(navtiveData);
- this._utils.log("--------广告缓存成功,列表: " + this._vivoNativeDateList.length);
- }
- if (callBack) {
- callBack(success);
- }
- }
- navtiveData.initAd(self._config, go);
- }
- if (bigTime < 3.1) {//广告加载时间,离上次未到3秒,等待剩余时间
- let time = 3.1 - bigTime;
- this._utils.log("--------离上次未到3秒,等待剩余时间 : " + bigTime);
- this.scheduleOnce(initADgo, time);
- return;
- }
- initADgo();
- }
- public hasNativeData() {
- if (this._vivoNativeDateList.length > 0) {
- this._utils.log("----列表已存在广告: " + this._vivoNativeDateList.length);
- return true;
- }
- return false;
- }
- //获取广告数据
- public getVivoNativeData() {
- this._utils.log("----广告列表: " + this._vivoNativeDateList.length);
- let date = null;
- if (this._vivoNativeDateList.length > 0) {
- date = this._vivoNativeDateList.shift();
- }
- if (date && date.isReady()) {
- this._utils.log("-----存在新广告,用新的--------");
- return date;
- } else {
- this._utils.log("-----不存在广告或只存在旧广告--------");
- }
- this._utils.log("-----广告列表还有: " + this._vivoNativeDateList.length);
- return null;
- }
- /**
- * 展示原生banner
- * @param top 是否顶部显示
- */
- public showBanner(type: NativeType = null, top: boolean = false, y: number = 0, x: number = 0) {
- if (this._banner)
- {
- this._banner.showBanner(type, top, x, y);
- }
- }
- public hideBanner() {
- if (this._banner) {
- this._banner.hideBanner();
- }
- }
- /**
- * 隐藏原生icon
- */
- public hideIcon() {
- if(this._icon){
- this._icon. hideIcon();
- }
- }
- /**
- * 展示原生插屏
- * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
- * @param id 默认0
- * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false
- */
- public showInterstitial(type: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) {
- if (this._interstitial) {
- this._interstitial.showInterstitial(type);
- }
- }
- /**
- * 展示原生icon广告
- * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
- * @param id
- */
- public showIconAd(id: number = 0, x: number, y: number) {
- if(this._icon){
- this._icon.showIconAd(id,x,y);
- }
- }
- /*显示视频*/
- public showRewardVideo(callBacks: Function, fail: Function, noAd: Function) {
- this._utils.log("---------showRewardVideo-----");
- let self = this
- let sucCall = () => {
- if (callBacks) {
- callBacks();
- }
- }
- let failCall = () => {
- if (fail) {
- fail();
- }
- }
- let noAdCall = () => {
- if (noAd) {
- noAd();
- } else {
- self.showToast("暂无广告!");
- }
- }
- if (this._video)
- this._video.showRewardVideo(sucCall, failCall, noAdCall);
- }
- /**获取剪切板 */
- public getClipboardData(callbacks) {
- let self = this;
- qg.getClipboardData({
- success: function (data) {
- if (callbacks) {
- callbacks(data.text);
- }
- self._utils.log(`Clipboar handling success: ${data.text}`)
- },
- fail: function (data, code) {
- self._utils.log(`handling fail, code = ${code}`)
- }
- })
- }
- public getSystemInfoSync() {
- return qg.getSystemInfoSync();
- }
- public getUserId() {
- let user = qg.getUserIdSync();;
- if(user && user.userId){
- return user.userId;
- }
- return "vivo not user id";
- }
- /**
- * 用户来源
- */
- public getSource() {
- return qg.getLaunchOptionsSync();
- }
- public showToast(message: string) {
- qg.showToast({
- message: message
- })
- }
- }
|