Files
fc/dev/project/assets/Scripts/mod/mainui/cmp/cmp.main.ads.js
T
2026-05-23 22:10:14 +08:00

172 lines
3.9 KiB
JavaScript

/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* 广告图轮播
*
*
******************************************************************/
const BridgeComponent = require( "bridge.component" );
const SEC_DELAY = 2;
cc.Class( {
extends: BridgeComponent,
properties: {
autoSecs: { default: 3 },
adPages: { default: null, type: cc.PageView },
tPage: { default: null, type: cc.Node },
},
// 显示
onEnable: function() {
// 清空
this.adPages.removeAllPages();
// 视图绑定
if( nx.bridge.acts ) {
nx.bridge.acts.vbind( this, [
[ "foreshow", this.onFreshAds.bind( this ) ]
] );
}
// 自动弹出
this.popDone = false;
this.scheduleOnce( () => {
this.tryAutoPopup( true );
}, SEC_DELAY );
},
// 关闭
onDisable: function() {
// 视图绑定
if( nx.bridge.acts ) {
nx.bridge.acts.vunbind( this );
}
},
// 重建轮播列表
onFreshAds: function( _data ) {
// 空
if( nx.dt.arrEmpty( _data ) ) {
this.node.opacity = 0;
return;
}
// 清空
this.adPages.removeAllPages();
this.node.opacity = 255;
this.ads = nx.dt.objClone( _data );
this.toLeft = true;
this.ads.forEach( ad => {
this.addPage( ad );
} );
// 自动轮播
this.unscheduleAllCallbacks();
this.schedule( () => {
this.nextAd();
}, this.autoSecs );
},
// 添加广告图
addPage: function( _ad ) {
// 创建新页面
let page = cc.instantiate( this.tPage );
page.name = "" + _ad.id;
page.adinfo = _ad;
let path = cc.path.join( "locals", nx.getLocLanguage(), "images/ads", _ad.sc );
nx.gui.setSpriteFrame( page, "img", path );
// 加入PV
this.adPages.addPage( page );
},
// 下一张
nextAd: function() {
let total = this.adPages.getPages().length;
let cur = this.adPages.getCurrentPageIndex();
// 左移
if( this.toLeft ) {
let next = cur + 1;
if( total > 1 ){
if( next < total ) {
this.adPages.scrollToPage( next );
return;
}
}else{
if( next <= total ) {
this.adPages.scrollToPage( next );
return;
}
}
this.toLeft = false;
this.nextAd();
}
else {
let next = cur - 1;
if( next >= 0 ) {
this.adPages.scrollToPage( next );
return;
}
this.toLeft = true;
this.nextAd();
}
},
// 点击广告图
onTouchAd: function( _ad ) {
let info = _ad ? _ad.adinfo : {};
if( nx.dt.objNEmpty( info ) ){
if( nx.dt.strNEmpty( info.sk ) ) {
nx.bridge.cmd.doStr( info.sk );
}
}
},
// 自动弹出
tryAutoPopup: function( _reset = false ) {
let pop = nx.storage.get( "foreshow" );
// 已经弹出过了
if( _reset ) { this.popDone = false; }
if( this.popDone || ( pop && pop == 1 ) ) {
return;
}
// 弹出
let list = nx.bridge.acts.vget( "foreshow" );
if( nx.dt.arrNEmpty( list ) ) {
this.popDone = true;
nx.bridge.createPanel( "WndActsForeshow" );
nx.storage.set( "foreshow", 1 );
return;
}
// 延迟再试
this.scheduleOnce( () => {
this.tryAutoPopup();
}, SEC_DELAY );
},
} );