UserAvatar.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <view class="user-avatar">
  3. <image class="user-avatar-content" v-if="avatarLink && showImg" :src="avatarLink" @load="imageLoad" @error="imageLoadError" />
  4. <view class="user-avatar-content" :style="{ background: getAvatarGradient(userName) }" v-else>{{ getUserIndexChat() }}</view>
  5. </view>
  6. </template>
  7. <script>
  8. import { getAvatarGradient } from '@/utils/handler'
  9. export default {
  10. props: {
  11. avatarLink: {
  12. type: String,
  13. default: ''
  14. },
  15. userName: {
  16. type: String,
  17. default: ''
  18. }
  19. },
  20. data () {
  21. return {
  22. showImg: true
  23. }
  24. },
  25. methods: {
  26. getAvatarGradient,
  27. getUserIndexChat () {
  28. return this.userName ? this.userName.split('')[0] : '用'
  29. },
  30. imageLoad () {
  31. this.showImg = true
  32. },
  33. imageLoadError () {
  34. this.showImg = false
  35. }
  36. }
  37. }
  38. </script>
  39. <style lang="scss" scoped>
  40. .user-avatar {
  41. width: 100%;
  42. height: 100%;
  43. &-content {
  44. width: 100%;
  45. height: 100%;
  46. display: flex;
  47. align-items: center;
  48. justify-content: center;
  49. color: #fff;
  50. font-weight: 500;
  51. }
  52. }
  53. </style>