Skip to content

Commit 1549a57

Browse files
YunaiVgitee-org
authored andcommitted
!385 fix: 用户头像、昵称修改,同步更新Layout/UserInfo
Merge pull request !385 from dhb52/hotfix/dhb52/avatarupdate
2 parents d3fab9a + a3e95b5 commit 1549a57

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

src/assets/imgs/avatar.jpg

-6.12 KB
Binary file not shown.

src/components/Cropper/src/CropperAvatar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useDesign } from '@/hooks/web/useDesign'
1818
import { propTypes } from '@/utils/propTypes'
1919
import { useI18n } from 'vue-i18n'
2020
import CopperModal from './CopperModal.vue'
21-
import avatar from '@/assets/imgs/avatar.jpg'
21+
import avatar from '@/assets/imgs/avatar.gif'
2222
2323
defineOptions({ name: 'CropperAvatar' })
2424

src/layout/components/UserInfo/src/UserInfo.vue

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
<script lang="ts" setup>
22
import { ElMessageBox } from 'element-plus'
33
4-
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
5-
import { useDesign } from '@/hooks/web/useDesign'
64
import avatarImg from '@/assets/imgs/avatar.gif'
7-
import { useUserStore } from '@/store/modules/user'
5+
import { useDesign } from '@/hooks/web/useDesign'
86
import { useTagsViewStore } from '@/store/modules/tagsView'
7+
import { useUserStore } from '@/store/modules/user'
98
109
defineOptions({ name: 'UserInfo' })
1110
1211
const { t } = useI18n()
1312
14-
const { wsCache } = useCache()
15-
1613
const { push, replace } = useRouter()
1714
1815
const userStore = useUserStore()
@@ -23,24 +20,21 @@ const { getPrefixCls } = useDesign()
2320
2421
const prefixCls = getPrefixCls('user-info')
2522
26-
const user = wsCache.get(CACHE_KEY.USER)
27-
28-
const avatar = user.user.avatar ? user.user.avatar : avatarImg
29-
30-
const userName = user.user.nickname ? user.user.nickname : 'Admin'
23+
const avatar = computed(() => userStore.user.avatar ?? avatarImg)
24+
const userName = computed(() => userStore.user.nickname ?? 'Admin')
3125
32-
const loginOut = () => {
33-
ElMessageBox.confirm(t('common.loginOutMessage'), t('common.reminder'), {
34-
confirmButtonText: t('common.ok'),
35-
cancelButtonText: t('common.cancel'),
36-
type: 'warning'
37-
})
38-
.then(async () => {
39-
await userStore.loginOut()
40-
tagsViewStore.delAllViews()
41-
replace('/login?redirect=/index')
26+
const loginOut = async () => {
27+
try {
28+
await ElMessageBox.confirm(t('common.loginOutMessage'), t('common.reminder'), {
29+
confirmButtonText: t('common.ok'),
30+
cancelButtonText: t('common.cancel'),
31+
type: 'warning'
4232
})
43-
.catch(() => {})
33+
await userStore.loginOut()
34+
tagsViewStore.delAllViews()
35+
replace('/login?redirect=/index')
36+
}
37+
catch { }
4438
}
4539
const toProfile = async () => {
4640
push('/user/profile')

src/store/modules/user.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ export const useUserStore = defineStore('admin-user', {
6363
wsCache.set(CACHE_KEY.USER, userInfo)
6464
wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
6565
},
66+
async setUserAvatarAction(avatar: string) {
67+
const userInfo = wsCache.get(CACHE_KEY.USER)
68+
// NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
69+
this.user.avatar = avatar
70+
userInfo.user.avatar = avatar
71+
wsCache.set(CACHE_KEY.USER, userInfo)
72+
},
73+
async setUserNicknameAction(nickname: string) {
74+
const userInfo = wsCache.get(CACHE_KEY.USER)
75+
// NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
76+
this.user.nickname = nickname
77+
userInfo.user.nickname = nickname
78+
wsCache.set(CACHE_KEY.USER, userInfo)
79+
},
6680
async loginOut() {
6781
await loginOut()
6882
removeToken()

src/views/Profile/components/BasicInfo.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import {
2121
updateUserProfile,
2222
UserProfileUpdateReqVO
2323
} from '@/api/system/user/profile'
24+
import { useUserStore } from '@/store/modules/user'
2425
2526
defineOptions({ name: 'BasicInfo' })
2627
2728
const { t } = useI18n()
2829
const message = useMessage() // 消息弹窗
30+
const userStore = useUserStore()
2931
// 表单校验
3032
const rules = reactive<FormRules>({
3133
nickname: [{ required: true, message: t('profile.rules.nickname'), trigger: 'blur' }],
@@ -78,13 +80,15 @@ const submit = () => {
7880
const data = unref(formRef)?.formModel as UserProfileUpdateReqVO
7981
await updateUserProfile(data)
8082
message.success(t('common.updateSuccess'))
81-
await init()
83+
const profile = await init()
84+
userStore.setUserNicknameAction(profile.nickname)
8285
}
8386
})
8487
}
8588
const init = async () => {
8689
const res = await getUserProfile()
8790
unref(formRef)?.setValues(res)
91+
return res
8892
}
8993
onMounted(async () => {
9094
await init()

src/views/Profile/components/UserAvatar.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414
import { propTypes } from '@/utils/propTypes'
1515
import { uploadAvatar } from '@/api/system/user/profile'
1616
import { CropperAvatar } from '@/components/Cropper'
17+
import { useUserStore } from '@/store/modules/user'
18+
1719
1820
defineOptions({ name: 'UserAvatar' })
1921
2022
defineProps({
2123
img: propTypes.string.def('')
2224
})
2325
26+
const userStore = useUserStore()
27+
28+
2429
const cropperRef = ref()
2530
const handelUpload = async ({ data }) => {
26-
await uploadAvatar({ avatarFile: data })
31+
const res = await uploadAvatar({ avatarFile: data })
2732
cropperRef.value.close()
33+
userStore.setUserAvatarAction(res.data)
2834
}
2935
</script>
3036

0 commit comments

Comments
 (0)