201 lines
5.9 KiB
JavaScript
201 lines
5.9 KiB
JavaScript
|
|
/******************************************************************
|
|
* Copyright(C) 2019 - 2020 Nx Studio
|
|
*
|
|
* 通用支付项
|
|
*
|
|
* 2024.01.24
|
|
******************************************************************/
|
|
|
|
const SVCItem = require( "nx.fx.sv.expand.item" );
|
|
const Payment = require( "payment.mod" );
|
|
|
|
// 价格标记
|
|
const PFS = {
|
|
TimeLimit: 2, // 次数上限
|
|
TimeCurrent: 6, // 已购买次数
|
|
PriceRaw: 26, // 原价
|
|
PriceDisInfo: 25, // 折扣信息
|
|
PriceDiscount: 27, // 折后价
|
|
SortRight: 39, // 排序
|
|
};
|
|
|
|
cc.Class( {
|
|
|
|
extends: SVCItem,
|
|
|
|
properties: {
|
|
|
|
// 基本信息
|
|
fabItem: { default: null, type: cc.Prefab },
|
|
nodGoods: { default: null, type: cc.Node },
|
|
nodName: { default: null, type: cc.Node },
|
|
nodReaminTimes: { default: null, type: cc.Node },
|
|
|
|
// 价格
|
|
nodRMB: { default: null, type: cc.Node },
|
|
nodDiamond: { default: null, type: cc.Node },
|
|
|
|
// 具体操作
|
|
nodBuy: { default: null, type: cc.Node },
|
|
nodDone: { default: null, type: cc.Node },
|
|
|
|
// 回调
|
|
doneEvent: { default: [], type: cc.Component.EventHandler, displayName: "购买回调" },
|
|
|
|
},
|
|
|
|
// 数据重置
|
|
rebind: function( _idx, _data, _key ) {
|
|
this._super( _idx, _data, _key );
|
|
this.setData( _data );
|
|
},
|
|
|
|
// 设置
|
|
setData: function( _data ) {
|
|
|
|
this.mdata = nx.dt.objClone( _data );
|
|
|
|
// 名字刷新
|
|
if( this.nodName ) {
|
|
let name = nx.text.getKey( _data.aim_str );
|
|
nx.gui.setString( this.nodName, "", name );
|
|
}
|
|
|
|
// 商品刷新
|
|
if( this.nodGoods && this.fabItem ) {
|
|
|
|
let items = _data.item_list || [];
|
|
if( nx.dt.arrEmpty( items ) ) {
|
|
this.nodGoods.active = false;
|
|
} else {
|
|
this.nodGoods.active = true;
|
|
for( let i = 0; i < this.nodGoods.children.length; ++i ) {
|
|
let info = items[ i ];
|
|
let node = this.nodGoods.children[ i ];
|
|
if( nx.dt.objEmpty( info ) ) {
|
|
node.active = false;
|
|
continue;
|
|
}
|
|
|
|
node.active = true;
|
|
if( node.children.length == 0 ) {
|
|
let item = cc.instantiate( this.fabItem );
|
|
item.parent = node;
|
|
item.position = cc.v2( 0, 0 );
|
|
}
|
|
let cmp = node.children[ 0 ].getComponent( "cmp.item.base" );
|
|
cmp.setData( info.bid, info.num );
|
|
}
|
|
}
|
|
}
|
|
|
|
// 数据统计
|
|
this.infos = {};
|
|
for( let i = 0; i < _data.aim_args.length; ++i ) {
|
|
|
|
let tmp = _data.aim_args[ i ];
|
|
if( !tmp ) {
|
|
continue;
|
|
}
|
|
|
|
switch( tmp.aim_args_key ) {
|
|
|
|
case PFS.TimeLimit: {
|
|
this.infos.maxTimes = tmp.aim_args_val;
|
|
} break;
|
|
case PFS.TimeCurrent: {
|
|
this.infos.curTimes = tmp.aim_args_val;
|
|
} break;
|
|
case PFS.PriceRaw: {
|
|
this.infos.price = tmp.aim_args_val;
|
|
} break;
|
|
case PFS.PriceDisInfo: {
|
|
this.infos.disinfo = tmp.aim_args_val;
|
|
} break;
|
|
case PFS.PriceDiscount: {
|
|
this.infos.disprice = tmp.aim_args_val;
|
|
} break;
|
|
case PFS.SortRight: {
|
|
this.infos.sort = tmp.aim_args_val;
|
|
} break;
|
|
default: break;
|
|
};
|
|
}
|
|
|
|
// 价格刷新
|
|
if( this.nodRMB || this.nodDiamond ) {
|
|
let price = this.infos.disprice || this.infos.price || 0;
|
|
if( _data.aim == 0 ) {
|
|
nx.gui.setActive( this.nodRMB, "", false );
|
|
nx.gui.setActive( this.nodDiamond, "", true );
|
|
nx.gui.setString( this.nodDiamond, "txt", price );
|
|
} else {
|
|
nx.gui.setActive( this.nodRMB, "", true );
|
|
nx.gui.setActive( this.nodDiamond, "", false );
|
|
let txt = Payment.getInstance().fmtPrice( price );
|
|
nx.gui.setString( this.nodRMB, "txt", txt );
|
|
}
|
|
}
|
|
|
|
// 变数刷新
|
|
this.freshInfos();
|
|
|
|
},
|
|
|
|
// 变数刷新
|
|
freshInfos: function() {
|
|
|
|
// 次数刷新
|
|
let times = ( this.infos.maxTimes - this.infos.curTimes ) || 0;
|
|
if( this.nodReaminTimes ) {
|
|
nx.gui.setString( this.nodReaminTimes, "", times );
|
|
}
|
|
|
|
// 操作刷新
|
|
nx.gui.setActive( this.nodBuy, "", times > 0 );
|
|
nx.gui.setActive( this.nodDone, "", times <= 0 );
|
|
},
|
|
|
|
// 点击购买
|
|
onTouchBuy: function() {
|
|
|
|
if( !this.mdata || !this.infos ) {
|
|
return;
|
|
}
|
|
|
|
let self = this;
|
|
let done = function() {
|
|
self.infos.curTimes += 1;
|
|
self.freshInfos();
|
|
};
|
|
|
|
// 如果aim=0则为钻石购买,直接抛出
|
|
if( this.mdata.aim == 0 ) {
|
|
this.doneEvent.forEach( _event => {
|
|
if( _event && _event.customEventData === "" ) {
|
|
_event.customEventData = {
|
|
diamond: true,
|
|
data: this.mdata,
|
|
cb: () => {
|
|
done();
|
|
}
|
|
};
|
|
}
|
|
} );
|
|
cc.Component.EventHandler.emitEvents( this.doneEvent );
|
|
return;
|
|
}
|
|
|
|
// 支付
|
|
Payment.getInstance().reqPayment( this.mdata.aim, ( _ret ) => {
|
|
if( _ret && nx.dt.arrNEmpty( this.doneEvent ) ) {
|
|
done();
|
|
cc.Component.EventHandler.emitEvents( this.doneEvent );
|
|
}
|
|
} );
|
|
|
|
},
|
|
|
|
} );
|