UnisKB/ui/src/components/app-avatar/index.vue

61 lines
1.1 KiB
Vue
Raw Normal View History

2023-10-25 10:35:28 +00:00
<template>
2023-11-27 10:14:48 +00:00
<el-avatar
:size="30"
:style="{ background: props.pinyinColor && getAvatarColour(firstUserName) }"
v-bind="$attrs"
>
2023-10-25 10:35:28 +00:00
<slot> {{ firstUserName }} </slot>
</el-avatar>
</template>
<script setup lang="ts">
2023-12-01 07:59:50 +00:00
import { pinyin } from 'pinyin-pro'
2023-10-25 10:35:28 +00:00
import { computed } from 'vue'
defineOptions({ name: 'AppAvatar' })
const props = defineProps({
name: {
type: String,
default: ''
2023-11-27 10:14:48 +00:00
},
pinyinColor: {
type: Boolean,
default: false
2023-10-25 10:35:28 +00:00
}
})
const firstUserName = computed(() => {
return props.name?.substring(0, 1)
})
2023-11-27 10:14:48 +00:00
2023-12-01 07:59:50 +00:00
const getAvatarColour = (name: string) => {
2023-11-27 10:14:48 +00:00
const colours = [
2023-12-01 07:59:50 +00:00
'#3370FF',
'#4954E6',
'#F54A45',
'#00B69D',
'#2CA91F',
'#98B600',
'#F80F80',
'#D136D1',
'#F01D94',
'#7F3BF5',
'#8F959E'
2023-11-27 10:14:48 +00:00
]
2023-12-04 06:04:19 +00:00
let charIndex = name
2023-12-04 06:17:57 +00:00
? pinyin(name).charAt(0).toUpperCase().charCodeAt(0) - 65 >= 0
2023-12-04 06:04:19 +00:00
? pinyin(name).charAt(0).toUpperCase().charCodeAt(0) - 65
: 0
: 0
2023-12-01 07:59:50 +00:00
function getColor() {
2023-12-04 06:04:19 +00:00
if (!colours?.[charIndex]) {
2023-12-04 06:17:57 +00:00
charIndex -= 11
2023-12-01 07:59:50 +00:00
getColor()
}
return colours[charIndex]
}
return getColor()
2023-11-27 10:14:48 +00:00
}
2023-10-25 10:35:28 +00:00
</script>
<style lang="scss" scoped></style>