Tab.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. bgProgressNode: cc.Node,
  11. friendSpriteFrames: [cc.SpriteFrame],
  12. artistSpriteFrames: [cc.SpriteFrame],
  13. friendBtn: cc.Button,
  14. artistBtn: cc.Button,
  15. friendItem: cc.Prefab,
  16. friendScrollView: cc.ScrollView,
  17. artistScrollView: cc.ScrollView,
  18. noArtistTipNode: cc.Node,
  19. _currentTab: 0
  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.bgProgressNode.zIndex = 1;
  29. this.friendScrollView.node.zIndex = 1;
  30. this.artistScrollView.node.zIndex = 1;
  31. if (this._currentTab === 0) {
  32. this.friendBtn.node.setScale(1.0, 1.0);
  33. this.artistBtn.node.setScale(0.9, 0.9);
  34. this.friendBtn.node.zIndex = 2;
  35. this.artistBtn.node.zIndex = 0;
  36. this.friendScrollView.node.active = true;
  37. this.artistScrollView.node.active = false;
  38. this.artistScrollView.node.position = cc.v2(0, -250);
  39. } else {
  40. this.friendBtn.node.setScale(0.9, 0.9);
  41. this.artistBtn.node.setScale(1.0, 1.0);
  42. this.friendBtn.node.zIndex = 0;
  43. this.artistBtn.node.zIndex = 2;
  44. this.friendScrollView.node.active = false;
  45. this.artistScrollView.node.active = true;
  46. this.friendScrollView.node.position = cc.v2(0, -250);
  47. }
  48. this.loadFriendData()
  49. .then((result) => { // 好友数据回调
  50. if (result) {
  51. this.friends = result;
  52. }
  53. return this.loadArtists();
  54. }).then((result) => { // 艺人数据回调
  55. if (result != undefined) {
  56. this.artists = result;
  57. this.noArtistTipNode.active = false;
  58. } else {
  59. this.noArtistTipNode.active = true;
  60. }
  61. this.layout();
  62. }).catch((err) => { // 捕获到报错
  63. cc.error(err);
  64. });
  65. // 使用轮循的方式更新好友列表
  66. this.schedule(() => {
  67. this.loadFriendData()
  68. .then((result) => {
  69. if (result && result.length != this.friends.length) {
  70. this.friends = result;
  71. this.friendLayout();
  72. }
  73. }).catch((err) => {
  74. cc.error(err);
  75. });
  76. this.loadArtists()
  77. .then((result) => {
  78. if (result) {
  79. let hasChange = false;
  80. if (result != undefined) {
  81. if (result.length === this.artists.length) {
  82. for (let i = 0; i < result.length; i++) {
  83. let newItem = result[i];
  84. let oldItem = this.artists[i];
  85. if (newItem.role != oldItem.role) {
  86. hasChange = true;
  87. break;
  88. }
  89. }
  90. } else {
  91. hasChange = true;
  92. }
  93. this.noArtistTipNode.active = false;
  94. } else {
  95. if (this.artists.length !==0) {
  96. this.hasChange = true;
  97. }
  98. this.noArtistTipNode.active = true;
  99. }
  100. this.artists = result;
  101. if (hasChange) {
  102. this.layout();
  103. }
  104. }
  105. }).catch((err) => {
  106. cc.error(err);
  107. });
  108. }, 5.0);
  109. // 刷新艺人列表
  110. Global.GameEvent.on(Notikey.RefreshFriendList, this, () => {
  111. this.loadArtists()
  112. .then((result) => {
  113. if (result != undefined) {
  114. this.artists = result;
  115. this.noArtistTipNode.active = false;
  116. } else {
  117. this.artists = [];
  118. this.noArtistTipNode.active = true;
  119. }
  120. this.layout();
  121. }).catch((err) => {
  122. cc.error(err);
  123. });
  124. });
  125. this.changeTabEvent = _.debounce((eventData) => {
  126. this._currentTab = eventData;
  127. this.setBtnSpriteFrames();
  128. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  129. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  130. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  131. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  132. this.showAnimation(showBtn, hideScrollView);
  133. this.hideAnimation(hideBtn, showScrollView);
  134. }, 500, true);
  135. },
  136. setBtnSpriteFrames() {
  137. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
  138. this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
  139. },
  140. showAnimation(btn, scrollView) {
  141. let tempBtn = btn;
  142. let tempScrollView = scrollView;
  143. tempBtn.node.zIndex = 2;
  144. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
  145. let scrollCallback = cc.callFunc(() => {
  146. tempScrollView.node.active = false;
  147. });
  148. tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
  149. },
  150. hideAnimation(btn, scrollView) {
  151. let tempBtn = btn;
  152. let tempScrollView = scrollView;
  153. tempBtn.node.zIndex = 0;
  154. tempScrollView.node.active = true;
  155. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -25));
  156. tempScrollView.node.runAction(scrollMoveAction);
  157. },
  158. changeTab(target, eventData) {
  159. if (this._currentTab == eventData) { return; }
  160. this.changeTabEvent(eventData);
  161. },
  162. loadFriendData() {
  163. return new Promise((resolve, reject) => {
  164. HomeApi.getFriends((responseData) => {
  165. resolve(responseData);
  166. }, (error) => {
  167. reject(error);
  168. });
  169. });
  170. },
  171. loadArtists() {
  172. return new Promise((resolve, reject) => {
  173. HomeApi.getFriendManageList( (responseData) => {
  174. resolve(responseData);
  175. }, (error) => {
  176. reject(error);
  177. })
  178. });
  179. },
  180. layout() {
  181. this.friendLayout();
  182. this.artistLayout();
  183. },
  184. friendLayout() {
  185. for (let child of this.friendScrollView.content.children) {
  186. child.destroy();
  187. }
  188. // 默认有全部好友和邀请两个节点
  189. let count = 1;
  190. count += this.friends.length;
  191. for (let i = 0; i < count; i++) {
  192. let item = cc.instantiate(this.friendItem);
  193. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  194. if (i === 0) {
  195. scriptComponent.style = Style.AllFriend;
  196. } else {
  197. scriptComponent.style = Style.Friend;
  198. scriptComponent.setFriendInfo(this.friends[i-1]);
  199. }
  200. this.friendScrollView.content.addChild(item);
  201. }
  202. },
  203. artistLayout() {
  204. for (let child of this.artistScrollView.content.children) {
  205. child.destroy();
  206. }
  207. let artistCount = 1;
  208. artistCount += this.artists.length;
  209. for (let i = 0; i < artistCount; i++) {
  210. let item = cc.instantiate(this.friendItem);
  211. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  212. if (i === 0) {
  213. scriptComponent.style = Style.Talent;
  214. } else {
  215. scriptComponent.style = Style.Artist;
  216. scriptComponent.setFriendInfo(this.artists[i-1]);
  217. }
  218. this.artistScrollView.content.addChild(item);
  219. }
  220. },
  221. });