GameRankItem.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. headDefaultSpriteFrame: cc.SpriteFrame,
  5. headSprite: cc.Sprite,
  6. background: cc.Sprite,
  7. rankSprite: cc.Sprite,
  8. // rankRichText: cc.RichText,
  9. rankLabel: cc.Label,
  10. nameLabel: cc.Label,
  11. // lvRichText: cc.RichText,
  12. lvLabel: cc.Label,
  13. rankSpriteFrame: [cc.SpriteFrame],
  14. backgroundSpriteFrame: [cc.SpriteFrame],
  15. isMe: {
  16. get: function() {
  17. if (!this._isMe) {
  18. this._isMe = false;
  19. }
  20. return this._isMe;
  21. },
  22. set: function(value) {
  23. this._isMe = value;
  24. if (this._isMe) {
  25. this.background.spriteFrame = this.backgroundSpriteFrame[1];
  26. } else {
  27. this.background.spriteFrame = this.backgroundSpriteFrame[0];
  28. }
  29. }
  30. },
  31. },
  32. // LIFE-CYCLE CALLBACKS:
  33. // onLoad () {},
  34. init (model, isMe = false) {
  35. this.configData(model);
  36. this.isMe = isMe;
  37. },
  38. start () {
  39. },
  40. configData(model) {
  41. this.headSprite.spriteFrame = this.headDefaultSpriteFrame;
  42. if (model.avatarUrl) {
  43. this.createImageFromUrl(model.avatarUrl, (spriteFrame) => {
  44. this.headSprite.spriteFrame = spriteFrame;
  45. });
  46. }
  47. this.nameLabel.string = model.nickname == undefined ? model.nickName : model.nickname;
  48. if (model.rank < 4) {
  49. this.rankSprite.node.active = true;
  50. this.rankLabel.node.active = false;
  51. this.rankSprite.spriteFrame = this.rankSpriteFrame[(model.rank - 1)];
  52. } else {
  53. this.rankSprite.node.active = false;
  54. this.rankLabel.node.active = true;
  55. let rank = (model.rank > 100) ? "100+" : model.rank;
  56. this.rankLabel.string = `${rank}`;
  57. }
  58. if (model.score != undefined) {
  59. this.lvLabel.string = `LV.${model.score}`;
  60. } else {
  61. if (model.KVDataList && model.KVDataList.length > 0) {
  62. let value = JSON.parse(model.KVDataList[0].value);
  63. this.lvLabel.string = `LV.${value.wxgame.score}`;
  64. }
  65. }
  66. },
  67. createImageFromUrl(avatarUrl, success, fail) {
  68. if (avatarUrl) {
  69. var index = avatarUrl.indexOf('https:');
  70. var httpIndex = avatarUrl.indexOf('http:');
  71. if (index === 0 || httpIndex === 0) {
  72. //以'https:'开头
  73. } else {
  74. avatarUrl = 'https:' + avatarUrl;
  75. }
  76. if (window.wx != undefined) {
  77. try {
  78. let image = wx.createImage();
  79. image.onload = () => {
  80. try {
  81. let texture = new cc.Texture2D();
  82. texture.initWithElement(image);
  83. texture.handleLoadedTexture();
  84. success(new cc.SpriteFrame(texture))
  85. } catch (e) {
  86. cc.log(e);
  87. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  88. success(spriteFrame);
  89. })
  90. }
  91. };
  92. image.src = avatarUrl;
  93. } catch (e) {
  94. cc.log(e);
  95. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  96. success(spriteFrame);
  97. })
  98. }
  99. } else {
  100. cc.loader.load({
  101. url: avatarUrl, type: 'jpg'
  102. }, (err, texture) => {
  103. if (err === undefined || err === null) {
  104. success(new cc.SpriteFrame(texture));
  105. } else {
  106. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  107. success(spriteFrame);
  108. })
  109. }
  110. });
  111. }
  112. } else {
  113. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  114. success(spriteFrame);
  115. })
  116. }
  117. }
  118. // update (dt) {},
  119. });