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. // 刷新艺人列表
  72. GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {
  73. this.loadArtists()
  74. .then((result) => {
  75. if (result != undefined) {
  76. this.artists = result;
  77. this.noArtistTipNode.active = false;
  78. } else {
  79. this.artists = [];
  80. this.noArtistTipNode.active = true;
  81. }
  82. this.artistLayout();
  83. }).catch((err) => {
  84. console.log(err);
  85. });
  86. });
  87. this.changeTabEvent = _.debounce((eventData) => {
  88. this._currentTab = eventData;
  89. this.setBtnSpriteFrames();
  90. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  91. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  92. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  93. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  94. this.showAnimation(showBtn, hideScrollView);
  95. this.hideAnimation(hideBtn, showScrollView);
  96. }, 500, true);
  97. },
  98. setBtnSpriteFrames() {
  99. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
  100. this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
  101. },
  102. showAnimation(btn, scrollView) {
  103. let tempBtn = btn;
  104. let tempScrollView = scrollView;
  105. tempBtn.node.zIndex = 2;
  106. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
  107. let scrollCallback = cc.callFunc(() => {
  108. tempScrollView.node.active = false;
  109. });
  110. tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
  111. },
  112. hideAnimation(btn, scrollView) {
  113. let tempBtn = btn;
  114. let tempScrollView = scrollView;
  115. tempBtn.node.zIndex = 0;
  116. tempScrollView.node.active = true;
  117. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -10));
  118. tempScrollView.node.runAction(scrollMoveAction);
  119. },
  120. changeTab(target, eventData) {
  121. if (this._currentTab == eventData) { return; }
  122. this.changeTabEvent(eventData);
  123. },
  124. loadFriendData() {
  125. return new Promise((resolve, reject) => {
  126. HomeApi.getFriends((responseData) => {
  127. resolve(responseData);
  128. }, (error) => {
  129. reject(error);
  130. });
  131. });
  132. },
  133. loadArtists() {
  134. return new Promise((resolve, reject) => {
  135. HomeApi.getFriendManageList( (responseData) => {
  136. resolve(responseData);
  137. }, (error) => {
  138. reject(error);
  139. })
  140. });
  141. },
  142. layout() {
  143. this.friendLayout();
  144. this.artistLayout();
  145. },
  146. friendLayout() {
  147. for (let child of this.friendScrollView.content.children) {
  148. child.destroy();
  149. }
  150. // 默认有全部好友和邀请两个节点
  151. let count = 1;
  152. count += this.friends.length;
  153. for (let i = 0; i < count; i++) {
  154. let item = cc.instantiate(this.friendItem);
  155. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  156. if (i === 0) {
  157. scriptComponent.style = Style.AllFriend;
  158. } else {
  159. scriptComponent.style = Style.Friend;
  160. scriptComponent.setFriendInfo(this.friends[i-1]);
  161. }
  162. this.friendScrollView.content.addChild(item);
  163. }
  164. },
  165. artistLayout() {
  166. for (let child of this.artistScrollView.content.children) {
  167. child.destroy();
  168. }
  169. let artistCount = 1;
  170. artistCount += this.artists.length;
  171. for (let i = 0; i < artistCount; i++) {
  172. let item = cc.instantiate(this.friendItem);
  173. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  174. if (i === 0) {
  175. scriptComponent.style = Style.Talent;
  176. } else {
  177. scriptComponent.style = Style.Artist;
  178. scriptComponent.setFriendInfo(this.artists[i-1]);
  179. }
  180. this.artistScrollView.content.addChild(item);
  181. }
  182. },
  183. });