egret中将所需要的数据封装成对象,再获取相关数据时就可以使用这个对象来获取,通过判断不同的类型来使同一个UI面板上显示不同的效果

点击不同按钮弹出同一个面板上的不同效果,通过传入类型来判断:

this.btnRight.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onUpgradeforgift,this);

this.btnLeft.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onPayforgift,this);

 

private onUpgradeforgift(evt:egret.TouchEvent){
  manager.PopupManager.addPopup(new UpgradeForGift(2));
}
private onPayforgift(evt:egret.TouchEvent){
  manager.PopupManager.addPopup(new UpgradeForGift(1));
}

UpgradeForGift.ts文件:

module view{
export class UpgradeForGift extends BasePanel{
private static INVITE_FRIEND_CALL_ABCK:string = "INVITE_FRIEND_CALL_ABCK";
private btn_back:eui.Image;
private cellScroller:eui.Scroller;
private cellGroup:eui.Group;
private list:utils.List;

public inviteGiftImg:eui.Image; //邀请送礼包
public upgradeImg:eui.Image; //好友升级送豪礼 根据类型判断显示不同的标题
private rewardDes:eui.Label;   //根据类型的不同,显示不同的文字
private smallTitle:eui.Label;
private tip:eui.Image;

private inviteeRewardInfo:Array<any>
public info:Array<any>
private inviteeArr:Array<any>;
private InviteeLevel:number;
private rewardArr:Array<any>;
private _dataArr:Array<any>;
public panelType:number
private addReward:Array<any>
public constructor(type:number) {  //传入用于判断类型的参数
super();
this.panelType = type;//1充值返 2等级返
this.onLoadUI("resource/eui_skins/invitefriend/UpgradeforGift.exml", this.init,"UpgradeforGift");
}

private init(){
// this.inviteTitle.source = "invitefriend_json.gift_2";

this.btnClose = this.btn_back;
this.btnCloseAddListener();
this.onCloseFun = this.hide;

this.list = new utils.List(100,1,view.UpgradeItemForGift,0,0,"",true,this.cellGroup);

if(this.panelType == 1)
{
this.upgradeImg.source = "invitefriend_json.gift_1";
this.upgradeImg.x = 353;
this.rewardDes.text = "当您邀请的好友每次充值购买礼包时,您都可以获得礼包内元宝数量的20%作为奖励。";
this.smallTitle.text = "您的邀请返利";
this.tip.visible = false;
var cmd:Net.InvitePayRewardCommand = new Net.InvitePayRewardCommand();   //不同的类型发送不同的协议
cmd.sucessCallBack = this.setData.bind(this);
cmd.send();
}
else if(this.panelType == 2)
{
this.upgradeImg.source = "invitefriend_json.gift_2";
this.upgradeImg.x = 278;
this.rewardDes.text = "当您邀请的好友主城到达相应等级时,您可以获得相应的等级奖励。";
this.smallTitle.text = "您的好友升级奖励";
this.tip.visible = true;
var cmd:Net.InviteeLevelRewardInfoCmd = new Net.InviteeLevelRewardInfoCmd();//判断不同类型发送不同协议
cmd.sucessCallBack = this.setData.bind(this);
cmd.send();
}


// observer.ComDispatcher.addGlobalEventListerner(InviteFriend.INVITE_FRIEND_CALL_ABCK,this.shareSuccuess,this);
}

 

private setData(obj:Object){ //返回数据

this._dataArr = [];
if(!obj){
return;
}
var arr:Array<any>;
if(this.panelType == 1)
{
arr = obj["InvitePayArr"];     //通过打断点来知道对象的各个属性,InvitePayArr就是对象obj的一个属性,是一个数组,以此来获得属性。
var _index:number = 0;
for(var i:number = 0; i < arr.length; i++)  
{
var _friends:Object = arr[i];     //将每条数据都提取出来,并将各个属性都声明出来做备用。 
var _InviteeName:string = _friends["InviteeName"];
var _InviteePic:string = _friends["InviteePic"];
var _InviteeUid:string = _friends["InviteeUid"];
var _productId:string = _friends["productId"];
var _status:number = Number(_friends["InviteeRewardInfo"]);
if(_status == 0)            //如果有要满足的另外一个限定条件,则可以这样用if限定
{
var _obj:Object = new Object();   //通过将所要用到的所有对象的属性都封装到一个对象里,来方便以后对数据的使用
_obj["InviteeName"] = _InviteeName;
_obj["InviteePic"] = _InviteePic;
_obj["InviteeUid"] = _InviteeUid;
_obj["productId"] = _productId;
_obj["status"] = _status;
_obj["index"] = _index++;     //这条是通过后加一个index属性,来在UpgradeForGiftItem文件中通过判断index的奇偶性来设置背景
_obj["panelType"] = this.panelType;   //如果要用到其他的一些属性,也可以封装到这个对象里,如用于判断类型的panelType,状态status,索引index
this._dataArr.push(_obj);
}
}
}
else if(this.panelType == 2)
{
arr= obj["InviteeArr"]; //好友数组
this.addReward = obj["addReward"];//获取奖励的数组
this.tip.addEventListener(egret.TouchEvent.TOUCH_TAP,this.showTip,this);
for(var i:number = 0; i < arr.length; i++)
{
var _friends:Object = arr[i];
var _InviteeLevel:number = Number(_friends["InviteeLevel"]);
var _InviteeName:string = _friends["InviteeName"];
var _InviteePic:string = _friends["InviteePic"];
var _InviteeUid:string = _friends["InviteeUid"];
var _statusArr:Array<number> = _friends["InviteeRewardInfo"].split("|");
var _index:number = 0;
for(var j:number = 0; j < _statusArr.length; j++)
{
if(_InviteeLevel >= this.addReward[j]["Level"] && Number(_statusArr[j]) == 0)
{
var _obj:Object = new Object();
_obj["InviteeName"] = _InviteeName;
_obj["InviteeLevel"] = _InviteeLevel;
_obj["rewardLevel"] = this.addReward[j]["Level"];
_obj["InviteePic"] = _InviteePic;
_obj["InviteeUid"] = _InviteeUid;
_obj["status"] = Number(_statusArr[j]);
_obj["index"] = _index++;
_obj["panelType"] = this.panelType;
this._dataArr.push(_obj);
}
}
}
}
this.list.data = this._dataArr; //判断类型后,将不同的数据装进数组中就好,而不用分别装进两个数组,再通过不同类型设置list的值。
}

private showTip(){
manager.PopupManager.addPopup(new view.Tip("",this.addReward),false,true);

}


private sortOrderByStr(arr: Array<any>, str: string) {
var str1: any;
var str2: any;
arr.sort((a, b) => {
str1 = a[str];
str2 = b[str];
return str1 - str2;
});
}

private hide(): void {
manager.PopupManager.removePopup(this);
this.dispose();
}

public dispose(){
super.dispose();
}
}
}

 UpgradeItemForGift.ts文件

