feat: add BaseUserAvatar and unify avatar usage

This commit is contained in:
Tim
2025-09-24 00:26:51 +08:00
parent 26d1db79f4
commit efbb83924b
15 changed files with 263 additions and 91 deletions

View File

@@ -3,10 +3,19 @@
<div class="timeline-item" v-for="(item, idx) in items" :key="idx">
<div
class="timeline-icon"
:class="{ clickable: !!item.iconClick }"
@click="item.iconClick && item.iconClick()"
:class="{ clickable: !!item.iconClick && !item.src }"
@click="!item.src && item.iconClick && item.iconClick()"
>
<BaseImage v-if="item.src" :src="item.src" class="timeline-img" alt="timeline item" />
<BaseUserAvatar
v-if="item.src"
:class="['timeline-img', { 'is-clickable': !!item.iconClick }]"
:user-id="item.userId"
:avatar="item.src"
:username="item.userName || item.username"
:width="32"
:link="!item.iconClick"
@click.stop="item.iconClick && item.iconClick()"
/>
<component
v-else-if="item.icon && (typeof item.icon !== 'string' || !item.icon.includes(' '))"
:is="item.icon"
@@ -64,10 +73,12 @@ export default {
}
.timeline-img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
width: 32px;
height: 32px;
}
.timeline-img.is-clickable {
cursor: pointer;
}
.timeline-emoji {

View File

@@ -0,0 +1,116 @@
<template>
<component :is="wrapperTag" v-bind="wrapperAttrs" :class="containerClass" :style="mergedStyle">
<BaseImage :src="currentSrc" :alt="altText" class="base-user-avatar-img" @error="handleError" />
</component>
</template>
<script setup>
import { computed, ref, watch } from 'vue'
import { useAttrs } from 'vue'
const DEFAULT_AVATAR = '/default-avatar.svg'
const props = defineProps({
userId: {
type: [String, Number],
required: true,
},
avatar: {
type: String,
default: '',
},
username: {
type: String,
default: '',
},
width: {
type: [Number, String],
default: 40,
},
alt: {
type: String,
default: '',
},
link: {
type: Boolean,
default: true,
},
})
const attrs = useAttrs()
const currentSrc = ref(props.avatar || DEFAULT_AVATAR)
watch(
() => props.avatar,
(newVal) => {
currentSrc.value = newVal || DEFAULT_AVATAR
},
)
const wrapperTag = computed(() => (props.link ? 'NuxtLink' : 'div'))
const sizeStyle = computed(() => {
const value = typeof props.width === 'number' ? `${props.width}px` : props.width || '40px'
return {
width: value,
height: value,
}
})
const altText = computed(() => {
if (props.alt) return props.alt
if (props.username) return `${props.username}的头像`
return '用户头像'
})
const containerClass = computed(() => {
const classes = ['base-user-avatar']
if (props.link) classes.push('is-link')
if (attrs.class) classes.push(attrs.class)
return classes
})
const mergedStyle = computed(() => {
if (!attrs.style) return sizeStyle.value
return [sizeStyle.value, attrs.style]
})
const wrapperAttrs = computed(() => {
const { class: _class, style: _style, ...rest } = attrs
if (props.link) {
return {
...rest,
to: `/users/${props.userId}`,
}
}
return rest
})
function handleError() {
if (currentSrc.value !== DEFAULT_AVATAR) {
currentSrc.value = DEFAULT_AVATAR
}
}
</script>
<style scoped>
.base-user-avatar {
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 50%;
overflow: hidden;
background-color: var(--avatar-background, rgba(0, 0, 0, 0.05));
}
.base-user-avatar.is-link {
cursor: pointer;
}
.base-user-avatar-img {
width: 100%;
height: 100%;
border-radius: 50%;
object-fit: cover;
display: block;
}
</style>

View File

@@ -26,10 +26,14 @@
<span v-if="level >= 2" class="reply-item">
<next class="reply-icon" />
<span class="reply-info">
<BaseImage
<BaseUserAvatar
v-if="comment.parentUserName"
class="reply-avatar"
:src="comment.parentUserAvatar || '/default-avatar.svg'"
alt="avatar"
:user-id="comment.parentUserId"
:avatar="comment.parentUserAvatar"
:username="comment.parentUserName"
:width="20"
:link="Boolean(comment.parentUserId)"
@click="comment.parentUserClick && comment.parentUserClick()"
/>
<span class="reply-user-name">{{ comment.parentUserName }}</span>
@@ -253,16 +257,19 @@ const submitReply = async (parentUserName, text, clear) => {
replyList.push({
id: data.id,
userName: data.author.username,
userId: data.author.id,
time: TimeManager.format(data.createdAt),
avatar: data.author.avatar,
medal: data.author.displayMedal,
text: data.content,
parentUserName: parentUserName,
parentUserAvatar: props.comment.avatar,
parentUserId: props.comment.userId,
reactions: [],
reply: (data.replies || []).map((r) => ({
id: r.id,
userName: r.author.username,
userId: r.author.id,
time: TimeManager.format(r.createdAt),
avatar: r.author.avatar,
text: r.content,
@@ -394,9 +401,7 @@ const handleContentClick = (e) => {
.reply-avatar {
width: 20px;
height: 20px;
border-radius: 50%;
margin-right: 5px;
cursor: pointer;
}
.reply-icon {

View File

@@ -70,7 +70,14 @@
<DropdownMenu v-if="isLogin" ref="userMenu" :items="headerMenuItems">
<template #trigger>
<div class="avatar-container">
<img class="avatar-img" :src="avatar" alt="avatar" />
<BaseUserAvatar
class="avatar-img"
:user-id="authState.userId"
:avatar="avatar"
:username="authState.username"
:width="32"
:link="false"
/>
<down />
</div>
</template>
@@ -434,7 +441,6 @@ onMounted(async () => {
height: 32px;
border-radius: 50%;
background-color: lightgray;
object-fit: cover;
}
.dropdown-icon {

View File

@@ -1,12 +1,13 @@
<template>
<div :id="`change-log-${log.id}`" class="change-log-container">
<div class="change-log-text">
<BaseImage
<BaseUserAvatar
v-if="log.userAvatar"
class="change-log-avatar"
:src="log.userAvatar"
alt="avatar"
@click="() => navigateTo(`/users/${log.username}`)"
:user-id="log.userId"
:avatar="log.userAvatar"
:username="log.username"
:width="20"
/>
<span v-if="log.username" class="change-log-user">{{ log.username }}</span>
<span v-if="log.type === 'CONTENT'" class="change-log-content">变更了文章内容</span>
@@ -57,8 +58,6 @@ import { html } from 'diff2html'
import { createTwoFilesPatch } from 'diff'
import { useIsMobile } from '~/utils/screen'
import 'diff2html/bundles/css/diff2html.min.css'
import BaseImage from '~/components/BaseImage.vue'
import { navigateTo } from 'nuxt/app'
import { themeState } from '~/utils/theme'
import ArticleCategory from '~/components/ArticleCategory.vue'
import ArticleTags from '~/components/ArticleTags.vue'

View File

@@ -53,24 +53,26 @@
</div>
</div>
<div class="prize-member-container">
<BaseImage
<BaseUserAvatar
v-for="p in lotteryParticipants"
:key="p.id"
class="prize-member-avatar"
:src="p.avatar"
alt="avatar"
@click="gotoUser(p.id)"
:user-id="p.id"
:avatar="p.avatar"
:username="p.username"
:width="30"
/>
<div v-if="lotteryEnded && lotteryWinners.length" class="prize-member-winner">
<medal-one class="medal-icon"></medal-one>
<span class="prize-member-winner-name">获奖者: </span>
<BaseImage
<BaseUserAvatar
v-for="w in lotteryWinners"
:key="w.id"
class="prize-member-avatar"
:src="w.avatar"
alt="avatar"
@click="gotoUser(w.id)"
:user-id="w.id"
:avatar="w.avatar"
:username="w.username"
:width="30"
/>
<div v-if="lotteryWinners.length === 1" class="prize-member-winner-name">
{{ lotteryWinners[0].username }}
@@ -106,8 +108,6 @@ const hasJoined = computed(() => {
return lotteryParticipants.value.some((p) => p.id === Number(authState.userId))
})
const gotoUser = (id) => navigateTo(`/users/${id}`, { replace: true })
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl
const joinLottery = async () => {
@@ -246,8 +246,6 @@ const joinLottery = async () => {
width: 30px;
height: 30px;
margin-left: 3px;
border-radius: 50%;
object-fit: cover;
cursor: pointer;
}

View File

@@ -17,13 +17,14 @@
></div>
</div>
<div class="poll-participants">
<BaseImage
<BaseUserAvatar
v-for="p in pollOptionParticipants[idx] || []"
:key="p.id"
class="poll-participant-avatar"
:src="p.avatar"
alt="avatar"
@click="gotoUser(p.id)"
:user-id="p.id"
:avatar="p.avatar"
:username="p.username"
:width="30"
/>
</div>
</div>
@@ -152,8 +153,6 @@ watch([hasVoted, pollEnded], ([voted, ended]) => {
if (voted || ended) showPollResult.value = true
})
const gotoUser = (id) => navigateTo(`/users/${id}`, { replace: true })
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl
const voteOption = async (idx) => {
@@ -426,7 +425,6 @@ const submitMultiPoll = async () => {
.poll-participant-avatar {
width: 30px;
height: 30px;
border-radius: 50%;
cursor: pointer;
}
</style>

View File

@@ -24,10 +24,13 @@
</template>
<template #option="{ option }">
<div class="search-option-item">
<BaseImage
:src="option.avatar || '/default-avatar.svg'"
<BaseUserAvatar
class="avatar"
@error="handleAvatarError"
:user-id="option.id"
:avatar="option.avatar"
:username="option.username"
:width="32"
:link="false"
/>
<div class="result-body">
<div class="result-main" v-html="highlight(option.username)"></div>
@@ -87,10 +90,6 @@ const highlight = (text) => {
return text.replace(reg, (m) => `<span class="highlight">${m}</span>`)
}
const handleAvatarError = (e) => {
e.target.src = '/default-avatar.svg'
}
watch(selected, async (val) => {
if (!val) return
const user = results.value.find((u) => u.id === val)
@@ -178,8 +177,6 @@ defineExpose({
.avatar {
width: 32px;
height: 32px;
border-radius: 50%;
object-fit: cover;
}
.result-body {

View File

@@ -2,7 +2,14 @@
<div class="user-list">
<BasePlaceholder v-if="users.length === 0" text="暂无用户" icon="inbox" />
<div v-for="u in users" :key="u.id" class="user-item" @click="handleUserClick(u)">
<BaseImage :src="u.avatar" alt="avatar" class="user-avatar" />
<BaseUserAvatar
class="user-avatar"
:user-id="u.id"
:avatar="u.avatar"
:username="u.username"
:width="50"
:link="false"
/>
<div class="user-info">
<div class="user-name">{{ u.username }}</div>
<div v-if="u.introduction" class="user-intro">{{ u.introduction }}</div>
@@ -41,9 +48,7 @@ const handleUserClick = (user) => {
.user-avatar {
width: 50px;
height: 50px;
border-radius: 50%;
flex-shrink: 0;
object-fit: cover;
}
.user-info {
display: flex;