fix: 优化用户信息展示
This commit is contained in:
+28
-2
@@ -9,9 +9,32 @@ interface UserState {
|
||||
token: string;
|
||||
}
|
||||
|
||||
const USER_PROFILE_KEY = 'cpms_user_profile';
|
||||
|
||||
function loadUserProfile(): Pick<UserState, 'phone' | 'nickname'> {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(USER_PROFILE_KEY);
|
||||
if (!raw) return { phone: '', nickname: '' };
|
||||
const data = JSON.parse(raw);
|
||||
return {
|
||||
phone: typeof data.phone === 'string' ? data.phone : '',
|
||||
nickname: typeof data.nickname === 'string' ? data.nickname : '',
|
||||
};
|
||||
} catch {
|
||||
window.localStorage.removeItem(USER_PROFILE_KEY);
|
||||
return { phone: '', nickname: '' };
|
||||
}
|
||||
}
|
||||
|
||||
function persistUserProfile(profile: Pick<UserState, 'phone' | 'nickname'>) {
|
||||
window.localStorage.setItem(USER_PROFILE_KEY, JSON.stringify(profile));
|
||||
}
|
||||
|
||||
const savedProfile = loadUserProfile();
|
||||
|
||||
const state = reactive<UserState>({
|
||||
phone: '',
|
||||
nickname: '',
|
||||
phone: savedProfile.phone,
|
||||
nickname: savedProfile.nickname,
|
||||
token: '',
|
||||
});
|
||||
|
||||
@@ -20,12 +43,15 @@ export function useUserStore() {
|
||||
if (user.phone !== undefined) state.phone = user.phone;
|
||||
if (user.nickname !== undefined) state.nickname = user.nickname;
|
||||
if (user.token !== undefined) state.token = user.token;
|
||||
|
||||
persistUserProfile({ phone: state.phone, nickname: state.nickname });
|
||||
};
|
||||
|
||||
const clearUser = () => {
|
||||
state.phone = '';
|
||||
state.nickname = '';
|
||||
state.token = '';
|
||||
window.localStorage.removeItem(USER_PROFILE_KEY);
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user