head-image.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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: ["#7dd24b", "#c7515a", "#db68ef", "#15d29b", "#85029b",
  18. "#c9b455", "#fb2609", "#bda818", "#af0831", "#326eb6"
  19. ]
  20. }
  21. },
  22. props: {
  23. id: {
  24. type: Number
  25. },
  26. size: {
  27. type: Number,
  28. default: 20
  29. },
  30. url: {
  31. type: String
  32. },
  33. name: {
  34. type: String,
  35. default: "?"
  36. },
  37. online: {
  38. type: Boolean,
  39. default: false
  40. }
  41. },
  42. methods: {
  43. showUserInfo(e) {
  44. if (this.id && this.id > 0) {
  45. uni.navigateTo({
  46. url: "/pages/common/user-info?id="+this.id
  47. })
  48. }
  49. }
  50. },
  51. computed: {
  52. avatarImageStyle() {
  53. return `width:${this.size}rpx; height:${this.size}rpx;`
  54. },
  55. avatarTextStyle() {
  56. return `width: ${this.size}rpx;height:${this.size}rpx;
  57. color:${this.textColor};font-size:${this.size*0.6}rpx;`
  58. },
  59. textColor() {
  60. let hash = 0;
  61. for (var i = 0; i < this.name.length; i++) {
  62. hash += this.name.charCodeAt(i);
  63. }
  64. return this.colors[hash % this.colors.length];
  65. }
  66. }
  67. }
  68. </script>
  69. <style scoped lang="scss">
  70. .head-image {
  71. position: relative;
  72. cursor: pointer;
  73. .avatar-image {
  74. position: relative;
  75. overflow: hidden;
  76. border-radius: 10%;
  77. border: 1px solid #ccc;
  78. vertical-align: bottom;
  79. }
  80. .avatar-text {
  81. background-color: #f2f2f2;
  82. /* 默认背景色 */
  83. border-radius: 50%;
  84. /* 圆角效果 */
  85. display: flex;
  86. align-items: center;
  87. justify-content: center;
  88. border: 1px solid #ccc;
  89. }
  90. .online {
  91. position: absolute;
  92. right: -10%;
  93. bottom: 0;
  94. width: 24rpx;
  95. height: 24rpx;
  96. background: limegreen;
  97. border-radius: 50%;
  98. border: 6rpx solid white;
  99. }
  100. }
  101. </style>