You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.3 KiB

<template>
<div class="head-image" @click="showUserInfo($event)">
<img class="avatar-image" v-show="url" :src="url"
:style="avatarImageStyle" loading="lazy" />
2 years ago
<div class="avatar-text" v-show="!url" :style="avatarTextStyle">
1 year ago
{{name.substring(0,2).toUpperCase()}}</div>
2 years ago
<div v-show="online" class="online" title="用户当前在线"></div>
<slot></slot>
</div>
</template>
<script>
export default {
name: "headImage",
data() {
2 years ago
return {
1 year ago
colors: ["#5daa31", "#c7515a", "#e03697", "#85029b",
"#c9b455", "#326eb6"]
2 years ago
}
},
props: {
id:{
type: Number
},
size: {
type: Number,
default: 50
},
width: {
type: Number
},
height: {
type: Number
},
radius:{
type: String,
1 year ago
default: "50%"
},
url: {
type: String
2 years ago
},
name:{
type: String,
2 years ago
default: "?"
2 years ago
},
online:{
type: Boolean,
default:false
}
},
methods:{
showUserInfo(e){
if(this.id && this.id>0){
this.$http({
url: `/user/find/${this.id}`,
method: 'get'
}).then((user) => {
this.$store.commit("setUserInfoBoxPos",e);
this.$store.commit("showUserInfoBox",user);
})
}
}
2 years ago
},
computed:{
avatarImageStyle() {
let w = this.width ? this.width : this.size;
let h = this.height ? this.height : this.size;
return `width:${w}px; height:${h}px;
border-radius: ${this.radius};`
2 years ago
},
avatarTextStyle() {
let w = this.width ? this.width : this.size;
let h = this.height ? this.height : this.size;
return `width: ${w}px;height:${h}px;
1 year ago
background-color:${this.textColor};
font-size:${w*0.35}px;
border-radius: ${this.radius};`
2 years ago
},
textColor(){
let hash = 0;
for (var i = 0; i< this.name.length; i++) {
hash += this.name.charCodeAt(i);
}
return this.colors[hash%this.colors.length];
}
}
}
</script>
<style scoped lang="scss">
.head-image {
position: relative;
2 years ago
cursor: pointer;
.avatar-image {
position: relative;
overflow: hidden;
display: block;
}
2 years ago
.avatar-text{
1 year ago
color: white;
2 years ago
display: flex;
align-items: center;
justify-content: center;
border: 1px solid #ccc;
}
.online{
position: absolute;
right: -5px;
bottom: 0;
width: 12px;
height: 12px;
background: limegreen;
border-radius: 50%;
border: 3px solid white;
}
}
</style>