module view{
export class UpgradeItemForGift extends utils.ItemRendererBase{
private inviteeLevel:eui.Label;
private inviteeName:eui.Label;
private inviteePic:eui.Image;
private btnText:eui.Label;
private btn:eui.Button;

private bg2:eui.Image;
private bg1:eui.Image;

private constructor() {
super();
this.itemWidth = 1080;
this.itemHeight = 150;
this.onLoadUI("resource/eui_skins/invitefriend/UpgradeItemForGift.exml", this.init.bind(this),"UpgradeItemForGift");
}

private inviteeRewardInfo:Array<any>
public info:Array<any>
private inviteeArr:Array<any>;
private InviteeLevel:number;
private rewardArr:Array<any>;

public init(){
  if(this.__data)
  {
    this.data = this.__data;
    this.initData();   //此处加这句是为了在资源加载完后,数据还未进行设置
  }
}
private __data:Object;
public get data():any{
  return this.__data;
}
public set data(param1: any) {

  this.__data = param1;

  if (!this.loadComplete) {//判断是否加载完用非号而不是false或null
    return;
  }
  if (this.__data == null) {//判断数据是否有,用null
    return
  }

  this.initData();    //通常将设置数据提出来另外用方法封装,然后再setdata里面调用;而不是直接将一大串子都放在setdata方法里面

}

private initData()
{
  if (!this.__data)
  {
  return;
  }
  var level = parseInt(this.__data["index"].toString()); //此处的index就是在上个文件中设置封装所有数据的对象时也作为属性设置的;
  if (level % 2 == 1) {
    this.bg2.visible = true;
    this.bg1.visible = false;
  } else if (level % 2 == 0) {
    this.bg2.visible = false;
    this.bg1.visible = true;
  }

if(this.__data["panelType"] == 1)   //通过判断不同的类型来设置不同数据
{
  this.inviteeName.text = this.__data["InviteeName"] + " 购买了";
  var _productID:string = this.__data["productId"];
  var obj:model.GoldExchangeItem = cache.CommonCache.instance.currentPlayer.goldExchangeList[_productID];
  this.btnText.text = "领取奖励";

  this.inviteeLevel.text = loader.ResLoader.getValue(obj.name) + "礼包";
}
else if(this.__data["panelType"] == 2)
{
this.inviteeName.text = this.__data["InviteeName"] + "";
this.inviteeLevel.text = "等级" + this.__data["InviteeLevel"] + "";
this.btnText.text = "领取" +this.__data["rewardLevel"] + "级奖励";
}
this.inviteePic.source = "resource/assets/Image/icons/head/" + this.__data["InviteePic"] + ".png";
if(this.__data["status"] == 0)
{
this.btn.visible = true;
}
else
{
this.btn.visible = false;
}
if(this.btn)
{
this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onGetReward,this);
}
}

private onGetReward()
{
  this.__data["status"] = 1;
  this.btn.visible = false;
  this.btnText.visible = false;
  if(this.__data["panelType"] == 1)  //通过判断所在的不同类型面板,来给后台发送不同的协议
  {
    var cmd:Net.InvitePayGetRewardCommand = new Net.InvitePayGetRewardCommand(this.__data["InviteeUid"],this.__data["productId"]);
    cmd.send();
  }
  else if(this.__data["panelType"] == 2)
  {
    var cmd:Net.InviteeLevelGetRewardCmd = new Net.InviteeLevelGetRewardCmd(this.__data["InviteeUid"],this.__data["rewardLevel"]);
    cmd.send();
  }
}
public dispose()
{
if(this.btn)
{
this.btn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onGetReward,this);
}
}
}
}

