cc.Class({ extends: cc.Component, properties: { scrollView: cc.ScrollView, layout: cc.Layout, rankItem: cc.Prefab, meNode: cc.Node, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.listItem = []; for(let i = 0; i < 5; i++) { let item = cc.instantiate(this.rankItem); this.layout.node.addChild(item); item.active = false; this.listItem.push(item); } // this.winSize = cc.view.getVisibleSize(); // if (this.winSize.height <= 1000) { // this.scrollView.vertical = true; // } }, start () { if (window.wx != undefined) { window.wx.onMessage(data => { if (data.messageType == 0) { //好友排行榜 this.isGroup = false; this.loadFriendData(); } else if (data.messageType == 1) { //好友排行榜上一页 this.previousPage(); } else if (data.messageType == 2) { //好友排行榜下一页 this.nextPage(); } else if (data.messageType == 3) { //群排行榜 this.isGroup = true; this.loadGroupData(data.key1, data.key2); } else if (data.messageType == 4) { //排行榜关闭销毁 this.clearData(); } }) } }, //清除所有数据 clearData() { this.isGroup = false; this.listItem.forEach(n => { n.active = false; }); this.meNode.active = false; this.friendPageIndex = 0; this.groupPageIndex = 0; this.friendRanks = []; this.groupRanks = []; }, loadFriendData() { this.winSize = cc.view.getVisibleSize(); console.log('view size ' + this.winSize); console.log('node node size ' + this.scrollView.node.width + ' ' + this.scrollView.node.height); this.friendPageIndex = 0; this.listItem.forEach(n => { n.active = false; }); this.meNode.active = false; //ranks数组大于0证明已经加载过有数据不用再加载 if (this.friendRanks && this.friendRanks.length > 0) { this._setupFriendList(); this._setupFriendMyData(); } else { if (window.wx != undefined) { let self = this; wx.getUserInfo({ openIdList: ['selfOpenId'], success: (userRes) => { if (userRes.data.length > 0) { this.friendMyData = userRes.data[0]; wx.getFriendCloudStorage({ keyList: ['buildingLevel'], success: function (res) { self.configFriendRank(res); }, fail: function (res) { console.error('error ' + res); } }); } }, fail: (res) => { console.error('user error ' + res); } }); } } }, loadGroupData(key1, key2) { this.groupPageIndex = 0; this.listItem.forEach(n => { n.active = false; }); this.meNode.active = false; let isRefresh = key2; //ranks数组大于0证明已经加载过有数据不用再加载 if (this.groupRanks && this.groupRanks.length > 0 && !isRefresh) { this._setupGroupList(); this._setupGroupMyData(); } else { if (window.wx != undefined) { let self = this; wx.getUserInfo({ openIdList: ['selfOpenId'], success: (userRes) => { if (userRes.data.length > 0) { this.groupMyData = userRes.data[0]; wx.getGroupCloudStorage({ shareTicket: key1, keyList: ['buildingLevel'], success: function (res) { self.configGroupRank(res); }, fail: function (res) { console.error('error ' + res); } }); } }, fail: (res) => { console.error('user error ' + res); } }); } } }, //好友排行榜 _setupFriendMyData() { this.meNode.active = true; this.meNode.getComponent('GameRankItem').init(this.friendMyData, true); }, configFriendRank(res) { let ranks = res.data; ranks.sort((a, b) => { if (a.KVDataList.length == 0 && b.KVDataList.length == 0) { return 0; } if (a.KVDataList.length == 0) { return 1; } if (b.KVDataList.length == 0) { return -1; } let b_score = JSON.parse(b.KVDataList[0].value).wxgame.score; let a_score = JSON.parse(a.KVDataList[0].value).wxgame.score; return b_score - a_score; }); for (let i = 0; i < ranks.length; i++) { if (this.friendMyData.avatarUrl == ranks[i].avatarUrl) { this.friendMyData.rank = (i + 1); if (ranks[i].KVDataList && ranks[i].KVDataList.length > 0) { let value = JSON.parse(ranks[i].KVDataList[0].value); this.friendMyData.score = value.wxgame.score; } else { this.friendMyData.score = 1; } } } this.friendRanks = ranks; this._setupFriendList(); this._setupFriendMyData(); }, _setupFriendList() { let start = this.friendPageIndex * 5; let end = start + 5; let sortArray = this.friendRanks.slice(start,end); this.listItem.forEach(n => { n.active = false; }); for(let i = 0; i < sortArray.length; i++) { let item = this.listItem[i]; let model = sortArray[i]; model.rank = this.friendPageIndex * 5 + (i + 1); item.active = true; item.getComponent('GameRankItem').init(model); } }, //群排行榜 _setupGroupMyData() { this.meNode.active = true; this.meNode.getComponent('GameRankItem').init(this.groupMyData, true); }, configGroupRank(res) { let ranks = res.data; ranks.sort((a, b) => { if (a.KVDataList.length == 0 && b.KVDataList.length == 0) { return 0; } if (a.KVDataList.length == 0) { return 1; } if (b.KVDataList.length == 0) { return -1; } let b_score = JSON.parse(b.KVDataList[0].value).wxgame.score; let a_score = JSON.parse(a.KVDataList[0].value).wxgame.score; return b_score - a_score; }); for (let i = 0; i < ranks.length; i++) { if (this.groupMyData.avatarUrl == ranks[i].avatarUrl) { this.groupMyData.rank = (i + 1); if (ranks[i].KVDataList && ranks[i].KVDataList.length > 0) { let value = JSON.parse(ranks[i].KVDataList[0].value); this.groupMyData.score = value.wxgame.score; } else { this.groupMyData.score = 1; } } } this.groupRanks = ranks; this._setupGroupList(); this._setupGroupMyData(); }, _setupGroupList() { let start = this.groupPageIndex * 5; let end = start + 5; let sortArray = this.groupRanks.slice(start,end); this.listItem.forEach(n => { n.active = false; }); for(let i = 0; i < sortArray.length; i++) { let item = this.listItem[i]; let model = sortArray[i]; model.rank = this.groupPageIndex * 5 + (i + 1); item.active = true; item.getComponent('GameRankItem').init(model); } }, previousPage() { if (this.isGroup) { if (this.groupPageIndex <= 0 || this.groupRanks == undefined || this.groupRanks.length == 0) { return; } this.groupPageIndex -= 1; this._setupGroupList(); } else { if (this.friendPageIndex <= 0 || this.friendRanks == undefined || this.friendRanks.length == 0) { return; } this.friendPageIndex -= 1; this._setupFriendList(); } }, nextPage() { if (this.isGroup) { if (this.groupPageIndex >= (Math.ceil(this.groupRanks.length / 5) - 1) || this.groupRanks == undefined || this.groupRanks.length == 0) { return; } this.groupPageIndex += 1; this._setupGroupList(); } else { if (this.friendPageIndex >= (Math.ceil(this.friendRanks.length / 5) - 1) || this.friendRanks == undefined || this.friendRanks.length == 0) { return; } this.friendPageIndex += 1; this._setupFriendList(); } } // update (dt) {}, });