123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- const DWTool = require('./utils/DWTool');
- const Style = require('./utils/GameEnum').LevelHomeFriendItemStyle;
- const HomeApi = require('./net/HomeApi');
- const GameModule = require("./utils/GameModule");
- const Notikey = require('./utils/GameEnum').GameNotificationKey;
- cc.Class({
- extends: cc.Component,
- properties: {
- bgNode: cc.Node,
- bgProgressNode: cc.Node,
-
- friendSpriteFrames: [cc.SpriteFrame],
- artistSpriteFrames: [cc.SpriteFrame],
- friendBtn: cc.Button,
- artistBtn: cc.Button,
- friendItem: cc.Prefab,
- friendScrollView: cc.ScrollView,
- artistScrollView: cc.ScrollView,
- noArtistTipNode: cc.Node,
- _currentTab: 0
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
- GameModule.tab = this.node;
- this.friends = [];
- this.artists = [];
- this.setBtnSpriteFrames();
- this.bgNode.zIndex = 1;
- this.bgProgressNode.zIndex = 1;
- this.friendScrollView.node.zIndex = 1;
- this.artistScrollView.node.zIndex = 1;
- if (this._currentTab === 0) {
- this.friendBtn.node.setScale(1.0, 1.0);
- this.artistBtn.node.setScale(0.9, 0.9);
- this.friendBtn.node.zIndex = 2;
- this.artistBtn.node.zIndex = 0;
- this.friendScrollView.node.active = true;
- this.artistScrollView.node.active = false;
- this.artistScrollView.node.position = cc.v2(0, -250);
- } else {
- this.friendBtn.node.setScale(0.9, 0.9);
- this.artistBtn.node.setScale(1.0, 1.0);
- this.friendBtn.node.zIndex = 0;
- this.artistBtn.node.zIndex = 2;
- this.friendScrollView.node.active = false;
- this.artistScrollView.node.active = true;
- this.friendScrollView.node.position = cc.v2(0, -250);
- }
-
- this.loadFriendData()
- .then((result) => { // 好友数据回调
- if (result) {
- this.friends = result;
- }
- return this.loadArtists();
- }).then((result) => { // 艺人数据回调
- if (result != undefined) {
- this.artists = result;
- this.noArtistTipNode.active = false;
- } else {
- this.noArtistTipNode.active = true;
- }
- this.layout();
- }).catch((err) => { // 捕获到报错
- cc.error(err);
- });
-
- // 使用轮循的方式更新好友列表
- this.schedule(() => {
- this.loadFriendData()
- .then((result) => {
- if (result && result.length != this.friends.length) {
- this.friends = result;
- this.friendLayout();
- }
-
- }).catch((err) => {
- cc.error(err);
- });
- this.loadArtists()
- .then((result) => {
- if (result) {
- let hasChange = false;
- if (result != undefined) {
- if (result.length === this.artists.length) {
- for (let i = 0; i < result.length; i++) {
-
- let newItem = result[i];
- let oldItem = this.artists[i];
-
- if (newItem.role != oldItem.role) {
- hasChange = true;
- break;
- }
- }
- } else {
- hasChange = true;
- }
- this.noArtistTipNode.active = false;
-
- } else {
- if (this.artists.length !==0) {
- this.hasChange = true;
- }
- this.noArtistTipNode.active = true;
- }
- this.artists = result;
- if (hasChange) {
- this.layout();
- }
- }
- }).catch((err) => {
- cc.error(err);
- });
- }, 5.0);
- // 刷新艺人列表
- Global.GameEvent.on(Notikey.RefreshFriendList, this, () => {
- this.loadArtists()
- .then((result) => {
-
- if (result != undefined) {
- this.artists = result;
- this.noArtistTipNode.active = false;
- } else {
- this.artists = [];
- this.noArtistTipNode.active = true;
- }
-
- this.layout();
- }).catch((err) => {
- cc.error(err);
- });
- });
- this.changeTabEvent = _.debounce((eventData) => {
- this._currentTab = eventData;
- this.setBtnSpriteFrames();
-
- let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
- let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
- let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
- let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
- this.showAnimation(showBtn, hideScrollView);
- this.hideAnimation(hideBtn, showScrollView);
- }, 500, true);
- },
- setBtnSpriteFrames() {
- this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
- this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
- },
- showAnimation(btn, scrollView) {
- let tempBtn = btn;
- let tempScrollView = scrollView;
- tempBtn.node.zIndex = 2;
- let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
- let scrollCallback = cc.callFunc(() => {
- tempScrollView.node.active = false;
- });
- tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
-
- },
- hideAnimation(btn, scrollView) {
- let tempBtn = btn;
- let tempScrollView = scrollView;
- tempBtn.node.zIndex = 0;
- tempScrollView.node.active = true;
- let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -25));
- tempScrollView.node.runAction(scrollMoveAction);
- },
- changeTab(target, eventData) {
- if (this._currentTab == eventData) { return; }
-
- this.changeTabEvent(eventData);
- },
- loadFriendData() {
- return new Promise((resolve, reject) => {
- HomeApi.getFriends((responseData) => {
- resolve(responseData);
- }, (error) => {
- reject(error);
- });
- });
- },
- loadArtists() {
- return new Promise((resolve, reject) => {
- HomeApi.getFriendManageList( (responseData) => {
- resolve(responseData);
- }, (error) => {
- reject(error);
- })
- });
- },
- layout() {
-
- this.friendLayout();
- this.artistLayout();
- },
- friendLayout() {
- for (let child of this.friendScrollView.content.children) {
- child.destroy();
- }
-
- // 默认有全部好友和邀请两个节点
- let count = 1;
- count += this.friends.length;
- for (let i = 0; i < count; i++) {
- let item = cc.instantiate(this.friendItem);
- let scriptComponent = item.getComponent('LevelHomeFriendItem');
- if (i === 0) {
- scriptComponent.style = Style.AllFriend;
- } else {
- scriptComponent.style = Style.Friend;
- scriptComponent.setFriendInfo(this.friends[i-1]);
- }
- this.friendScrollView.content.addChild(item);
- }
- },
- artistLayout() {
- for (let child of this.artistScrollView.content.children) {
- child.destroy();
- }
- let artistCount = 1;
- artistCount += this.artists.length;
- for (let i = 0; i < artistCount; i++) {
- let item = cc.instantiate(this.friendItem);
-
- let scriptComponent = item.getComponent('LevelHomeFriendItem');
- if (i === 0) {
- scriptComponent.style = Style.Talent;
- } else {
- scriptComponent.style = Style.Artist;
- scriptComponent.setFriendInfo(this.artists[i-1]);
- }
- this.artistScrollView.content.addChild(item);
- }
- },
- });
|