web-class-frontend/app/stores/user.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-05-03 19:34:43 +08:00
export interface UserInfo {
2025-04-23 17:43:14 +08:00
userId: string;
userName: string;
userEmail: string;
userAvatar: string;
2025-05-03 19:34:43 +08:00
userAmount: string;
lastGetTime: Date;
}
export interface UserState extends UserInfo {
usernameOrEmail: string;
2025-04-23 17:43:14 +08:00
}
export const useUserStore = defineStore('User', {
state: (): UserState => ({
2025-05-03 19:34:43 +08:00
usernameOrEmail: '',
2025-04-23 17:43:14 +08:00
userId: '',
2025-05-03 19:34:43 +08:00
userName: '',
2025-04-23 17:43:14 +08:00
userEmail: '',
userAvatar: '',
2025-05-03 19:34:43 +08:00
userAmount: '',
lastGetTime: new Date(),
2025-04-23 17:43:14 +08:00
}),
2025-05-03 19:34:43 +08:00
actions: {
async getUserInfo() {
const { $http } = useNuxtApp();
try {
const res = await $http.GET<UserInfo>('/user/info');
console.log(res.data);
this.userId = res.data.userId;
this.userName = res.data.userName;
this.userEmail = res.data.userEmail;
this.userAvatar = res.data.userAvatar;
this.userAmount = '500';
this.lastGetTime = new Date();
} catch (error) {
console.error('Error fetching user info:', error);
}
},
async getAvatarURL() {
const now = new Date();
if (now.getTime() - this.lastGetTime.getTime() < 1000 * 60 * 9) {
return this.userAvatar;
}
this.lastGetTime = now;
const { $http } = useNuxtApp();
try {
const res = await $http.GET<string>('/user/avatar');
this.userAvatar = res.data;
} catch (error) {
console.error('Error fetching user avatar:', error);
}
},
},
2025-04-23 17:43:14 +08:00
});