WechatFriendList.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. const ListViewAdapter = require('./ListViewAdapter');
  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 myData = 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. for (let i = 0; i < data.length; i++) {
  146. if (data[i].avatarUrl == myData.avatarUrl) {
  147. data.splice(i, 1);
  148. }
  149. }
  150. this.users = data;
  151. this.listAdapter.updateItems(this.users, this.prefabRankItem, 'WechatFriendListItem');
  152. this.loadingLabel.node.active = false;
  153. },
  154. fail: res => {
  155. console.log("wx.getFriendCloudStorage fail", res);
  156. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  157. },
  158. });
  159. },
  160. fail: (res) => {
  161. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  162. }
  163. });
  164. }
  165. },
  166. previousPage() {
  167. if (this.current > 0) {
  168. let start = this.current - 5;
  169. this.current = start;
  170. this.notifyData();
  171. } else {
  172. // wx.showToast({
  173. // title:'已经是第一页了~'
  174. // })
  175. }
  176. },
  177. nextPage() {
  178. let start = this.current + 5;
  179. if (start >= this.users.length) {
  180. if (this.hasMore) {
  181. this.getTotalRank();
  182. } else {
  183. // wx.showToast({
  184. // title:'没有下一页了~'
  185. // })
  186. }
  187. } else {
  188. this.current = start;
  189. this.notifyData();
  190. }
  191. },
  192. fetchGroupFriendData(key1, shareTicket) {
  193. this.removeChild();
  194. if (window.wx != undefined) {
  195. wx.getUserInfo({
  196. openIdList: ['selfOpenId'],
  197. success: (userRes) => {
  198. console.log('success', userRes.data)
  199. let userData = userRes.data[0];
  200. //取出所有好友数据
  201. wx.getGroupCloudStorage({
  202. shareTicket: shareTicket,
  203. keyList: [key1],
  204. success: res => {
  205. console.log("wx.getGroupCloudStorage success", res);
  206. this.loadingLabel.active = false;
  207. let data = res.data;
  208. data.sort((a, b) => {
  209. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  210. return 0;
  211. }
  212. if (a.KVDataList.length == 0) {
  213. return 1;
  214. }
  215. if (b.KVDataList.length == 0) {
  216. return -1;
  217. }
  218. return b.KVDataList[0].value - a.KVDataList[0].value;
  219. });
  220. for (let i = 0; i < data.length; i++) {
  221. var playerInfo = data[i];
  222. var item = cc.instantiate(this.prefabRankItem);
  223. item.getComponent('RankItem').init(i, playerInfo);
  224. item.getComponent('RankItem').isSelf = false;
  225. this.content.addChild(item);
  226. if (data[i].avatarUrl == userData.avatarUrl) {
  227. let userItem = cc.instantiate(this.prefabRankItem);
  228. userItem.getComponent('RankItem').isSelf = true;
  229. userItem.getComponent('RankItem').init(i, playerInfo);
  230. userItem.y = -354;
  231. this.node.addChild(userItem, 1, 1000);
  232. }
  233. }
  234. },
  235. fail: res => {
  236. console.log("wx.getFriendCloudStorage fail", res);
  237. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  238. },
  239. });
  240. },
  241. fail: (res) => {
  242. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  243. }
  244. });
  245. }
  246. },
  247. gameOverRank(key1) {
  248. this.removeChild();
  249. this.gameOverRankLayout.active = true;
  250. if (window.wx != undefined) {
  251. wx.getUserInfo({
  252. openIdList: ['selfOpenId'],
  253. success: (userRes) => {
  254. cc.log('success', userRes.data)
  255. let userData = userRes.data[0];
  256. //取出所有好友数据
  257. wx.getFriendCloudStorage({
  258. keyList: [key1],
  259. success: res => {
  260. cc.log("wx.getFriendCloudStorage success", res);
  261. this.loadingLabel.active = false;
  262. let data = res.data;
  263. data.sort((a, b) => {
  264. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  265. return 0;
  266. }
  267. if (a.KVDataList.length == 0) {
  268. return 1;
  269. }
  270. if (b.KVDataList.length == 0) {
  271. return -1;
  272. }
  273. return b.KVDataList[0].value - a.KVDataList[0].value;
  274. });
  275. for (let i = 0; i < data.length; i++) {
  276. if (data[i].avatarUrl == userData.avatarUrl) {
  277. if ((i - 1) >= 0) {
  278. if ((i + 1) >= data.length && (i - 2) >= 0) {
  279. let userItem = cc.instantiate(this.prefabGameOverRank);
  280. userItem.getComponent('GameOverRank').init(i - 2, data[i - 2]);
  281. this.gameOverRankLayout.addChild(userItem);
  282. }
  283. let userItem = cc.instantiate(this.prefabGameOverRank);
  284. userItem.getComponent('GameOverRank').init(i - 1, data[i - 1]);
  285. this.gameOverRankLayout.addChild(userItem);
  286. } else {
  287. if ((i + 2) >= data.length) {
  288. let node = new cc.Node();
  289. node.width = 200;
  290. this.gameOverRankLayout.addChild(node);
  291. }
  292. }
  293. let userItem = cc.instantiate(this.prefabGameOverRank);
  294. userItem.getComponent('GameOverRank').init(i, data[i], true);
  295. this.gameOverRankLayout.addChild(userItem);
  296. if ((i + 1) < data.length) {
  297. let userItem = cc.instantiate(this.prefabGameOverRank);
  298. userItem.getComponent('GameOverRank').init(i + 1, data[i + 1]);
  299. this.gameOverRankLayout.addChild(userItem);
  300. if ((i - 1) < 0 && (i + 2) < data.length) {
  301. let userItem = cc.instantiate(this.prefabGameOverRank);
  302. userItem.getComponent('GameOverRank').init(i + 2, data[i + 2]);
  303. this.gameOverRankLayout.addChild(userItem);
  304. }
  305. }
  306. }
  307. }
  308. },
  309. fail: res => {
  310. console.log("wx.getFriendCloudStorage fail", res);
  311. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  312. },
  313. });
  314. },
  315. fail: (res) => {
  316. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  317. }
  318. });
  319. }
  320. },
  321. });