head-image.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view class="head-image" @click="showUserInfo($event)" :title="name">
  3. <image class="avatar-image" v-if="url" :src="url"
  4. :style="avatarImageStyle" lazy-load="true" 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,
  27. default: 20
  28. },
  29. url: {
  30. type: String
  31. },
  32. name: {
  33. type: String,
  34. default: "?"
  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. avatarImageStyle() {
  52. return `width:${this.size}rpx;
  53. height:${this.size}rpx;`
  54. },
  55. avatarTextStyle() {
  56. return `width: ${this.size}rpx;
  57. height:${this.size}rpx;
  58. background-color:${this.textColor};
  59. font-size:${this.size*0.5}rpx;
  60. `
  61. },
  62. textColor() {
  63. let hash = 0;
  64. for (var i = 0; i < this.name.length; i++) {
  65. hash += this.name.charCodeAt(i);
  66. }
  67. return this.colors[hash % this.colors.length];
  68. }
  69. }
  70. }
  71. </script>
  72. <style scoped lang="scss">
  73. .head-image {
  74. position: relative;
  75. cursor: pointer;
  76. .avatar-image {
  77. position: relative;
  78. overflow: hidden;
  79. border-radius: 50%;
  80. vertical-align: bottom;
  81. }
  82. .avatar-text {
  83. color: white;
  84. border-radius: 50%;
  85. display: flex;
  86. align-items: center;
  87. justify-content: center;
  88. }
  89. .online {
  90. position: absolute;
  91. right: -10%;
  92. bottom: 0;
  93. width: 24rpx;
  94. height: 24rpx;
  95. background: limegreen;
  96. border-radius: 50%;
  97. border: 6rpx solid white;
  98. }
  99. }
  100. </style>