import { defineStore } from "pinia"; import { request } from "../api"; export const useInfoStore = defineStore({ id: "info", state: () => { return { isLoadMoreLock: false, tabs: ["exhibitions", "activates", "news", "notices"], exhibitions: [], activates: [], news: [], notices: [], detail: { createTime: "", creatorId: "", creatorName: "", display: null, id: "", name: "", publishDate: "", richText: "", thumb: "", type: "", video: "", }, total: 0, pagination: { pageNum: 1, pageSize: 20, searchKey: "", type: "", pages: 0, current: 0, }, }; }, getters: { isLoad() { return this.total > 0; }, isLast() { return ( this.pagination.current && this.pagination.pages && this.pagination.pages === this.pagination.current ); }, }, actions: { async getExhibitions(page) { this.pagination.type = "exhibition"; this.pagination.pageNum = page || 1; const { data, status } = await request.post("/show/news/pageList", { ...this.pagination, }); if (data.code === 0) { const { records, total, current, pages } = data.data; const lists = this.exhibitions.concat(records); this.exhibitions = lists; this.total = total; this.pagination.current = current; this.pagination.pages = pages; this.isLoadMoreLock = false; } else { this.resetPage(); this.exhibitions = []; } }, async getActivates(page) { this.pagination.pageNum = page || 1; this.pagination.type = "activity"; const { data, status } = await request.post("/show/news/pageList", { ...this.pagination, }); if (data.code === 0) { const { records, total, current, pages } = data.data; const lists = this.activates.concat(records); this.activates = lists; this.total = total; this.pagination.current = current; this.pagination.pages = pages; } else { this.resetPage(); this.activates = []; } }, async getNews(page) { this.pagination.type = "news"; this.pagination.pageNum = page || 1; const { data, status } = await request.post("/show/news/pageList", { ...this.pagination, }); if (data.code === 0) { const { records, total, current, pages } = data.data; const lists = this.news.concat(records); this.news = lists; this.total = total; this.pagination.current = current; this.pagination.pages = pages; } else { this.resetPage(); this.news = []; } }, async getNotices(page) { this.pagination.type = "notice"; this.pagination.pageNum = page || 1; const { data, status } = await request.post("/show/news/pageList", { ...this.pagination, }); if (data.code === 0) { const { records, total, current, pages } = data.data; const lists = this.notices.concat(records); this.notices = lists; this.total = total; this.pagination.current = current; this.pagination.pages = pages; } else { this.resetPage(); this.notices = []; } }, async getDetail(id) { const { data, status } = await request.get(`/show/news/detail/${id}`); if (data.code === 0) { this.detail = data.data; } else { this.detail = null; } }, resetPage() { this.total = 0; this.pagination = { pageNum: 1, pageSize: 20, searchKey: "", type: "", current: null, pages: null, }; }, loadMore(currentTab) { if (!this.isLoadMoreLock && !this.isLast) { console.log("loadMore", currentTab); this.isLoadMoreLock = true; const page = this.pagination.pageNum < this.pagination.pages ? this.pagination.pageNum + 1 : this.pagination.pages; switch (currentTab) { case "exhibitions": this.getExhibitions(page); break; case "activates": this.getActivates(page); break; case "news": this.getNews(page); break; case "notices": this.getNotices(page); break; } } }, switchTab(tab) { this.resetPage(); switch (tab) { case "exhibitions": this.exhibitions = []; this.getExhibitions(); break; case "activates": this.activates = []; this.getActivates(); break; case "news": this.news = []; this.getNews(); break; case "notices": this.notices = []; this.getNotices(); break; } }, }, });