fix: 申请与审批页翻页失效,页码换算成后端认识的 limit/offset
后端 GetMyApplications / GetAllApplications 只读 limit/offset,前端原先发 page/pageSize,无法识别的参数被静默忽略并退回默认的 limit=20 offset=0, 于是翻页不报错但始终返回第一页。 转换点固定在 api/application.js,对外签名 (status, page, pageSize) 不变, stores/application.js 与 stores/approval.js 两个调用点无需改动;同时对非法 页码兜底,避免算出负 offset。 application.test.js 覆盖换算结果与边界,其中「第 2 页不再是 offset=0」 是本次的回归点。
This commit is contained in:
parent
102c75079d
commit
121d0c9e1f
|
|
@ -1,17 +1,27 @@
|
||||||
import request from './index'
|
import request from './index'
|
||||||
|
|
||||||
|
// 后端的分页接口只认 limit/offset,收到分页参数后会直接忽略并退回默认的
|
||||||
|
// limit=20, offset=0,所以「点第 2 页还是第一页数据」这种失效是不报错的。
|
||||||
|
// 转换点固定在这里,调用方(stores/application.js、stores/approval.js)
|
||||||
|
// 继续按页码语义传参,不需要各自记一套后端字段名。
|
||||||
|
function toPageParams(status, page, pageSize) {
|
||||||
|
const size = pageSize > 0 ? pageSize : 20
|
||||||
|
const currentPage = page > 0 ? page : 1
|
||||||
|
return { status, limit: size, offset: (currentPage - 1) * size }
|
||||||
|
}
|
||||||
|
|
||||||
export const applicationApi = {
|
export const applicationApi = {
|
||||||
create(data) {
|
create(data) {
|
||||||
return request.post('/applications', data)
|
return request.post('/applications', data)
|
||||||
},
|
},
|
||||||
getMy(status = '', page = 1, pageSize = 20) {
|
getMy(status = '', page = 1, pageSize = 20) {
|
||||||
return request.get('/applications/my', {
|
return request.get('/applications/my', {
|
||||||
params: { status, page, pageSize }
|
params: toPageParams(status, page, pageSize)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
getAll(status = '', page = 1, pageSize = 20) {
|
getAll(status = '', page = 1, pageSize = 20) {
|
||||||
return request.get('/applications', {
|
return request.get('/applications', {
|
||||||
params: { status, page, pageSize }
|
params: toPageParams(status, page, pageSize)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
cancel(id) {
|
cancel(id) {
|
||||||
|
|
|
||||||
47
frontend/src/api/application.test.js
Normal file
47
frontend/src/api/application.test.js
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
import { applicationApi } from './application'
|
||||||
|
|
||||||
|
const request = vi.hoisted(() => ({
|
||||||
|
get: vi.fn()
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('./index', () => ({ default: request }))
|
||||||
|
|
||||||
|
describe('application API 分页参数', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
|
request.get.mockResolvedValue({ data: { items: [], total: 0 } })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('把页码换算成后端认识的 limit/offset,第 2 页不再是 offset=0', async () => {
|
||||||
|
await applicationApi.getMy('', 2, 20)
|
||||||
|
|
||||||
|
expect(request.get).toHaveBeenCalledWith('/applications/my', {
|
||||||
|
params: { status: '', limit: 20, offset: 20 }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('审批页的 getAll 同样换算,且不落下 status 筛选', async () => {
|
||||||
|
await applicationApi.getAll('pending', 3, 10)
|
||||||
|
|
||||||
|
expect(request.get).toHaveBeenCalledWith('/applications', {
|
||||||
|
params: { status: 'pending', limit: 10, offset: 20 }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('第 1 页从 offset=0 开始', async () => {
|
||||||
|
await applicationApi.getAll('', 1, 20)
|
||||||
|
|
||||||
|
expect(request.get).toHaveBeenCalledWith('/applications', {
|
||||||
|
params: { status: '', limit: 20, offset: 0 }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('页码或每页条数非法时退回第 1 页的默认值,不产生负 offset', async () => {
|
||||||
|
await applicationApi.getMy('', 0, 0)
|
||||||
|
|
||||||
|
expect(request.get).toHaveBeenCalledWith('/applications/my', {
|
||||||
|
params: { status: '', limit: 20, offset: 0 }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Reference in New Issue
Block a user