const FriendSystemApi = require('../net/FriendSystemApi'); const ListViewAdapter = require('ListViewAdpater'); cc.Class({ extends: cc.Component, properties: { item: cc.Prefab, itemScriptName: '', scrollView: cc.ScrollView, content: cc.Node, tipLabel: cc.Label, // 0是好友列表, 1是推荐好友 tab: 0, items: [], /** * 发送添加好友之后的Toast提示 */ addFriendApplySended: cc.Node, requestDuration: 10, friendLastRequestTime: -1, recommentRequestTime: -1, dataList: [], canLoad: true, }, // LIFE-CYCLE CALLBACKS: onLoad() { if (this.tab === 0) { GameEvent.on('refresh_friend_list', this, () => { this.timeline = 0; this.getNetworkData(true); }); GameEvent.on("friend_relation_change", this, () => { this.timeline = 0; this.getNetworkData(true); }); } this.listAdapter = new ListViewAdapter(this.scrollView); }, onEnable() { this.timeline = 0; this.hasNext = 0; this.getNetworkData(false); this.canLoad = true; }, getNetworkData(ignoneRequestDuration) { if (this.canLoad) { console.log('canload'); this.canLoad = false; let self = this; if (this.tab === 0) { let offset = (Date.parse(new Date()) - this.friendLastRequestTime) / 1000; if (ignoneRequestDuration || this.friendLastRequestTime === -1 || offset > this.requestDuration) { this.friendLastRequestTime = Date.parse(new Date()); FriendSystemApi.getFriend(this.timeline, 20, this.configData.bind(self), this.networkError.bind(self)); } } else { let offset = (Date.parse(new Date()) - this.recommentRequestTime) / 1000; if (ignoneRequestDuration || this.recommentRequestTime === -1 || offset > this.requestDuration) { this.recommentRequestTime = Date.parse(new Date()); FriendSystemApi.getFriendRecommend(this.timeline, 20, this.configData.bind(self), this.networkError.bind(self)); } } } }, networkError(error) { console.log(error); this.tipLabel.node.active = true; this.tipLabel.string = '网络错误'; this.content.active = false; this.canLoad = true; }, configData(responseData) { console.log('configData'); if (this.timeline === 0) { this.dataList = responseData.users; this.layout(); } else { this.listAdapter.addData(responseData.users); } this.timeline = responseData.timeline; this.hasNext = responseData.next; this.canLoad = true; }, layout() { console.log('layout'); if (this.dataList === undefined || this.dataList === null || this.dataList.length === 0) { this.tipLabel.node.active = true; this.tipLabel.string = this.tab === 0 ? '暂时没有好友陪你玩这个游戏,赶紧去邀请好友一起来玩吧!' : '暂时没有好友推荐'; this.content.active = false; } else { this.tipLabel.node.active = false; this.content.active = true; this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName); this.listAdapter.setLoadMoreCallBack(() => { this.loadMore(); }); } }, loadMore() { if (this.hasNext > 0) { getNetworkData(true); } }, update(dt) { if (this.listAdapter != undefined) { this.listAdapter.update(dt); } }, showToast() { this.addFriendApplySended.active = true; this.scheduleOnce(() => { this.addFriendApplySended.active = false; }, 2); }, onDestroy() { if (this.tab === 0) { GameEvent.off('refresh_friend_list', this); GameEvent.off("friend_relation_change", this); } } });