head-image.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="head-image" @click="showUserInfo($event)" :title="name">
  3. <image class="avatar-image" v-if="url" :src="url" :style="avatarImageStyle" lazy-load="true"
  4. mode="aspectFill" />
  5. <view class="avatar-text" v-if="!url" :style="avatarTextStyle">
  6. {{ name?.substring(0, 1).toUpperCase() }}
  7. </view>
  8. <view v-if="online" class="online" title="用户当前在线">
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: "head-image",
  15. data() {
  16. return {
  17. colors: ["#5daa31", "#c7515a", "#e03697", "#85029b",
  18. "#c9b455", "#326eb6"]
  19. }
  20. },
  21. props: {
  22. id: {
  23. type: Number
  24. },
  25. size: {
  26. type: [Number, String],
  27. default: 'default'
  28. },
  29. url: {
  30. type: String
  31. },
  32. name: {
  33. type: String,
  34. default: null
  35. },
  36. online: {
  37. type: Boolean,
  38. default: false
  39. },
  40. },
  41. methods: {
  42. showUserInfo(e) {
  43. if (this.id && this.id > 0) {
  44. uni.navigateTo({
  45. url: "/pages/common/user-info?id=" + this.id
  46. })
  47. }
  48. }
  49. },
  50. computed: {
  51. _size() {
  52. if (typeof this.size === 'number') {
  53. return this.size;
  54. } else if (typeof this.size === 'string') {
  55. return {
  56. 'default': 96,
  57. 'small': 84,
  58. 'smaller': 72,
  59. 'mini': 60,
  60. 'minier': 48,
  61. 'lage': 108,
  62. 'lager': 120,
  63. }[this.size]
  64. }
  65. },
  66. avatarImageStyle() {
  67. return `width:${this._size}rpx;
  68. height:${this._size}rpx;`
  69. },
  70. avatarTextStyle() {
  71. return `width: ${this._size}rpx;
  72. height:${this._size}rpx;
  73. background-color:${this.name ? this.textColor : '#fff'};
  74. font-size:${this._size * 0.5}rpx;
  75. `
  76. },
  77. textColor() {
  78. let hash = 0;
  79. for (var i = 0; i < this.name.length; i++) {
  80. hash += this.name.charCodeAt(i);
  81. }
  82. return this.colors[hash % this.colors.length];
  83. }
  84. }
  85. }
  86. </script>
  87. <style scoped lang="scss">
  88. .head-image {
  89. position: relative;
  90. cursor: pointer;
  91. .avatar-image {
  92. position: relative;
  93. overflow: hidden;
  94. border-radius: 50%;
  95. vertical-align: bottom;
  96. }
  97. .avatar-text {
  98. color: white;
  99. border-radius: 50%;
  100. display: flex;
  101. align-items: center;
  102. justify-content: center;
  103. }
  104. .online {
  105. position: absolute;
  106. right: -10%;
  107. bottom: 0;
  108. width: 24rpx;
  109. height: 24rpx;
  110. background: limegreen;
  111. border-radius: 50%;
  112. border: 6rpx solid white;
  113. }
  114. }
  115. </style>