123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- // Learn cc.Class:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
- // Learn Attribute:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
- cc.Class({
- extends: cc.Component,
- properties: {
- rankScrollView: cc.Node,
- _viewitemTemp: null,
- _content: null,
- SignAtlas:{
- default:null,
- type:cc.SpriteAtlas,
- },
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- this._viewlist = this.node.getChildByName("PHB").getChildByName("rankScrollView");
- this._content = this.rankScrollView.getChildByName("view").getChildByName("content");
- if (this._viewitemTemp == null || this._viewitemTemp == 'undefined') {
- this._viewitemTemp = this._content.children[0];
- this._content.removeChild(this._viewitemTemp);
- }
- this.showFriendRank();
- },
- start() {
- this.userhead = "";
- var self = this;
- this.GetUserHead();
- wx.onMessage(function (data) {
- switch (data.method) {
- case 'updateRank': {
- break;
- }
- case 'showMyRank': {
- self.node.getChildByName("PHB").active = false;
- self.node.getChildByName("MYPHB").active = true;
-
- self.showFriendRank();
-
- break;
- }
- case 'showGroupRank': {
- break;
- }
- case 'hideRank': {
- self.node.getChildByName("PHB").active = false;
- self.node.getChildByName("MYPHB").active = false;
- break;
- }
- case 'showFriendRank': {
- var show = data.data == "true";
-
- self.node.getChildByName("PHB").active = show;
- self.node.getChildByName("MYPHB").active = !show;
-
- self.showFriendRank();
-
- break;
- }
- }
- })
- },
- packHeadImg(data) {
- var headlist = data.split('/');
- var len = headlist.length;
- if (len === 0 || len === 1) {
- return data;
- }
- var headimg = "";
- for (var i = 0; i < len - 1; ++i) {
- headimg += headlist[i] + '/';
- }
- headimg += '132';
- return headimg;
- },
- createImage(sprite, url) {
- let image = wx.createImage();
- image.onload = function () {
- let texture = new cc.Texture2D();
- texture.initWithElement(image);
- texture.handleLoadedTexture();
- sprite.spriteFrame = new cc.SpriteFrame(texture);
- };
- image.src = url;
- },
- // bubbleSort(arr) {
- // for (var i = 1; i < arr.length; i++) {
- // for (var r = 0; r < arr.length - i; r++) {
- // if (parseInt(arr[r].score) < parseInt(arr[r + 1].score)) {
- // arr[r] = [arr[r + 1], arr[r + 1] = arr[r]][0];
- // }
- // }
- // }
- // },
- bubbleSort(arr) {
- var len = arr.length;
- for (var i = 0; i < len; i++) {
- for (var j = 0; j < len - 1 - i; j++) {
- if (parseInt(arr[j].score) < parseInt(arr[j + 1].score)) { //相邻元素两两对比
- var temp = arr[j + 1]; //元素交换
- arr[j + 1] = arr[j];
- arr[j] = temp;
- }
- }
- }
- return arr;
- },
- onClickRankItem: function (event) {
- //var strIndex = event.target.getChildByName("ranknum").getComponent(cc.Label).string;
- //this.node.getChildByName("PHB").getChildByName("IndexPos").getComponent(cc.Label).string = "名次:" + strIndex;
- //this.node.getChildByName("PHB").getChildByName("PlayScore").getComponent(cc.Label).string = event.target.getChildByName("score").getComponent(cc.Label).string;
- //this.node.getChildByName("PHB").getChildByName("headimg").getComponent(cc.Sprite).spriteFrame = event.target.getChildByName("headimg").getComponent(cc.Sprite).spriteFrame;
- },
- addRank(data) {
- var rankArr = new Array();
- for (var index = 0; index < data.length; ++index) {
- var openid = data[index].openid;
- var nickname = data[index].nickname;
- var headimg = this.packHeadImg(data[index].avatarUrl);
- var score = '0';
- var driLevel = '0';
- if (!this.IsEmpty(data[index].KVDataList)) {
- for (var num = 0; num < data[index].KVDataList.length; ++num) {
- if (data[index].KVDataList[num].key == 'driLevel') {
- driLevel = data[index].KVDataList[num].value;
- } else {
- score = data[index].KVDataList[num].value;
- }
- }
- if (data[index].KVDataList.length == 2) {
- rankArr.push({ openid: openid, nickname: nickname, headimg: headimg, score: score, driLevel: driLevel });
- }
- }
- // if (!this.IsEmpty(data[index].KVDataList)) {
- // score = data[index].KVDataList[0].value;
- // rankArr.push({ openid: openid, nickname: nickname, headimg: headimg, score: score });
- // }
- }
- this.bubbleSort(rankArr);
- //this.node.getChildByName("PHB").active = true;
- this._content.removeAllChildren();
- //for (var n = 0; n < 5; ++n) {
- for (var index = 0; index < rankArr.length; ++index) {
- var node = cc.instantiate(this._viewitemTemp);
- node.getChildByName('ranknum').getComponent(cc.Label).string = index + 1;
- if (index < 3) {
- node.getChildByName('ranknunbg' + index).active = true;
- }
- node.getChildByName('username').getComponent(cc.Label).string = rankArr[index].nickname;
- this.createImage(node.getChildByName('headimg').getComponent(cc.Sprite), rankArr[index].headimg);
- node.getChildByName('score').getComponent(cc.Label).string = rankArr[index].score;
- node.getChildByName('CheckMark').active = ((index % 2) != 0);
- this._content.addChild(node);
- node.getChildByName("levelSgin").active = false;
- var level = parseInt(rankArr[index].driLevel);
- if (level != 'NAN' && level > 0) {
- node.getChildByName("levelSgin").active = true;
- node.getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
- }else{
- node.getChildByName("levelSgin").active = false;
- }
- // if (index == 0) {
- // node.getComponent(cc.Toggle).check();
- // var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
- // this.node.getChildByName("PHB").getChildByName("IndexPos").getComponent(cc.Label).string = "名次:" + strIndex;
- // this.node.getChildByName("PHB").getChildByName("PlayScore").getComponent(cc.Label).string = node.getChildByName("score").getComponent(cc.Label).string;
- // this.createImage(this.node.getChildByName("PHB").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index].headimg);
- // }
- if (this.isMySelf(rankArr[index].headimg)) {
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('ranknum').active = true;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('headimg').active = true;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('score').active = true;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('username').active = true;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('headSprite').active = true;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('ranknum').getComponent(cc.Label).string = index + 1;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('username').getComponent(cc.Label).string = rankArr[index].nickname;
- this.createImage(this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('headimg').getComponent(cc.Sprite), rankArr[index].headimg);
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('score').getComponent(cc.Label).string = rankArr[index].score;
- var level = parseInt(rankArr[index].driLevel);
- if (level != 'NAN' && level > 0) {
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName("levelSgin").active = true;
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
- }else{
- this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName("levelSgin").active = false;
- }
- if (rankArr.length > 3) {
- if (index == 0) {//第一
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), index, rankArr[index]);
-
- if (index+1 < rankArr.length) {
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), index+1, rankArr[index+1]);
- }
- if (index+2 < rankArr.length) {
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), index+2, rankArr[index+2]);
- }
- }else if (index == rankArr.length - 1) {
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), index-2, rankArr[index-2]);
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), index-1, rankArr[index-1]);
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), index, rankArr[index]);
- }
- else {
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), index-1, rankArr[index-1]);
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), index, rankArr[index]);
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), index+1, rankArr[index+1]);
- }
- }else {
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), 0, rankArr[0]);
- if (1 < rankArr.length)
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), 1, rankArr[1]);
- if (2 < rankArr.length)
- this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), 2, rankArr[2]);
- }
-
- /*
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("ranknum").getComponent(cc.Label).string = index + 1;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("username").getComponent(cc.Label).string = rankArr[index].nickname;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("score").getComponent(cc.Label).string = "#" + rankArr[index].score;
- this.createImage(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index].headimg);
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").active = true;
- level = parseInt(rankArr[index].driLevel);
- if (level != 'NAN' && level > 0) {
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("levelSgin").active = true;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
- }else{
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("levelSgin").active = false;
- }
- */
- /*
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").active = false;
- if (index > 0) {
- var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("ranknum").getComponent(cc.Label).string = index;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("username").getComponent(cc.Label).string = rankArr[index - 1].nickname;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("score").getComponent(cc.Label).string = "#" + rankArr[index - 1].score;
- this.createImage(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index - 1].headimg);
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").active = true;
- var level = parseInt(rankArr[index - 1].driLevel);
- if (level != 'NAN' && level > 0) {
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("levelSgin").active = true;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
- }
- else{
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("levelSgin").active = false;
- }
- }
- */
- /*
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").active = false;
- if (index < rankArr.length - 1) {
- var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("ranknum").getComponent(cc.Label).string = index + 2;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("username").getComponent(cc.Label).string = rankArr[index + 1].nickname;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("score").getComponent(cc.Label).string = "#" + rankArr[index + 1].score;
- this.createImage(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index + 1].headimg);
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").active = true;
- var level = parseInt(rankArr[index + 1].driLevel);
- if (level != 'NAN' && level > 0) {
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("levelSgin").active = true;
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
- }
- else{
- this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("levelSgin").active = false;
- }
- }*/
- }
- }
- //}
- rankArr = null;
- },
- showInfoNode:function (node, index, data) {
- //var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
- node.getChildByName("ranknum").getComponent(cc.Label).string = index+1;
- node.getChildByName("username").getComponent(cc.Label).string = data.nickname;
- node.getChildByName("score").getComponent(cc.Label).string = "#" + data.score;
- this.createImage(node.getChildByName("headimg").getComponent(cc.Sprite), data.headimg);
- node.active = true;
- node.getChildByName('ranknunbg0').active = false;
- node.getChildByName('ranknunbg1').active = false;
- node.getChildByName('ranknunbg2').active = false;
- if (index < 3) {
- node.getChildByName('ranknunbg' + index).active = true;
- }
- var level = parseInt(data.driLevel);
- if (level != 'NAN' && level > 0) {
- node.getChildByName("levelSgin").active = true;
- node.getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
- }
- else{
- node.getChildByName("levelSgin").active = false;
- }
- },
- showFriendRank() {
- var self = this;
- var scorekey = self.getCurWeekScoreValue();
- wx.getFriendCloudStorage({
- keyList: [scorekey, 'driLevel'],
- success: function (res) {
- // 转发成功
- self.addRank(res.data);
- // console.error(res);
- },
- fail: function (res) {
- // 转发失败
- // console.error(res);
- },
- complete: function (res) {
- // console.error(res);
- }
- })
- },
- isMySelf: function (strAvt) {
- return this.userhead == strAvt;
- },
- GetUserHead() {
- var self = this;
- wx.getUserInfo({
- openIdList: ['selfOpenId'],
- success: function (res) {
- self.userhead = self.packHeadImg(res.data[0].avatarUrl);
- }
- })
- },
- _hide() {
- // let moveTo = cc.moveTo(0.5, 0, 1000);
- // this.display.runAction(moveTo);
- },
- getCurWeekScoreValue() {
- return this.getNowFormatDate();
- },
- getNowFormatDate() {
- var date = new Date();
- var seperator1 = "-";
- var year = date.getFullYear();
- // var month = date.getMonth() + 1;
- // if (month >= 1 && month <= 9) {
- // month = "0" + month;
- // }
- var week = this.Getweek();
- var currentdate = year + seperator1 + week;
- return currentdate;
- },
- getMonthWeek() {
- var date = new Date(),
- w = date.getDay(),
- d = date.getDate();
- return Math.ceil(
- (d + 7 - w) / 7
- );
- },
- Getweek(dateString) {
- var da = '';
- if (dateString == undefined) {
- var now = new Date();
- var now_m = now.getMonth() + 1;
- now_m = (now_m < 10) ? '0' + now_m : now_m;
- var now_d = now.getDate();
- now_d = (now_d < 10) ? '0' + now_d : now_d;
- da = now.getFullYear() + '-' + now_m + '-' + now_d;
- } else {
- da = dateString; //日期格式2015-12-30
- }
- var date1 = new Date(da.substring(0, 4), parseInt(da.substring(5, 7)) - 1, da.substring(8, 10)); //当前日期
- var date2 = new Date(da.substring(0, 4), 0, 1); //1月1号
- //获取1月1号星期(以周一为第一天,0周一~6周日)
- var dateWeekNum = date2.getDay() - 1;
- if (dateWeekNum < 0) {
- dateWeekNum = 6;
- }
- if (dateWeekNum < 4) {
- //前移日期
- date2.setDate(date2.getDate() - dateWeekNum);
- } else {
- //后移日期
- date2.setDate(date2.getDate() + 7 - dateWeekNum);
- }
- var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
- var year = date1.getFullYear();
- var week = Math.ceil((d + 1) / 7);
- return week;
- },
- IsEmpty(obj) {//判断是否为空
- if (typeof obj == "undefined" || obj == null || obj == "") {
- return true;
- } else {
- return false;
- }
- },
- // update (dt) {},
- });
|