@@ -4,6 +4,7 @@ import { useRequest } from 'alova'
4
4
import { ElMessage } from ' element-plus'
5
5
import { Select , CloseBold , EditPen } from ' @element-plus/icons-vue'
6
6
import { useUserStore } from ' @/stores/user'
7
+ import { useCachedStore } from ' @/stores/cached'
7
8
import { SexEnum , IsYetEnum } from ' @/enums'
8
9
import type { BadgeType } from ' @/services/types'
9
10
import apis from ' @/services/apis'
@@ -30,6 +31,7 @@ const editName = reactive({
30
31
})
31
32
32
33
const userStore = useUserStore ()
34
+ const cachedStore = useCachedStore ()
33
35
34
36
const userInfo = computed (() => userStore .userInfo )
35
37
const { send : handlerGetBadgeList, data : badgeList } = useRequest (apis .getBadgeList , {
@@ -47,12 +49,21 @@ const currentBadge = computed(() =>
47
49
badgeList .value .find ((item ) => item .obtain === IsYetEnum .YES && item .wearing === IsYetEnum .YES ),
48
50
)
49
51
52
+ // 更新缓存里面的用户信息
53
+ const updateCurrentUserCache = (key : ' name' | ' wearingItemId' , value : any ) => {
54
+ const currentUser = userStore .userInfo .uid && cachedStore .userCachedList [userStore .userInfo .uid ]
55
+ if (currentUser ) {
56
+ currentUser [key ] = value // 更新缓存里面的用户信息
57
+ }
58
+ }
59
+
50
60
// 佩戴卸下徽章
51
61
const toggleWarningBadge = async (badge : BadgeType ) => {
52
62
if (! badge ?.id ) return
53
63
await apis .setUserBadge (badge .id ).send ()
54
64
handlerGetBadgeList ()
55
65
badge .img && (userInfo .value .badge = badge .img )
66
+ updateCurrentUserCache (' wearingItemId' , badge .id ) // 更新缓存里面的用户徽章
56
67
}
57
68
58
69
// 编辑用户名
@@ -73,13 +84,15 @@ const onSaveUserName = async () => {
73
84
return
74
85
}
75
86
editName .saving = true
76
- await apis .modifyUserName (editName .tempName ).send ()
77
- userStore .userInfo .name = editName .tempName
78
- editName .saving = false
79
- editName .isEdit = false
80
- editName .tempName = ' '
87
+
88
+ await apis .modifyUserName (editName .tempName ).send () // 更改用户名
89
+ userStore .userInfo .name = editName .tempName // 更新用户信息里面的用户名
90
+ updateCurrentUserCache (' name' , editName .tempName ) // 更新缓存里面的用户信息
91
+ // 重置状态
92
+ onCancelEditName ()
93
+ // 没有更名机会就不走下去
81
94
if (! userInfo .value ?.modifyNameChance || userInfo .value .modifyNameChance === 0 ) return
82
- userInfo .value .modifyNameChance = userInfo .value ?.modifyNameChance - 1
95
+ userInfo .value .modifyNameChance = userInfo .value ?.modifyNameChance - 1 // 减少更名次数
83
96
}
84
97
// 确认保存用户名
85
98
const onCancelEditName = async () => {
0 commit comments