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( `/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('/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('/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('/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); } }, }, });