Tab.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. this.loadFriendData()
  47. .then((result) => { // 好友数据回调
  48. this.friends = result || [];
  49. return this.loadArtists();
  50. }).then((result) => { // 艺人数据回调
  51. if (result != undefined) {
  52. this.artists = result;
  53. this.noArtistTipNode.active = false;
  54. } else {
  55. this.noArtistTipNode.active = true;
  56. }
  57. this.layout();
  58. }).catch((err) => { // 捕获到报错
  59. console.log(err);
  60. });
  61. // 刷新艺人列表
  62. GameEvent.on(Notikey.RefreshFriendList, this, () => {
  63. this.loadFriendData()
  64. .then((result) => {
  65. this.friends = result || [];
  66. this.friendLayout();
  67. }).catch((err) => {
  68. console.log(err);
  69. });
  70. });
  71. GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {
  72. this.loadArtists()
  73. .then((result) => {
  74. if (result != undefined) {
  75. this.artists = result;
  76. this.noArtistTipNode.active = false;
  77. } else {
  78. this.artists = [];
  79. this.noArtistTipNode.active = true;
  80. }
  81. this.artistLayout();
  82. }).catch((err) => {
  83. console.log(err);
  84. });
  85. });
  86. this.changeTabEvent = _.debounce((eventData) => {
  87. this._currentTab = eventData;
  88. this.setBtnSpriteFrames();
  89. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  90. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  91. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  92. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  93. this.showAnimation(showBtn, hideScrollView);
  94. this.hideAnimation(hideBtn, showScrollView);
  95. }, 500, true);
  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. // 默认有全部好友和邀请两个节点
  150. let count = 1;
  151. count += this.friends.length;
  152. for (let i = 0; i < count; i++) {
  153. let item = cc.instantiate(this.friendItem);
  154. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  155. if (i === 0) {
  156. scriptComponent.style = Style.AllFriend;
  157. } else {
  158. scriptComponent.style = Style.Friend;
  159. scriptComponent.setFriendInfo(this.friends[i-1]);
  160. }
  161. this.friendScrollView.content.addChild(item);
  162. }
  163. },
  164. artistLayout() {
  165. for (let child of this.artistScrollView.content.children) {
  166. child.destroy();
  167. }
  168. let artistCount = 1;
  169. artistCount += this.artists.length;
  170. for (let i = 0; i < artistCount; i++) {
  171. let item = cc.instantiate(this.friendItem);
  172. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  173. if (i === 0) {
  174. scriptComponent.style = Style.Talent;
  175. } else {
  176. scriptComponent.style = Style.Artist;
  177. scriptComponent.setFriendInfo(this.artists[i-1]);
  178. }
  179. this.artistScrollView.content.addChild(item);
  180. }
  181. },
  182. });