123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- const FriendSystemApi = require('../net/FriendSystemApi');
- const ListViewAdapter = require('ListViewAdapter');
- const GameModule = require("../utils/GameModule");
- 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);
- });
- // 尝试触发抢夺好友引导
- let friends = GameModule.tab.getComponent('Tab').friends
- GameModule.homeGuide.getComponent('HomeGuide').handleGuideState14(friends)
- }
- this.listAdapter = new ListViewAdapter(this.scrollView);
- },
- onEnable() {
- this.timeline = 0;
- this.hasNext = 0;
- this.getNetworkData(false);
- this.canLoad = true;
- this.scrollView.scrollToTop(0);
- },
- 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);
- }
- },
- onScrollEvent(){
- if (this.listAdapter != undefined) {
- this.listAdapter.update();
- }
- },
- });
|