UserImg.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div class="userimg-container" @click="onClick">
  3. <template v-if="this.isJson(info)">
  4. <img v-if="isShowImg(userImg)&&!imgError" class="userimg-container-img" :src="userImg" @error="loadError"/>
  5. <div v-else class="userimg-container-box" :style="textStyle">
  6. <div class="usertext-container-text">{{userName}}</div>
  7. </div>
  8. </template>
  9. </div>
  10. </template>
  11. <style lang="scss" scoped>
  12. .userimg-container {
  13. display: inline-block;
  14. max-width: 100%;
  15. max-height: 100%;
  16. position: relative;
  17. overflow: hidden;
  18. .userimg-container-img {
  19. width: 100%;
  20. height: 100%;
  21. display: table;
  22. }
  23. .userimg-container-box {
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. width: 100%;
  28. height: 100%;
  29. display: flex;
  30. align-items: center;
  31. justify-content: center;
  32. font-weight: 400;
  33. color: #ffffff;
  34. .usertext-container-text {
  35. flex: 1;
  36. display: flex;
  37. align-items: center;
  38. justify-content: center;
  39. }
  40. }
  41. }
  42. </style>
  43. <script>
  44. export default {
  45. name: 'UserImg',
  46. props: {
  47. info: {
  48. default: {} //{username, nickname, userimg}
  49. },
  50. size: {
  51. }
  52. },
  53. data() {
  54. return {
  55. imgError: false,
  56. againUrl: '',
  57. }
  58. },
  59. computed: {
  60. textStyle() {
  61. const style = {
  62. backgroundColor: '#A0D7F1'
  63. };
  64. const {size} = this;
  65. if (size) {
  66. style.fontSize = /^\d+$/.test(size) ? (size + 'px') : size;
  67. }
  68. const {username} = this.info;
  69. if (username) {
  70. let bgColor = '';
  71. for (let i = 0; i < username.length; i++) {
  72. bgColor += parseInt(username[i].charCodeAt(0), 10).toString(16);
  73. }
  74. style.backgroundColor = '#' + bgColor.slice(1, 4);
  75. }
  76. return style;
  77. },
  78. userName() {
  79. if (!this.isJson(this.info)) {
  80. return '';
  81. }
  82. let name = this.info.send_username || this.info.username;
  83. if (this.info.nickname && !this.isEmojiPrefix(this.info.nickname)) {
  84. name = this.info.nickname;
  85. }
  86. return (name + " ").substring(0, 1).toLocaleUpperCase();
  87. },
  88. userImg() {
  89. if (!this.isJson(this.info)) {
  90. return '';
  91. }
  92. if (this.againUrl) {
  93. return this.againUrl;
  94. }
  95. return this.info.send_userimg || this.info.userimg;
  96. },
  97. },
  98. methods: {
  99. isJson(obj) {
  100. return typeof (obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && typeof obj.length == "undefined";
  101. },
  102. isShowImg(url) {
  103. return !!(url && !$A.rightExists(url, 'images/other/avatar.png'));
  104. },
  105. isEmojiPrefix(text) {
  106. return /^[\uD800-\uDBFF][\uDC00-\uDFFF]/.test(text);
  107. },
  108. loadError() {
  109. if (!this.againUrl) {
  110. if ($A.rightExists(this.userImg, 'images/other/system-message.png')) {
  111. this.againUrl = $A.fillUrl('images/other/system-message.png');
  112. return;
  113. } else if ($A.rightExists(this.userImg, 'images/other/group.png')) {
  114. this.againUrl = $A.fillUrl('images/other/group.png');
  115. return;
  116. }
  117. }
  118. this.imgError = true
  119. },
  120. onClick(e) {
  121. this.$emit('click', e);
  122. }
  123. }
  124. }
  125. </script>