1
0

WechatFriendListItem.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // "jobName":"国际巨星" //好友的艺人头衔
  11. // ""
  12. // }
  13. cc.Class({
  14. extends: cc.Component,
  15. properties: {
  16. bgSprite: cc.Sprite,
  17. headSprite: cc.Sprite,
  18. nickLabel: cc.Label,
  19. starCountLabel: cc.Label,
  20. genderIcon: cc.Sprite,
  21. _itemId: 0,
  22. titleLabel: cc.Label,
  23. levelText: cc.RichText,
  24. },
  25. // LIFE-CYCLE CALLBACKS:
  26. onLoad() {
  27. this.titleLabel.node.opacity = 0;
  28. },
  29. /**
  30. * gender [int]
  31. * head [string]
  32. * nick [string]
  33. * stars [int]
  34. * uid [int]
  35. * */
  36. updateItem(userInfo, itemId) {
  37. this._itemId = itemId;
  38. this.user = userInfo;
  39. //昵称
  40. this.nickLabel.string = userInfo.nickname;
  41. //头像
  42. this.createImageFromUrl(userInfo.avatarUrl, (spriteFrame) => {
  43. this.headSprite.spriteFrame = spriteFrame;
  44. }, () => {
  45. });
  46. if (userInfo.KVDataList && userInfo.KVDataList.length > 0) {
  47. let value = JSON.parse(userInfo.KVDataList[0].value);
  48. // 性别
  49. let genderPath = value.gender == 0 ? './icon_female' : './icon_male';
  50. cc.loader.loadRes(genderPath, cc.SpriteFrame, (err, spriteFrame) => {
  51. this.genderIcon.spriteFrame = spriteFrame;
  52. });
  53. //艺人头衔
  54. this.titleLabel.string = value.jobName;
  55. this.levelText.string = '<outline color=#ffffff width=2>' + value.jobLevel + '</outline>';
  56. //星星数量
  57. this.starCountLabel.string = value.wxgame.score;
  58. }
  59. },
  60. start() {
  61. },
  62. createImageFromUrl(avatarUrl, success, fail) {
  63. var index = avatarUrl.indexOf('https:');
  64. var httpIndex = avatarUrl.indexOf('http:');
  65. if (index === 0 || httpIndex === 0) {
  66. //以'https:'开头
  67. } else {
  68. avatarUrl = 'https:' + avatarUrl;
  69. }
  70. if (window.wx != undefined) {
  71. try {
  72. let image = wx.createImage();
  73. image.onload = () => {
  74. try {
  75. let texture = new cc.Texture2D();
  76. texture.initWithElement(image);
  77. texture.handleLoadedTexture();
  78. success(new cc.SpriteFrame(texture))
  79. } catch (e) {
  80. cc.log(e);
  81. fail();
  82. }
  83. };
  84. image.src = avatarUrl;
  85. } catch (e) {
  86. cc.log(e);
  87. fail();
  88. }
  89. } else {
  90. cc.loader.load({
  91. url: avatarUrl, type: 'jpg'
  92. }, (err, texture) => {
  93. if (err === undefined || err === null) {
  94. success(new cc.SpriteFrame(texture));
  95. } else {
  96. fail();
  97. }
  98. });
  99. }
  100. }
  101. // update (dt) {},
  102. });