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. // debugger;
  49. if (model.rank < 4) {
  50. this.rankSprite.node.active = true;
  51. this.rankLabel.node.active = false;
  52. this.rankSprite.spriteFrame = this.rankSpriteFrame[(model.rank - 1)];
  53. } else {
  54. this.rankSprite.node.active = false;
  55. this.rankLabel.node.active = true;
  56. let rank = (model.rank > 100) ? "100+" : model.rank;
  57. this.rankLabel.string = `${rank}`;
  58. }
  59. if (model.score != undefined) {
  60. this.lvLabel.string = `LV.${model.score}`;
  61. } else {
  62. if (model.KVDataList && model.KVDataList.length > 0) {
  63. let value = JSON.parse(model.KVDataList[0].value);
  64. this.lvLabel.string = `LV.${value.wxgame.score}`;
  65. }
  66. }
  67. },
  68. createImageFromUrl(avatarUrl, success, fail) {
  69. if (avatarUrl) {
  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. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  89. success(spriteFrame);
  90. })
  91. }
  92. };
  93. image.src = avatarUrl;
  94. } catch (e) {
  95. cc.log(e);
  96. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  97. success(spriteFrame);
  98. })
  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. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  108. success(spriteFrame);
  109. })
  110. }
  111. });
  112. }
  113. } else {
  114. cc.loader.loadRes('./common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  115. success(spriteFrame);
  116. })
  117. }
  118. }
  119. // update (dt) {},
  120. });