123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- const ListViewAdapter = require('./ListViewAdapter');
- cc.Class({
- extends: cc.Component,
- properties: {
- parent: cc.Node,
- scrollView: cc.ScrollView,
- content: cc.Node,
- prefabRankItem: cc.Prefab,
- // prefabGameOverRank: cc.Prefab,
- // gameOverRankLayout: cc.Node,
- current: 0,
- users: [],
- spacing: 0,
- loadingLabel: cc.Label,
- },
- onLoad() {
- console.log('onLoad------------------------------------------------------------');
- this.listAdapter = new ListViewAdapter(this.scrollView);
- this.parent.height = cc.view.getVisibleSize().height;
- this.scrollView.node.y = (this.scrollView.node.height - this.parent.height) / 2 + 170;
- // console.log('height: ', this.parent.height);
- // console.log('scrollView y:', this.scrollView.node.height);
- },
- start() {
- this.removeChild();
- if (window.wx != undefined) {
- window.wx.onMessage(data => {
- console.log("接收主域发来消息:", data)
- if (data.messageType == 0) {//移除排行榜
- this.removeChild();
- } else if (data.messageType == 1) {//获取好友排行榜
- cc.game.resume();
- this.fetchFriendData(data.key1);
- } else if (data.messageType == 3) {//提交得分
- this.submitScore(data.key1, data.stars);
- } else if (data.messageType == 4) {//获取好友排行榜横向排列展示模式
- this.gameOverRank(data.key1);
- } else if (data.messageType == 5) {//获取群排行榜
- this.fetchGroupFriendData(data.key1, data.shareTicket);
- } else if (data.messageType == 6) {
- this.previousPage();
- } else if (data.messageType == 7) {
- this.nextPage();
- } else if (data.messageType == 8) {
- cc.game.pause();
- } else if (data.messageType == 9) {
- cc.game.resume();
- }
- });
- } else {
- this.fetchFriendData(1000);
- }
- },
- update(dt) {
- // console.log('子域 update');
- },
- // 开放数据域 KVData 以key/value形式保存
- // 以下为本项目value格式,转为string之后才能提交
- // {
- //
- // "wxgame": {//wxgame 字段是微信要求保留的字段,这样才能在微信小游戏中心显示好友排行榜(其实目前看来这个功能不是十分重要)
- // "score":16, // 好友星星数量,即等级展示
- // "update_time": 1513080573 //数据提交的时间戳
- // },
- // "gender":0, //0代表女性,1代表男性
- // "jobName":"国际巨星" //好友的艺人头衔
- // }
- submitScore(key1, score, gender, jobName) { //提交得分
- if (window.wx != undefined) {
- window.wx.getUserCloudStorage({
- // 以key/value形式存储
- keyList: [key1],
- success: function (getres) {
- if (getres.KVDataList.length != 0) {
- let preScore = JSON.parse(getres.KVDataList[0].value).wxgame.score;
- if (preScore > score) {
- return;
- }
- }
- // 对用户托管数据进行写数据操作
- let myValue = {
- wxgame: {
- score: score,
- update_time: Date.parse(new Date()) / 1000
- },
- gender: gender,
- jobName: jobName
- }
- let valueString = JSON.stringify(myValue);
- window.wx.setUserCloudStorage({
- KVDataList: [{ key: key1, value: valueString }],
- success: function (res) {
- console.log('setUserCloudStorage', 'success', res)
- },
- fail: function (res) {
- console.log('setUserCloudStorage', 'fail')
- },
- complete: function (res) {
- }
- });
- },
- fail: function (res) {
- console.log('getUserCloudStorage', 'fail')
- },
- complete: function (res) {
- }
- });
- } else {
- cc.log("提交得分:" + key1 + " : " + score)
- }
- },
- removeChild() {
- this.content.removeAllChildren();
- },
- fetchFriendData(key1) {
- this.loadingLabel.node.active = true;
- this.loadingLabel.node.string = '加载中...';
- this.current = 0;
- if (window.wx != undefined) {
- wx.getUserInfo({
- openIdList: ['selfOpenId'],
- success: (userRes) => {
- console.log('success', userRes.data)
- let myData = userRes.data[0];
- //取出所有好友数据
- wx.getFriendCloudStorage({
- keyList: [key1],
- success: res => {
- console.log("wx.getFriendCloudStorage success", res);
- let data = res.data;
- data.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 < data.length; i++) {
- if (data[i].avatarUrl == myData.avatarUrl) {
- data.splice(i, 1);
- }
- }
- this.users = data;
- this.listAdapter.updateItems(this.users, this.prefabRankItem, 'WechatFriendListItem');
- this.loadingLabel.node.active = false;
- },
- fail: res => {
- console.log("wx.getFriendCloudStorage fail", res);
- this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
- },
- });
- },
- fail: (res) => {
- this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
- }
- });
- }
- },
- previousPage() {
- if (this.current > 0) {
- let start = this.current - 5;
- this.current = start;
- this.notifyData();
- } else {
- // wx.showToast({
- // title:'已经是第一页了~'
- // })
- }
- },
- nextPage() {
- let start = this.current + 5;
- if (start >= this.users.length) {
- if (this.hasMore) {
- this.getTotalRank();
- } else {
- // wx.showToast({
- // title:'没有下一页了~'
- // })
- }
- } else {
- this.current = start;
- this.notifyData();
- }
- },
- fetchGroupFriendData(key1, shareTicket) {
- this.removeChild();
- if (window.wx != undefined) {
- wx.getUserInfo({
- openIdList: ['selfOpenId'],
- success: (userRes) => {
- console.log('success', userRes.data)
- let userData = userRes.data[0];
- //取出所有好友数据
- wx.getGroupCloudStorage({
- shareTicket: shareTicket,
- keyList: [key1],
- success: res => {
- console.log("wx.getGroupCloudStorage success", res);
- this.loadingLabel.active = false;
- let data = res.data;
- data.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;
- }
- return b.KVDataList[0].value - a.KVDataList[0].value;
- });
- for (let i = 0; i < data.length; i++) {
- var playerInfo = data[i];
- var item = cc.instantiate(this.prefabRankItem);
- item.getComponent('RankItem').init(i, playerInfo);
- item.getComponent('RankItem').isSelf = false;
- this.content.addChild(item);
- if (data[i].avatarUrl == userData.avatarUrl) {
- let userItem = cc.instantiate(this.prefabRankItem);
- userItem.getComponent('RankItem').isSelf = true;
- userItem.getComponent('RankItem').init(i, playerInfo);
- userItem.y = -354;
- this.node.addChild(userItem, 1, 1000);
- }
- }
- },
- fail: res => {
- console.log("wx.getFriendCloudStorage fail", res);
- this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
- },
- });
- },
- fail: (res) => {
- this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
- }
- });
- }
- },
- gameOverRank(key1) {
- this.removeChild();
- this.gameOverRankLayout.active = true;
- if (window.wx != undefined) {
- wx.getUserInfo({
- openIdList: ['selfOpenId'],
- success: (userRes) => {
- cc.log('success', userRes.data)
- let userData = userRes.data[0];
- //取出所有好友数据
- wx.getFriendCloudStorage({
- keyList: [key1],
- success: res => {
- cc.log("wx.getFriendCloudStorage success", res);
- this.loadingLabel.active = false;
- let data = res.data;
- data.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;
- }
- return b.KVDataList[0].value - a.KVDataList[0].value;
- });
- for (let i = 0; i < data.length; i++) {
- if (data[i].avatarUrl == userData.avatarUrl) {
- if ((i - 1) >= 0) {
- if ((i + 1) >= data.length && (i - 2) >= 0) {
- let userItem = cc.instantiate(this.prefabGameOverRank);
- userItem.getComponent('GameOverRank').init(i - 2, data[i - 2]);
- this.gameOverRankLayout.addChild(userItem);
- }
- let userItem = cc.instantiate(this.prefabGameOverRank);
- userItem.getComponent('GameOverRank').init(i - 1, data[i - 1]);
- this.gameOverRankLayout.addChild(userItem);
- } else {
- if ((i + 2) >= data.length) {
- let node = new cc.Node();
- node.width = 200;
- this.gameOverRankLayout.addChild(node);
- }
- }
- let userItem = cc.instantiate(this.prefabGameOverRank);
- userItem.getComponent('GameOverRank').init(i, data[i], true);
- this.gameOverRankLayout.addChild(userItem);
- if ((i + 1) < data.length) {
- let userItem = cc.instantiate(this.prefabGameOverRank);
- userItem.getComponent('GameOverRank').init(i + 1, data[i + 1]);
- this.gameOverRankLayout.addChild(userItem);
- if ((i - 1) < 0 && (i + 2) < data.length) {
- let userItem = cc.instantiate(this.prefabGameOverRank);
- userItem.getComponent('GameOverRank').init(i + 2, data[i + 2]);
- this.gameOverRankLayout.addChild(userItem);
- }
- }
- }
- }
- },
- fail: res => {
- console.log("wx.getFriendCloudStorage fail", res);
- this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
- },
- });
- },
- fail: (res) => {
- this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
- }
- });
- }
- },
- });
|