1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| <script setup lang="ts">
| import { computed } from 'vue'
|
| const props = withDefaults(defineProps<{
| name?: string
| src?: string
| size?: number
| fontSize?: number
| }>(), {
| name: '',
| src: '',
| size: 56,
| fontSize: 24,
| })
|
| const palette = [
| '#2F80ED',
| '#27AE60',
| '#9B51E0',
| '#F2994A',
| '#EB5757',
| '#56CCF2',
| '#219653',
| '#6FCF97',
| ]
|
| const displayInitial = computed(() => {
| const raw = (props.name || '').trim()
| if (!raw) {
| return '?'
| }
| return raw.slice(0, 1).toUpperCase()
| })
|
| const avatarStyle = computed(() => {
| const text = (props.name || '').trim()
| const code = text ? text.charCodeAt(0) : 0
| return {
| width: `${props.size}px`,
| height: `${props.size}px`,
| lineHeight: `${props.size}px`,
| fontSize: `${props.fontSize}px`,
| backgroundColor: palette[code % palette.length],
| }
| })
|
| const shouldUseImage = computed(() => Boolean(props.src && props.src.trim()))
| </script>
|
| <template>
| <view class="name-avatar" :style="avatarStyle">
| <image v-if="shouldUseImage" :src="src" class="name-avatar__img" mode="aspectFill" />
| <text v-else class="name-avatar__text">{{ displayInitial }}</text>
| </view>
| </template>
|
| <style scoped>
| .name-avatar {
| border-radius: 9999px;
| overflow: hidden;
| display: inline-flex;
| align-items: center;
| justify-content: center;
| color: #ffffff;
| font-weight: 600;
| flex-shrink: 0;
| }
|
| .name-avatar__img {
| width: 100%;
| height: 100%;
| }
|
| .name-avatar__text {
| display: block;
| text-align: center;
| }
| </style>
|
|