WechatFriendListItem.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. levelText: cc.RichText,
  25. artistLevelBgs: [cc.SpriteFrame],
  26. femaleFrame: cc.SpriteFrame,
  27. maleFrame: cc.SpriteFrame,
  28. },
  29. // LIFE-CYCLE CALLBACKS:
  30. onLoad() {
  31. this.titleLabel.node.opacity = 0;
  32. },
  33. /**
  34. * gender [int]
  35. * head [string]
  36. * nick [string]
  37. * stars [int]
  38. * uid [int]
  39. * */
  40. updateItem(userInfo, itemId) {
  41. this._itemId = itemId;
  42. this.user = userInfo;
  43. //昵称
  44. this.nickLabel.string = userInfo.nickname;
  45. //头像
  46. this.createImageFromUrl(userInfo.avatarUrl, (spriteFrame) => {
  47. this.headSprite.spriteFrame = spriteFrame;
  48. }, () => {
  49. });
  50. if (userInfo.KVDataList && userInfo.KVDataList.length > 0) {
  51. let value = JSON.parse(userInfo.KVDataList[0].value);
  52. // 性别
  53. this.genderIcon.spriteFrame = value.gender == 1 ? this.maleFrame : this.femaleFrame;
  54. //艺人头衔
  55. this.titleLabel.string = value.jobLevelName;
  56. // this.levelText.string = '<outline color=#ffffff width=2>' + value.jobLevel + '</outline>';
  57. if (value.jobLevel) {
  58. this.artistLevelSprite.node.active = true;
  59. this.artistLevelSprite.spriteFrame = this.artistLevelBgs[userInfo.jobLevel - 1];
  60. } else {
  61. this.artistLevelSprite.node.active = false;
  62. }
  63. //星星数量
  64. this.starCountLabel.string = value.wxgame.score;
  65. }
  66. },
  67. start() {
  68. },
  69. createImageFromUrl(avatarUrl, success, fail) {
  70. var index = avatarUrl.indexOf('https:');
  71. var httpIndex = avatarUrl.indexOf('http:');
  72. if (index === 0 || httpIndex === 0) {
  73. //以'https:'开头
  74. } else {
  75. avatarUrl = 'https:' + avatarUrl;
  76. }
  77. if (window.wx != undefined) {
  78. try {
  79. let image = wx.createImage();
  80. image.onload = () => {
  81. try {
  82. let texture = new cc.Texture2D();
  83. texture.initWithElement(image);
  84. texture.handleLoadedTexture();
  85. success(new cc.SpriteFrame(texture))
  86. } catch (e) {
  87. cc.log(e);
  88. fail();
  89. }
  90. };
  91. image.src = avatarUrl;
  92. } catch (e) {
  93. cc.log(e);
  94. fail();
  95. }
  96. } else {
  97. cc.loader.load({
  98. url: avatarUrl, type: 'jpg'
  99. }, (err, texture) => {
  100. if (err === undefined || err === null) {
  101. success(new cc.SpriteFrame(texture));
  102. } else {
  103. fail();
  104. }
  105. });
  106. }
  107. }
  108. // update (dt) {},
  109. });