feat: 头像显示

This commit is contained in:
2026-06-04 14:16:19 +08:00
parent 8ab14e88f1
commit aa672194ea
4 changed files with 32 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
import { createRouter, createWebHistory } from 'vue-router'
import { useUserInfoStore } from '@/stores/userInfo'
import { useGlobalStore } from '@/stores/global'
import { getUserLanguage } from '@/api/user'
import { getUserLanguage, fetchUserProfile } from '@/api/user'
import { fetchAllUnreadMessage } from '@/api/notification'
import i18n from '@/lang/index'
import myEvent from '@/utils/myEvent'
@@ -113,6 +113,16 @@ router.afterEach(async () => {
return
}
const avatarUrl = userInfoStore.state.userInfo.avatarUrl
if (!avatarUrl) {
fetchUserProfile().then((res) => {
const profile = res as any
if (profile.avatarUrl) {
userInfoStore.setAvatarUrl(profile.avatarUrl)
}
})
}
const globalStore = useGlobalStore()
fetchAllUnreadMessage().then((res) => {
globalStore.setUnredCount(res.totalUnread)

View File

@@ -13,6 +13,7 @@ export const useUserInfoStore = defineStore('userInfo', () => {
username: "",
accessToken: "",
expiresIn: "",
avatarUrl: "",
},
token: ''
})
@@ -28,6 +29,10 @@ export const useUserInfoStore = defineStore('userInfo', () => {
setLocal(data, 'token')
}
const setAvatarUrl = (url: string) => {
state.value.userInfo.avatarUrl = url
}
const logout = async (reload: boolean = false) => {
// 处理退出登录的一些逻辑
const userId = state.value.userInfo.userId
@@ -38,6 +43,7 @@ export const useUserInfoStore = defineStore('userInfo', () => {
username: "",
accessToken: "",
expiresIn: "",
avatarUrl: "",
}
state.value.token = ''
removeLocal('token')
@@ -49,6 +55,7 @@ export const useUserInfoStore = defineStore('userInfo', () => {
state,
setToken,
setUserInfo,
setAvatarUrl,
logout
}
})

View File

@@ -51,7 +51,9 @@
<div class="info">
<img src="@/assets/images/profile-content-bg.jpg" alt="" />
<div class="content">
<div class="profile"></div>
<div class="profile">
<img class="profile-avatar" :src="userInfoStore.state.userInfo.avatarUrl" alt="" />
</div>
<div class="name">
{{ $t('MainHeader.HiName', { name: userInfo.username }) }}
</div>
@@ -286,7 +288,12 @@
width: 5rem;
height: 5rem;
border-radius: 50%;
background: #cfcfcf;
.profile-avatar{
width: 100%;
height: 100%;
border-radius: 50%;
object-fit: cover;
}
}
> .name {
font-size: 1.4rem;

View File

@@ -119,10 +119,6 @@
loadUserProfile
} = useSettingsForm({ t, locale })
// watch(locale, () => {
// loadUserProfile()
// })
onMounted(() => {
loadUserProfile()
})