HeadImage.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div class="head-image" @click="showUserInfo($event)">
  3. <img class="avatar-image" v-show="url" :src="url"
  4. :style="avatarImageStyle" loading="lazy" />
  5. <div class="avatar-text" v-show="!url" :style="avatarTextStyle">
  6. {{name.substring(0,1).toUpperCase()}}</div>
  7. <div v-show="online" class="online" title="用户当前在线"></div>
  8. <slot></slot>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. name: "headImage",
  14. data() {
  15. return {
  16. colors:["#7dd24b","#c7515a","#db68ef","#15d29b","#85029b",
  17. "#c9b455","#fb2609","#bda818","#af0831","#326eb6"]
  18. }
  19. },
  20. props: {
  21. id:{
  22. type: Number
  23. },
  24. size: {
  25. type: Number,
  26. default: 50
  27. },
  28. url: {
  29. type: String
  30. },
  31. name:{
  32. type: String,
  33. default: "?"
  34. },
  35. online:{
  36. type: Boolean,
  37. default:false
  38. }
  39. },
  40. methods:{
  41. showUserInfo(e){
  42. if(this.id && this.id>0){
  43. this.$http({
  44. url: `/user/find/${this.id}`,
  45. method: 'get'
  46. }).then((user) => {
  47. this.$store.commit("setUserInfoBoxPos",e);
  48. this.$store.commit("showUserInfoBox",user);
  49. })
  50. }
  51. }
  52. },
  53. computed:{
  54. avatarImageStyle(){
  55. return `width:${this.size}px; height:${this.size}px;`
  56. },
  57. avatarTextStyle(){
  58. return `width: ${this.size}px;height:${this.size}px;
  59. color:${this.textColor};font-size:${this.size*0.6}px;`
  60. },
  61. textColor(){
  62. let hash = 0;
  63. for (var i = 0; i< this.name.length; i++) {
  64. hash += this.name.charCodeAt(i);
  65. }
  66. return this.colors[hash%this.colors.length];
  67. }
  68. }
  69. }
  70. </script>
  71. <style scoped lang="scss">
  72. .head-image {
  73. position: relative;
  74. cursor: pointer;
  75. .avatar-image {
  76. position: relative;
  77. overflow: hidden;
  78. border-radius: 10%;
  79. }
  80. .avatar-text{
  81. background-color: #f2f2f2; /* 默认背景色 */
  82. border-radius: 50%; /* 圆角效果 */
  83. display: flex;
  84. align-items: center;
  85. justify-content: center;
  86. border: 1px solid #ccc;
  87. }
  88. .online{
  89. position: absolute;
  90. right: -5px;
  91. bottom: 0;
  92. width: 12px;
  93. height: 12px;
  94. background: limegreen;
  95. border-radius: 50%;
  96. border: 3px solid white;
  97. }
  98. }
  99. </style>