123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- 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) {},
- });
|