WechatFriendList.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. const ListViewAdapter = require('./ListViewAdpater');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. parent: cc.Node,
  6. scrollView: cc.ScrollView,
  7. content: cc.Node,
  8. prefabRankItem: cc.Prefab,
  9. // prefabGameOverRank: cc.Prefab,
  10. // gameOverRankLayout: cc.Node,
  11. current: 0,
  12. users: [],
  13. spacing: 0,
  14. loadingLabel: cc.Label,
  15. },
  16. onLoad() {
  17. console.log('onLoad------------------------------------------------------------');
  18. this.listAdapter = new ListViewAdapter(this.scrollView);
  19. this.parent.height = cc.view.getVisibleSize().height;
  20. this.scrollView.node.y = (this.scrollView.node.height - this.parent.height) / 2 + 170;
  21. // console.log('height: ', this.parent.height);
  22. // console.log('scrollView y:', this.scrollView.node.height);
  23. },
  24. start() {
  25. this.removeChild();
  26. if (window.wx != undefined) {
  27. window.wx.onMessage(data => {
  28. console.log("接收主域发来消息:", data)
  29. if (data.messageType == 0) {//移除排行榜
  30. this.removeChild();
  31. } else if (data.messageType == 1) {//获取好友排行榜
  32. cc.game.resume();
  33. this.fetchFriendData(data.key1);
  34. } else if (data.messageType == 3) {//提交得分
  35. this.submitScore(data.key1, data.stars);
  36. } else if (data.messageType == 4) {//获取好友排行榜横向排列展示模式
  37. this.gameOverRank(data.key1);
  38. } else if (data.messageType == 5) {//获取群排行榜
  39. this.fetchGroupFriendData(data.key1, data.shareTicket);
  40. } else if (data.messageType == 6) {
  41. this.previousPage();
  42. } else if (data.messageType == 7) {
  43. this.nextPage();
  44. } else if (data.messageType == 8) {
  45. cc.game.pause();
  46. } else if (data.messageType == 9) {
  47. cc.game.resume();
  48. }
  49. });
  50. } else {
  51. this.fetchFriendData(1000);
  52. }
  53. },
  54. update(dt) {
  55. console.log('子域 update');
  56. },
  57. // 开放数据域 KVData 以key/value形式保存
  58. // 以下为本项目value格式,转为string之后才能提交
  59. // {
  60. //
  61. // "wxgame": {//wxgame 字段是微信要求保留的字段,这样才能在微信小游戏中心显示好友排行榜(其实目前看来这个功能不是十分重要)
  62. // "score":16, // 好友星星数量,即等级展示
  63. // "update_time": 1513080573 //数据提交的时间戳
  64. // },
  65. // "gender":0, //0代表女性,1代表男性
  66. // "jobName":"国际巨星" //好友的艺人头衔
  67. // }
  68. submitScore(key1, score, gender, jobName) { //提交得分
  69. if (window.wx != undefined) {
  70. window.wx.getUserCloudStorage({
  71. // 以key/value形式存储
  72. keyList: [key1],
  73. success: function (getres) {
  74. if (getres.KVDataList.length != 0) {
  75. let preScore = JSON.parse(getres.KVDataList[0].value).wxgame.score;
  76. if (preScore > score) {
  77. return;
  78. }
  79. }
  80. // 对用户托管数据进行写数据操作
  81. let myValue = {
  82. wxgame: {
  83. score: score,
  84. update_time: Date.parse(new Date()) / 1000
  85. },
  86. gender: gender,
  87. jobName: jobName
  88. }
  89. let valueString = JSON.stringify(myValue);
  90. window.wx.setUserCloudStorage({
  91. KVDataList: [{ key: key1, value: valueString }],
  92. success: function (res) {
  93. console.log('setUserCloudStorage', 'success', res)
  94. },
  95. fail: function (res) {
  96. console.log('setUserCloudStorage', 'fail')
  97. },
  98. complete: function (res) {
  99. }
  100. });
  101. },
  102. fail: function (res) {
  103. console.log('getUserCloudStorage', 'fail')
  104. },
  105. complete: function (res) {
  106. }
  107. });
  108. } else {
  109. cc.log("提交得分:" + key1 + " : " + score)
  110. }
  111. },
  112. removeChild() {
  113. this.content.removeAllChildren();
  114. },
  115. fetchFriendData(key1) {
  116. this.loadingLabel.node.active = true;
  117. this.loadingLabel.node.string = '加载中...';
  118. this.current = 0;
  119. if (window.wx != undefined) {
  120. wx.getUserInfo({
  121. openIdList: ['selfOpenId'],
  122. success: (userRes) => {
  123. console.log('success', userRes.data)
  124. let userData = userRes.data[0];
  125. //取出所有好友数据
  126. wx.getFriendCloudStorage({
  127. keyList: [key1],
  128. success: res => {
  129. console.log("wx.getFriendCloudStorage success", res);
  130. let data = res.data;
  131. data.sort((a, b) => {
  132. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  133. return 0;
  134. }
  135. if (a.KVDataList.length == 0) {
  136. return 1;
  137. }
  138. if (b.KVDataList.length == 0) {
  139. return -1;
  140. }
  141. let b_score = JSON.parse(b.KVDataList[0].value).wxgame.score;
  142. let a_score = JSON.parse(a.KVDataList[0].value).wxgame.score;
  143. return b_score - a_score;
  144. });
  145. this.users = data;
  146. this.listAdapter.updateItems(this.users, this.prefabRankItem, 'WechatFriendListItem');
  147. this.loadingLabel.node.active = false;
  148. },
  149. fail: res => {
  150. console.log("wx.getFriendCloudStorage fail", res);
  151. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  152. },
  153. });
  154. },
  155. fail: (res) => {
  156. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  157. }
  158. });
  159. }
  160. },
  161. previousPage() {
  162. if (this.current > 0) {
  163. let start = this.current - 5;
  164. this.current = start;
  165. this.notifyData();
  166. } else {
  167. // wx.showToast({
  168. // title:'已经是第一页了~'
  169. // })
  170. }
  171. },
  172. nextPage() {
  173. let start = this.current + 5;
  174. if (start >= this.users.length) {
  175. if (this.hasMore) {
  176. this.getTotalRank();
  177. } else {
  178. // wx.showToast({
  179. // title:'没有下一页了~'
  180. // })
  181. }
  182. } else {
  183. this.current = start;
  184. this.notifyData();
  185. }
  186. },
  187. fetchGroupFriendData(key1, shareTicket) {
  188. this.removeChild();
  189. if (window.wx != undefined) {
  190. wx.getUserInfo({
  191. openIdList: ['selfOpenId'],
  192. success: (userRes) => {
  193. console.log('success', userRes.data)
  194. let userData = userRes.data[0];
  195. //取出所有好友数据
  196. wx.getGroupCloudStorage({
  197. shareTicket: shareTicket,
  198. keyList: [key1],
  199. success: res => {
  200. console.log("wx.getGroupCloudStorage success", res);
  201. this.loadingLabel.active = false;
  202. let data = res.data;
  203. data.sort((a, b) => {
  204. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  205. return 0;
  206. }
  207. if (a.KVDataList.length == 0) {
  208. return 1;
  209. }
  210. if (b.KVDataList.length == 0) {
  211. return -1;
  212. }
  213. return b.KVDataList[0].value - a.KVDataList[0].value;
  214. });
  215. for (let i = 0; i < data.length; i++) {
  216. var playerInfo = data[i];
  217. var item = cc.instantiate(this.prefabRankItem);
  218. item.getComponent('RankItem').init(i, playerInfo);
  219. item.getComponent('RankItem').isSelf = false;
  220. this.content.addChild(item);
  221. if (data[i].avatarUrl == userData.avatarUrl) {
  222. let userItem = cc.instantiate(this.prefabRankItem);
  223. userItem.getComponent('RankItem').isSelf = true;
  224. userItem.getComponent('RankItem').init(i, playerInfo);
  225. userItem.y = -354;
  226. this.node.addChild(userItem, 1, 1000);
  227. }
  228. }
  229. },
  230. fail: res => {
  231. console.log("wx.getFriendCloudStorage fail", res);
  232. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  233. },
  234. });
  235. },
  236. fail: (res) => {
  237. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  238. }
  239. });
  240. }
  241. },
  242. gameOverRank(key1) {
  243. this.removeChild();
  244. this.gameOverRankLayout.active = true;
  245. if (window.wx != undefined) {
  246. wx.getUserInfo({
  247. openIdList: ['selfOpenId'],
  248. success: (userRes) => {
  249. cc.log('success', userRes.data)
  250. let userData = userRes.data[0];
  251. //取出所有好友数据
  252. wx.getFriendCloudStorage({
  253. keyList: [key1],
  254. success: res => {
  255. cc.log("wx.getFriendCloudStorage success", res);
  256. this.loadingLabel.active = false;
  257. let data = res.data;
  258. data.sort((a, b) => {
  259. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  260. return 0;
  261. }
  262. if (a.KVDataList.length == 0) {
  263. return 1;
  264. }
  265. if (b.KVDataList.length == 0) {
  266. return -1;
  267. }
  268. return b.KVDataList[0].value - a.KVDataList[0].value;
  269. });
  270. for (let i = 0; i < data.length; i++) {
  271. if (data[i].avatarUrl == userData.avatarUrl) {
  272. if ((i - 1) >= 0) {
  273. if ((i + 1) >= data.length && (i - 2) >= 0) {
  274. let userItem = cc.instantiate(this.prefabGameOverRank);
  275. userItem.getComponent('GameOverRank').init(i - 2, data[i - 2]);
  276. this.gameOverRankLayout.addChild(userItem);
  277. }
  278. let userItem = cc.instantiate(this.prefabGameOverRank);
  279. userItem.getComponent('GameOverRank').init(i - 1, data[i - 1]);
  280. this.gameOverRankLayout.addChild(userItem);
  281. } else {
  282. if ((i + 2) >= data.length) {
  283. let node = new cc.Node();
  284. node.width = 200;
  285. this.gameOverRankLayout.addChild(node);
  286. }
  287. }
  288. let userItem = cc.instantiate(this.prefabGameOverRank);
  289. userItem.getComponent('GameOverRank').init(i, data[i], true);
  290. this.gameOverRankLayout.addChild(userItem);
  291. if ((i + 1) < data.length) {
  292. let userItem = cc.instantiate(this.prefabGameOverRank);
  293. userItem.getComponent('GameOverRank').init(i + 1, data[i + 1]);
  294. this.gameOverRankLayout.addChild(userItem);
  295. if ((i - 1) < 0 && (i + 2) < data.length) {
  296. let userItem = cc.instantiate(this.prefabGameOverRank);
  297. userItem.getComponent('GameOverRank').init(i + 2, data[i + 2]);
  298. this.gameOverRankLayout.addChild(userItem);
  299. }
  300. }
  301. }
  302. }
  303. },
  304. fail: res => {
  305. console.log("wx.getFriendCloudStorage fail", res);
  306. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  307. },
  308. });
  309. },
  310. fail: (res) => {
  311. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  312. }
  313. });
  314. }
  315. },
  316. });