✨ 完成大作业部分及其打磨
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
export type Article = {
|
||||
id: number;
|
||||
title: string;
|
||||
mainIdea: string;
|
||||
content: string;
|
||||
};
|
||||
export type ArticleUser = {
|
||||
id: number;
|
||||
name: string;
|
||||
amount: number;
|
||||
};
|
||||
type ArticleStore = {
|
||||
articleList: ArticleUser[];
|
||||
pagination: BasicPagination;
|
||||
currentManageUser: UserInfo;
|
||||
currentUserAvatar: string;
|
||||
currentUserLastGetTime: Date;
|
||||
currentUserArticleList: Article[];
|
||||
currentUserPagination: BasicPagination;
|
||||
currentArticle: Article;
|
||||
};
|
||||
export const useArticleStore = defineStore('Article', {
|
||||
state: (): ArticleStore => ({
|
||||
articleList: [],
|
||||
pagination: {
|
||||
current: 1,
|
||||
pageSize: 5,
|
||||
defaultPageSize: 5,
|
||||
total: 0,
|
||||
defaultCurrent: 1,
|
||||
},
|
||||
currentManageUser: {
|
||||
userId: '',
|
||||
userName: '',
|
||||
userEmail: '',
|
||||
userAvatarPath: '',
|
||||
userAmount: '',
|
||||
userBirthday: '',
|
||||
},
|
||||
currentUserAvatar: '',
|
||||
currentUserLastGetTime: new Date(),
|
||||
currentUserArticleList: [],
|
||||
currentUserPagination: {
|
||||
current: 1,
|
||||
pageSize: 5,
|
||||
defaultPageSize: 5,
|
||||
total: 0,
|
||||
defaultCurrent: 1,
|
||||
},
|
||||
currentArticle: {
|
||||
id: 0,
|
||||
title: '',
|
||||
mainIdea: '',
|
||||
content: '',
|
||||
},
|
||||
}),
|
||||
actions: {
|
||||
async modifyArticle() {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
await $http.POST('/article/updateArticle', {
|
||||
...this.currentArticle,
|
||||
author: this.currentManageUser.userName,
|
||||
});
|
||||
this.getCurrentManageUserArticleList();
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error adding article:' + error);
|
||||
}
|
||||
},
|
||||
async deleteArticle(id: number) {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
await $http.GET('/article/deleteArticle', {
|
||||
id: '' + id,
|
||||
});
|
||||
this.getCurrentManageUserArticleList();
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error deleting article:' + error);
|
||||
}
|
||||
},
|
||||
async addNewArticle() {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
await $http.POST('/article/addArticle', {
|
||||
...this.currentArticle,
|
||||
author: this.currentManageUser.userName,
|
||||
});
|
||||
this.getCurrentManageUserArticleList();
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error adding article:' + error);
|
||||
}
|
||||
},
|
||||
async getCurrentManageUserArticleList() {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<{ records: Article[] }>(
|
||||
`/article/page/${this.currentManageUser.userName}`,
|
||||
{
|
||||
page: '' + this.pagination!.current,
|
||||
size: '' + this.pagination!.pageSize,
|
||||
},
|
||||
);
|
||||
this.currentUserArticleList = res.data.records;
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error fetching people list:' + error);
|
||||
}
|
||||
},
|
||||
async getCurrentManageUserArticleTotal() {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<number>(
|
||||
`/article/total/${this.currentManageUser.userName}`,
|
||||
);
|
||||
console.log(res.data);
|
||||
this.currentUserPagination.total = res.data;
|
||||
if (
|
||||
(this.currentUserPagination!.current - 1) *
|
||||
this.currentUserPagination!.pageSize >
|
||||
this.currentUserPagination!.total
|
||||
) {
|
||||
this.currentUserPagination!.current = Math.ceil(
|
||||
this.currentUserPagination!.total /
|
||||
this.currentUserPagination!.pageSize,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error fetching article total:' + error);
|
||||
}
|
||||
},
|
||||
async getCurrentManageUserAvatar(forceUpdate: boolean = false) {
|
||||
const now = new Date();
|
||||
if (
|
||||
!forceUpdate &&
|
||||
this.currentUserAvatar !== '' &&
|
||||
now.getTime() - this.currentUserLastGetTime.getTime() < 1000 * 60 * 9
|
||||
) {
|
||||
return this.currentUserAvatar;
|
||||
}
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<string>('/article/userAvatar', {
|
||||
userId: this.currentManageUser.userId,
|
||||
});
|
||||
this.currentUserAvatar = res.data;
|
||||
return this.currentUserAvatar;
|
||||
} catch (error) {
|
||||
await MessagePlugin.error(
|
||||
'Error fetching article user avatar:' + error,
|
||||
);
|
||||
}
|
||||
},
|
||||
async getCurrentManageUserInfo(userId: string) {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<UserInfo>('/article/userInfo', {
|
||||
userId: userId,
|
||||
});
|
||||
this.currentManageUser = res.data;
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error fetching article user info:' + error);
|
||||
}
|
||||
},
|
||||
async getArticleList() {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<{ records: ArticleUser[] }>(
|
||||
'/article/page',
|
||||
{
|
||||
page: '' + this.pagination!.current,
|
||||
size: '' + this.pagination!.pageSize,
|
||||
},
|
||||
);
|
||||
this.articleList = res.data.records;
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error fetching article list:' + error);
|
||||
}
|
||||
},
|
||||
async getTotal() {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<number>('/article/total');
|
||||
this.pagination.total = res.data;
|
||||
if (
|
||||
(this.pagination!.current - 1) * this.pagination!.pageSize >
|
||||
this.pagination!.total
|
||||
) {
|
||||
this.pagination!.current = Math.ceil(
|
||||
this.pagination!.total / this.pagination!.pageSize,
|
||||
);
|
||||
this.getArticleList();
|
||||
}
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error fetching article total:' + error);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
+5
-5
@@ -79,7 +79,7 @@ export const useLoginStore = defineStore('Login', {
|
||||
email: email,
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
await MessagePlugin.error(e as string);
|
||||
return false;
|
||||
}
|
||||
if (result.code === 200) {
|
||||
@@ -106,19 +106,20 @@ export const useLoginStore = defineStore('Login', {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
async register() {
|
||||
async register(recallWhenSuccess: () => void) {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
await $http.POST('/login/register', this.registerForm);
|
||||
recallWhenSuccess();
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
},
|
||||
async reset() {
|
||||
console.log(this.resetForm);
|
||||
async reset(recallWhenSuccess: () => void) {
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
await $http.POST('/login/passwordReset', this.resetForm);
|
||||
recallWhenSuccess();
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
@@ -144,7 +145,6 @@ export const useLoginStore = defineStore('Login', {
|
||||
case PageType.reset:
|
||||
return '重置密码';
|
||||
default:
|
||||
console.log('未知');
|
||||
return '未知';
|
||||
}
|
||||
},
|
||||
|
||||
@@ -58,9 +58,8 @@ export const usePeopleStore = defineStore('People', {
|
||||
},
|
||||
);
|
||||
this.peopleList = res.data.records;
|
||||
console.log(this.peopleList);
|
||||
} catch (error) {
|
||||
console.error('Error fetching people list:', error);
|
||||
await MessagePlugin.error('Error fetching people list:' + error);
|
||||
}
|
||||
},
|
||||
async getTotal() {
|
||||
@@ -79,12 +78,11 @@ export const usePeopleStore = defineStore('People', {
|
||||
);
|
||||
this.getPeopleList();
|
||||
}
|
||||
} catch {
|
||||
return;
|
||||
} catch (error) {
|
||||
await MessagePlugin.error('Error fetching people total:' + error);
|
||||
}
|
||||
},
|
||||
async addPeople() {
|
||||
console.log('add');
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
await $http.POST('/userManager/addUser', this.currentForm);
|
||||
|
||||
+28
-10
@@ -2,36 +2,45 @@ export interface UserInfo {
|
||||
userId: string;
|
||||
userName: string;
|
||||
userEmail: string;
|
||||
userAvatar: string;
|
||||
userAvatarPath: string;
|
||||
userAmount: string;
|
||||
userBirthday: string;
|
||||
lastGetTime: Date;
|
||||
}
|
||||
export class ExampleUserInfo implements UserInfo {
|
||||
userId = '';
|
||||
userName = '';
|
||||
userEmail = '';
|
||||
userAvatarPath = '';
|
||||
userAmount = '0';
|
||||
userBirthday = '';
|
||||
}
|
||||
export interface UserState extends UserInfo {
|
||||
lastGetTime: Date;
|
||||
usernameOrEmail: string;
|
||||
userAvatarUrl: string;
|
||||
}
|
||||
export const useUserStore = defineStore('User', {
|
||||
state: (): UserState => ({
|
||||
usernameOrEmail: '',
|
||||
userId: '',
|
||||
userName: '',
|
||||
userEmail: '',
|
||||
userAvatar: '',
|
||||
userAvatarPath: '',
|
||||
userAmount: '',
|
||||
userBirthday: '',
|
||||
lastGetTime: new Date(),
|
||||
usernameOrEmail: '',
|
||||
userAvatarUrl: '',
|
||||
}),
|
||||
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.userAvatarPath = res.data.userAvatarPath;
|
||||
this.userAmount = res.data.userAmount;
|
||||
this.userBirthday = res.data.userBirthday;
|
||||
this.lastGetTime = new Date();
|
||||
} catch (error) {
|
||||
@@ -40,14 +49,23 @@ export const useUserStore = defineStore('User', {
|
||||
},
|
||||
async getAvatarURL() {
|
||||
const now = new Date();
|
||||
if (now.getTime() - this.lastGetTime.getTime() < 1000 * 60 * 9) {
|
||||
return this.userAvatar;
|
||||
console.log('Current time:', now);
|
||||
if (
|
||||
this.userAvatarUrl !== '' &&
|
||||
now.getTime() - this.lastGetTime.getTime() < 1000 * 60 * 9
|
||||
) {
|
||||
console.log('Using cached avatar URL');
|
||||
console.log('Cached avatar URL:', this.userAvatarUrl);
|
||||
return this.userAvatarUrl;
|
||||
}
|
||||
console.log('Fetching new avatar URL');
|
||||
this.lastGetTime = now;
|
||||
const { $http } = useNuxtApp();
|
||||
try {
|
||||
const res = await $http.GET<string>('/user/avatar');
|
||||
this.userAvatar = res.data;
|
||||
this.userAvatarUrl = res.data;
|
||||
console.log('User avatar URL:', this.userAvatarUrl);
|
||||
return this.userAvatarUrl;
|
||||
} catch (error) {
|
||||
console.error('Error fetching user avatar:', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user