avatar.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <div class="avatar" :class="shape === 'circle' ? 'shape-circle' : ''">
  3. <img :src="avatarSrc">
  4. </div>
  5. </template>
  6. <script>
  7. import systemAvatar from '@/assets/image/system.png'
  8. import { getOpenIM } from '@/utils/openIM';
  9. export default {
  10. data:{
  11. return:{
  12. OpenIM: null,
  13. }
  14. },
  15. props: {
  16. src: String,
  17. type: {
  18. type: String,
  19. default: '1'
  20. },
  21. shape: {
  22. type: String,
  23. default: 'circle'
  24. }
  25. },
  26. async created() {
  27. try {
  28. this.OpenIM = getOpenIM()
  29. } catch (e) {
  30. console.error('OpenIM 获取失败', e)
  31. }
  32. },
  33. computed: {
  34. avatarSrc: function () {
  35. let src = this.src
  36. if (/^(https:|http:|\/\/)/.test(src)) {
  37. return src
  38. } else {
  39. return this.defaultSrc
  40. }
  41. },
  42. defaultSrc: function () {
  43. switch(this.type) {
  44. case 1:
  45. // 个人头像
  46. return 'https://imgcache.qq.com/open/qcloud/video/act/webim-avatar/avatar-2.png'
  47. case 3:
  48. // 群默认头像
  49. return 'https://imgcache.qq.com/open/qcloud/video/act/webim-avatar/avatar-3.png'
  50. case 4:
  51. return systemAvatar
  52. default:
  53. // 默认头像
  54. return 'https://imgcache.qq.com/open/qcloud/video/act/webim-avatar/avatar-1.png'
  55. }
  56. }
  57. }
  58. }
  59. </script>
  60. <style lang="stylus" scoped>
  61. .avatar
  62. background-color $first
  63. text-align center
  64. width 100%
  65. height 100%
  66. overflow hidden
  67. img
  68. width 100%
  69. height 100%
  70. .shape-circle
  71. border-radius 50%
  72. </style>