diff --git a/public/web.config b/public/web.config index 038459e..fb921e2 100644 --- a/public/web.config +++ b/public/web.config @@ -3,6 +3,13 @@ + + + + + + + diff --git a/src/components/ExpListContent.vue b/src/components/ExpListContent.vue index 4f9662e..4556514 100644 --- a/src/components/ExpListContent.vue +++ b/src/components/ExpListContent.vue @@ -8,7 +8,33 @@ export default defineComponent({ titles:[], error:null, isLoading: false, - pagination: null + pagination: null as { page: number, pageSize: number, pageCount: number, total: number } | null, + currentPageInput: 1, + } + }, + computed: { + _currentPage(): number { + if (this.pagination && typeof this.pagination.page === 'number') { + return this.pagination.page; + } + const params = new URLSearchParams(window.location.search); + const pageParam = params.get('page'); + if (pageParam) { + const parsedPage = parseInt(pageParam, 10); + if (!isNaN(parsedPage) && parsedPage > 0) { + return parsedPage; + } + } + return 1; + }, + totalPages(): number { + return (this.pagination && typeof this.pagination.pageCount === 'number' && this.pagination.pageCount > 0) ? this.pagination.pageCount : 1; + }, + isFirstPage(): boolean { + return this._currentPage === 1; + }, + isLastPage(): boolean { + return this._currentPage === this.totalPages; } }, methods:{ @@ -16,54 +42,109 @@ export default defineComponent({ this.isLoading=true; this.error=null; const params = new URLSearchParams(window.location.search); - const type = params.get('type'); - console.log('type!',type); let pageNum=1; - if(this.isActiveRoute('Examples')){ - const params = new URLSearchParams(window.location.search); - const page = params.get('page'); - if(page!=null){ - pageNum=parseInt(page, 10); + const pageParam = params.get('page'); + if(pageParam!=null){ + const parsedPage = parseInt(pageParam, 10); + if(!isNaN(parsedPage) && parsedPage > 0){ + pageNum=parsedPage; } } - let reqarg='/api/project-examples?fields=title,createdAt&sort=createdAt:desc&pagination[page]='+pageNum+'&pagination[pageSize]=15'; + this.currentPageInput = pageNum; + + let reqarg=`/api/project-examples?fields=title,createdAt&sort=createdAt:desc&pagination[page]=${pageNum}&pagination[pageSize]=15`; this.$axios.get(reqarg) .then(response =>{ const resp = response.data; - this.pagination= resp.meta.pagination; - // if(resp.meta.pagination.pageCount==0){ - // window.location.href = '/Examples'; - // console.log('turn'); - // } + this.pagination = resp.meta.pagination; + + if (this.pagination && typeof this.pagination.page === 'number') { + this.currentPageInput = this.pagination.page; + } + if (resp && Array.isArray(resp.data)) { this.titles = resp.data; - console.log('reqarg',reqarg); - if(this.titles.length==0){ - this.error = "返回的 JSON 对象中不存在 'data' 数组"; + if(this.titles.length === 0 && this.pagination && this.pagination.pageCount > 0 && this.pagination.page > this.pagination.pageCount){ + // 如果请求的页码超出总页数,且有数据(虽然这里是 length === 0,但 pageCount > 0 暗示有数据分布在其他页) + // 则跳转到最后一页 + this.navigateToPage(this.pagination.pageCount); + return; } } else { this.error = "返回的 JSON 对象中不存在 'data' 数组"; + this.pagination = null; } }) .catch(err => { console.error(err); this.error = '请求失败,请稍后重试'; + this.pagination = null; }) .finally(() => { this.isLoading = false; }); }, - isActiveRoute:function(routename) { - // Example: Apply style if route name starts with 'user-' - console.log('Current route name:', this.$route.path, 'Comparing with:', routename); - return this.$route.path === routename; + navigateToPage(page: number | string) { + const targetPage = Number(page); + + if (isNaN(targetPage) || targetPage < 1 || (this.pagination && this.pagination.pageCount > 0 && targetPage > this.totalPages) || targetPage === this._currentPage) { + if (targetPage !== this._currentPage && (targetPage < 1 || (this.pagination && this.pagination.pageCount > 0 && targetPage > this.totalPages))) { + alert(`页码必须在 1 到 ${this.totalPages} 之间。`); + } + if (this.pagination && typeof this.pagination.page === 'number') { + this.currentPageInput = this.pagination.page; // 重置输入框为当前有效页码 + } + return; + } + + // 所有分页按钮都使用 window.location.href 跳转到 /Examples?page=xxx + window.location.href = `/Examples?page=${targetPage}`; + }, + goToFirstPage() { + this.navigateToPage(1); + }, + goToPreviousPage() { + if (!this.isFirstPage) { + this.navigateToPage(this._currentPage - 1); + } + }, + goToNextPage() { + if (!this.isLastPage) { + this.navigateToPage(this._currentPage + 1); + } + }, + goToLastPage() { + if (this.pagination && this.pagination.pageCount) { + this.navigateToPage(this.totalPages); + } + }, + jumpToPage() { + const targetPage = Number(this.currentPageInput); + if (!isNaN(targetPage)) { + this.navigateToPage(targetPage); + } else { + alert('请输入有效的页码。'); + if (this.pagination && typeof this.pagination.page === 'number') { + this.currentPageInput = this.pagination.page; + } + } + }, + isActiveRoute(routePath: string): boolean { + return this.$route.path === routePath; }, toExamples:function(){ - window.location.href='/Examples'; + // 跳转到 /Examples 的第一页 + window.location.href='/Examples?page=1'; } }, mounted() { this.fetch_items(); + // 同步 currentPageInput + if (this.pagination && typeof this.pagination.page === 'number') { + this.currentPageInput = this.pagination.page; + } else { + this.currentPageInput = this._currentPage; // _currentPage 会从 URL 读取 page 参数 + } } }) @@ -86,15 +167,57 @@ export default defineComponent({ +
加载中...
+
{{ error }}
+
+ 暂无数据 +
+ + + +
+ +
- - - + v-else-if="!isActiveRoute('/Examples') || (pagination && pagination.pageCount <= 1 && titles.length > 0 && !isLoading)"> + @@ -104,4 +227,25 @@ export default defineComponent({ diff --git a/src/main.js b/src/main.js index dd00093..de9df58 100644 --- a/src/main.js +++ b/src/main.js @@ -12,7 +12,7 @@ import 'bootstrap/dist/css/bootstrap.min.css' import 'bootstrap/dist/js/bootstrap.bundle.min.js' const app = createApp(App) -axios.defaults.baseURL = 'https://cms2.yangprivate.site' +axios.defaults.baseURL = 'https://cms.kaldorei.site' // 全局挂载 axios,所有组件都可以 this.$axios 访问 app.config.globalProperties.$axios = axios