WechatFriendListItem.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // 开放数据域 KVData 以key/value形式保存
  2. // 以下为本项目value格式,转为string之后才能提交
  3. // {
  4. //
  5. // "wxgame": {//wxgame 字段是微信要求保留的字段,这样才能在微信小游戏中心显示好友排行榜(其实目前看来这个功能不是十分重要)
  6. // "score":16, // 好友星星数量,即等级展示
  7. // "update_time": 1513080573 //数据提交的时间戳
  8. // },
  9. // "gender":0, //0代表女性,1代表男性
  10. // "jobLevelName":"国际巨星" //好友的艺人头衔
  11. // "jobLevel"
  12. // }
  13. cc.Class({
  14. extends: cc.Component,
  15. properties: {
  16. bgSprite: cc.Sprite,
  17. artistLevelSprite: cc.Sprite,
  18. headSprite: cc.Sprite,
  19. nickLabel: cc.Label,
  20. starCountLabel: cc.Label,
  21. genderIcon: cc.Sprite,
  22. _itemId: 0,
  23. titleLabel: cc.Label,
  24. artistLevelBgs: [cc.SpriteFrame],
  25. femaleFrame: cc.SpriteFrame,
  26. maleFrame: cc.SpriteFrame,
  27. },
  28. // LIFE-CYCLE CALLBACKS:
  29. onLoad() {
  30. // this.titleLabel.node.opacity = 0;
  31. },
  32. /**
  33. * gender [int]
  34. * head [string]
  35. * nick [string]
  36. * stars [int]
  37. * uid [int]
  38. * */
  39. updateItem(userInfo, itemId) {
  40. this._itemId = itemId;
  41. this.user = userInfo;
  42. //昵称
  43. this.nickLabel.string = userInfo.nickname;
  44. //头像
  45. this.createImageFromUrl(userInfo.avatarUrl, (spriteFrame) => {
  46. this.headSprite.spriteFrame = spriteFrame;
  47. }, () => {
  48. });
  49. if (userInfo.KVDataList && userInfo.KVDataList.length > 0) {
  50. let value = JSON.parse(userInfo.KVDataList[0].value);
  51. // 性别
  52. this.genderIcon.spriteFrame = value.gender == 1 ? this.maleFrame : this.femaleFrame;
  53. //艺人头衔
  54. if (value.jobLevelName) {
  55. this.titleLabel.node.active = true;
  56. this.titleLabel.string = value.jobLevelName;
  57. } else {
  58. this.titleLabel.node.active = false;
  59. }
  60. if (value.jobLevel) {
  61. this.artistLevelSprite.node.active = true;
  62. this.artistLevelSprite.spriteFrame = this.artistLevelBgs[value.jobLevel - 1];
  63. } else {
  64. this.artistLevelSprite.node.active = false;
  65. }
  66. //星星数量
  67. this.starCountLabel.string = value.wxgame.score;
  68. }
  69. },
  70. start() {
  71. },
  72. createImageFromUrl(avatarUrl, success, fail) {
  73. if (avatarUrl) {
  74. var index = avatarUrl.indexOf('https:');
  75. var httpIndex = avatarUrl.indexOf('http:');
  76. if (index === 0 || httpIndex === 0) {
  77. //以'https:'开头
  78. } else {
  79. avatarUrl = 'https:' + avatarUrl;
  80. }
  81. if (window.wx != undefined) {
  82. try {
  83. let image = wx.createImage();
  84. image.onload = () => {
  85. try {
  86. let texture = new cc.Texture2D();
  87. texture.initWithElement(image);
  88. texture.handleLoadedTexture();
  89. success(new cc.SpriteFrame(texture))
  90. } catch (e) {
  91. cc.log(e);
  92. fail();
  93. }
  94. };
  95. image.src = avatarUrl;
  96. } catch (e) {
  97. cc.log(e);
  98. fail();
  99. }
  100. } else {
  101. cc.loader.load({
  102. url: avatarUrl, type: 'jpg'
  103. }, (err, texture) => {
  104. if (err === undefined || err === null) {
  105. success(new cc.SpriteFrame(texture));
  106. } else {
  107. fail();
  108. }
  109. });
  110. }
  111. } else {
  112. cc.loader.loadRes('./default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  113. success(spriteFrame);
  114. })
  115. }
  116. }
  117. // update (dt) {},
  118. });