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.

130 lines
2.6 KiB

<template>
1 year ago
<div class="head-image" @click="showUserInfo($event)" :style="{cursor : isShowUserInfo ? 'pointer': null}">
1 year ago
<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: {
1 year ago
id: {
type: Number
},
size: {
type: Number,
1 year ago
default: 42
},
width: {
type: Number
},
height: {
type: Number
},
1 year ago
radius: {
type: String,
1 year ago
default: "50%"
},
url: {
type: String
2 years ago
},
1 year ago
name: {
2 years ago
type: String,
1 year ago
default: null
2 years ago
},
1 year ago
online: {
2 years ago
type: Boolean,
1 year ago
default: false
1 year ago
},
1 year ago
isShowUserInfo: {
type: Boolean,
default: true
}
},
1 year ago
methods: {
showUserInfo(e) {
if (!this.isShowUserInfo) return;
if (this.id && this.id > 0) {
this.$http({
url: `/user/find/${this.id}`,
method: 'get'
}).then((user) => {
1 year ago
this.$store.commit("setUserInfoBoxPos", e);
this.$store.commit("showUserInfoBox", user);
})
}
}
2 years ago
},
1 year 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;
1 year ago
return `
width: ${w}px;height:${h}px;
background-color: ${this.name ? this.textColor : '#fff'};
1 year ago
font-size:${w*0.35}px;
1 year ago
border-radius: ${this.radius};
`
2 years ago
},
1 year ago
textColor() {
2 years ago
let hash = 0;
1 year ago
for (var i = 0; i < this.name.length; i++) {
2 years ago
hash += this.name.charCodeAt(i);
}
1 year ago
return this.colors[hash % this.colors.length];
2 years ago
}
1 year ago
}
}
</script>
<style scoped lang="scss">
.head-image {
position: relative;
1 year ago
//cursor: pointer;
2 years ago
.avatar-image {
position: relative;
overflow: hidden;
display: block;
}
1 year ago
1 year ago
.avatar-text {
1 year ago
color: white;
2 years ago
display: flex;
align-items: center;
justify-content: center;
1 year ago
//border: 1px solid #ccc;
1 year ago
//box-shadow: var(--im-box-shadow);
1 year ago
}
1 year ago
.online {
2 years ago
position: absolute;
right: -5px;
bottom: 0;
width: 12px;
height: 12px;
background: limegreen;
border-radius: 50%;
1 year ago
border: 2px solid white;
}
}
1 year ago
</style>