Tab.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const DWTool = require('./utils/DWTool');
  2. const Style = require('./utils/GameEnum').LevelHomeFriendItemStyle;
  3. const HomeApi = require('./net/HomeApi');
  4. const GameModule = require("./utils/GameModule");
  5. const Notikey = require('./utils/GameEnum').GameNotificationKey;
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. bgNode: cc.Node,
  10. friendSpriteFrames: [cc.SpriteFrame],
  11. artistSpriteFrames: [cc.SpriteFrame],
  12. friendBtn: cc.Button,
  13. artistBtn: cc.Button,
  14. friendItem: cc.Prefab,
  15. friendScrollView: cc.ScrollView,
  16. artistScrollView: cc.ScrollView,
  17. noArtistTipNode: cc.Node,
  18. _currentTab: 0 ,
  19. allFriendNode: cc.Node
  20. },
  21. // LIFE-CYCLE CALLBACKS:
  22. onLoad () {
  23. GameModule.tab = this.node;
  24. this.friends = [];
  25. this.artists = [];
  26. this.setBtnSpriteFrames();
  27. this.bgNode.zIndex = 1;
  28. this.friendScrollView.node.zIndex = 1;
  29. // this.artistScrollView.node.zIndex = 1;
  30. if (this._currentTab === 0) {
  31. // this.friendBtn.node.setScale(1.0, 1.0);
  32. // this.artistBtn.node.setScale(0.9, 0.9);
  33. // this.friendBtn.node.zIndex = 2;
  34. // this.artistBtn.node.zIndex = 0;
  35. this.friendScrollView.node.active = true;
  36. // this.artistScrollView.node.active = false;
  37. // this.artistScrollView.node.position = cc.v2(0, -250);
  38. } else {
  39. // this.friendBtn.node.setScale(0.9, 0.9);
  40. // this.artistBtn.node.setScale(1.0, 1.0);
  41. // this.friendBtn.node.zIndex = 0;
  42. // this.artistBtn.node.zIndex = 2;
  43. // this.friendScrollView.node.active = false;
  44. // this.artistScrollView.node.active = true;
  45. // this.friendScrollView.node.position = cc.v2(0, -250);
  46. }
  47. this.allFriendNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  48. GameEvent.fire(Notikey.ShowFriendSystem);
  49. GameModule.audioMng.playButton();
  50. }, 1000, true), this);
  51. // 刷新好友列表
  52. GameEvent.on(Notikey.RefreshFriendList, this, () => {
  53. this.loadFriendData().then((data) => {
  54. Global.friendList = this.friends = data.users || [];
  55. this.friendLayout();
  56. }).catch((err) => {
  57. console.log(err);
  58. });
  59. });
  60. // 刷新艺人列表
  61. GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {
  62. this.loadArtists()
  63. .then((result) => {
  64. if (result != undefined) {
  65. this.artists = result;
  66. this.noArtistTipNode.active = false;
  67. } else {
  68. this.artists = [];
  69. this.noArtistTipNode.active = true;
  70. }
  71. this.artistLayout();
  72. }).catch((err) => {
  73. console.log(err);
  74. });
  75. });
  76. this.changeTabEvent = _.debounce((eventData) => {
  77. this._currentTab = eventData;
  78. this.setBtnSpriteFrames();
  79. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  80. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  81. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  82. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  83. this.showAnimation(showBtn, hideScrollView);
  84. this.hideAnimation(hideBtn, showScrollView);
  85. }, 500, true);
  86. },
  87. init(game) {
  88. this.game = game
  89. this.loadFriendData().then((data) => { // 好友数据回调
  90. Global.friendList = this.friends = data.users || [];
  91. this.noArtistTipNode.active = false;
  92. this.friendLayout();
  93. }).catch((err) => { // 捕获到报错
  94. console.log(err);
  95. });
  96. },
  97. setBtnSpriteFrames() {
  98. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
  99. this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
  100. },
  101. showAnimation(btn, scrollView) {
  102. let tempBtn = btn;
  103. let tempScrollView = scrollView;
  104. tempBtn.node.zIndex = 2;
  105. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
  106. let scrollCallback = cc.callFunc(() => {
  107. tempScrollView.node.active = false;
  108. });
  109. tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
  110. },
  111. hideAnimation(btn, scrollView) {
  112. let tempBtn = btn;
  113. let tempScrollView = scrollView;
  114. tempBtn.node.zIndex = 0;
  115. tempScrollView.node.active = true;
  116. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -10));
  117. tempScrollView.node.runAction(scrollMoveAction);
  118. },
  119. changeTab(target, eventData) {
  120. if (this._currentTab == eventData) { return; }
  121. this.changeTabEvent(eventData);
  122. },
  123. loadFriendData() {
  124. return new Promise((resolve, reject) => {
  125. HomeApi.getFriends((responseData) => {
  126. resolve(responseData);
  127. }, (error) => {
  128. reject(error);
  129. });
  130. });
  131. },
  132. loadArtists() {
  133. return new Promise((resolve, reject) => {
  134. HomeApi.getFriendManageList((responseData) => {
  135. resolve(responseData);
  136. }, (error) => {
  137. reject(error);
  138. })
  139. });
  140. },
  141. layout() {
  142. this.friendLayout();
  143. // this.artistLayout();
  144. },
  145. friendLayout() {
  146. for (let child of this.friendScrollView.content.children) {
  147. child.destroy();
  148. }
  149. for (let i = 0; i < this.friends.length; i++) {
  150. let item = cc.instantiate(this.friendItem);
  151. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  152. scriptComponent.style = Style.Friend;
  153. scriptComponent.setFriendInfo(this.friends[i]);
  154. this.friendScrollView.content.addChild(item);
  155. }
  156. },
  157. artistLayout() {
  158. for (let child of this.artistScrollView.content.children) {
  159. child.destroy();
  160. }
  161. let artistCount = 1;
  162. artistCount += this.artists.length;
  163. for (let i = 0; i < artistCount; i++) {
  164. let item = cc.instantiate(this.friendItem);
  165. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  166. if (i === 0) {
  167. scriptComponent.style = Style.Talent;
  168. } else {
  169. scriptComponent.style = Style.Artist;
  170. scriptComponent.setFriendInfo(this.artists[i-1]);
  171. }
  172. this.artistScrollView.content.addChild(item);
  173. }
  174. },
  175. });