head-image.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="head-image none-pointer-events" @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: linear-gradient(145deg,#ffffff20 25%,#00000060),${this.textColor};
  74. font-size:${this._size * 0.45}rpx;
  75. border-radius: ${this.radius};
  76. `
  77. },
  78. textColor() {
  79. if(!this.name){
  80. return '#fff';
  81. }
  82. let hash = 0;
  83. for (var i = 0; i < this.name.length; i++) {
  84. hash += this.name.charCodeAt(i);
  85. }
  86. return this.colors[hash % this.colors.length];
  87. }
  88. }
  89. }
  90. </script>
  91. <style scoped lang="scss">
  92. .head-image {
  93. position: relative;
  94. cursor: pointer;
  95. .avatar-image {
  96. position: relative;
  97. overflow: hidden;
  98. border-radius: 50%;
  99. vertical-align: bottom;
  100. }
  101. .avatar-text {
  102. color: white;
  103. border-radius: 50%;
  104. display: flex;
  105. align-items: center;
  106. justify-content: center;
  107. }
  108. .online {
  109. position: absolute;
  110. right: -10%;
  111. bottom: 0;
  112. width: 24rpx;
  113. height: 24rpx;
  114. background: limegreen;
  115. border-radius: 50%;
  116. border: 6rpx solid white;
  117. }
  118. }
  119. </style>