UserImg.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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="imgError=true"/>
  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. }
  57. },
  58. computed: {
  59. textStyle() {
  60. const style = {
  61. backgroundColor: '#A0D7F1'
  62. };
  63. const {size} = this;
  64. if (size) {
  65. style.fontSize = /^\d+$/.test(size) ? (size + 'px') : size;
  66. }
  67. const {username} = this.info;
  68. if (username) {
  69. let bgColor = '';
  70. for (let i = 0; i < username.length; i++) {
  71. bgColor += parseInt(username[i].charCodeAt(0), 10).toString(16);
  72. }
  73. style.backgroundColor = '#' + bgColor.slice(1, 4);
  74. }
  75. return style;
  76. },
  77. userName() {
  78. if (!this.isJson(this.info)) {
  79. return '';
  80. }
  81. let name = this.info.send_username || this.info.username;
  82. if (this.info.nickname && !this.isEmojiPrefix(this.info.nickname)) {
  83. name = this.info.nickname;
  84. }
  85. return (name + " ").substring(0, 1).toLocaleUpperCase();
  86. },
  87. userImg() {
  88. if (!this.isJson(this.info)) {
  89. return '';
  90. }
  91. return this.info.send_userimg || this.info.userimg;
  92. },
  93. },
  94. methods: {
  95. isJson(obj) {
  96. return typeof (obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && typeof obj.length == "undefined";
  97. },
  98. isShowImg(url) {
  99. return !!(url && !$A.rightExists(url, 'images/other/avatar.png'));
  100. },
  101. isEmojiPrefix(text) {
  102. return /^[\uD800-\uDBFF][\uDC00-\uDFFF]/.test(text);
  103. },
  104. onClick(e) {
  105. this.$emit('click', e);
  106. }
  107. }
  108. }
  109. </script>