Tab.js 6.9 KB

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