InviteePayGetRewardCommand.文件    //协议文件

module Net {
export class InvitePayGetRewardCommand extends BaseCommand{
public static TYPE:string = "invite.getPayreward";    //这个指令要和后台能接收到的指令相同,就是发给后台的就是这条指令
public constructor(inviteeUid:string,orderId:string) {
  super(InvitePayGetRewardCommand.TYPE);
  this.putParams("inviteeUid",inviteeUid);     //传的是后台的参数,两个参数是key和value , key应该是和后台传过来的名称相同的。
  this.putParams("orderId",orderId);
}
public handleReceive(response: Net.BaseResponse) {
  if (this["type"] != response["type"]) {
    throw new Error('Wrong response handle').message;
  }
  if (response["data"].hasOwnProperty("errorCode")) {
    this.callFail(response["data"]);
  }
  else {
    this.callSucess(response["data"]);
    var addDollar:number = Number(response["data"]["dollar"]);
    if (addDollar && !isNaN(addDollar)) {
  cache.CommonCache.instance.currentPlayer.gold += addDollar;
  }
  assist.TipAsist.getInstance().showTip("领取好友购买礼包奖励成功");
  observer.ComDispatcher.dispatchGlobalEvent(new observer.ComEvent(observer.COMEVENTTYPE.MSG_RESOURCE_CHANGE));//派发事件
}
}
}
}

posted @ 2017-06-28 10:16  RitaLee  阅读(1206)  评论(0编辑  收藏  举报