WechatFriendList.js 14 KB

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