fix: 修复可接任务页面分页第二页无数据的BUG(前端分页逻辑优化)

This commit is contained in:
Your Name 2026-01-16 10:34:36 +08:00
parent 66dac83e8c
commit c46f35cfd4

View File

@ -2693,7 +2693,7 @@ const handleDeptViewChange = (deptId: number | null) => {
// 根据当前Tab加载对应数据
switch (activeTab.value) {
case 'available':
loadTasks()
loadTasks(true) // 部门切换时强制刷新
break
case 'in-progress':
loadInProgressTasks()
@ -3218,14 +3218,39 @@ const loadConfigs = async () => {
}
}
// 缓存所有可接任务数据(用于前端分页)
let allAvailableTasks: any[] = []
// 加载任务大厅列表(可接单的任务)
// 任务大厅只对接单部门开放,只显示子任务
const loadTasks = async () => {
const loadTasks = async (forceReload = false) => {
loading.value = true
try {
// 获取用户部门类型(使用部门视角切换后的有效类型)
const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
const currentDeptType = effectiveDeptType.value // 使用切换后的部门类型
const isAdmin = userInfo.isAdmin === 1 || userInfo.isAdmin === true
// 需求部门用户不应看到任务大厅的任务
if (currentDeptType === 'demand' && !isAdmin) {
taskList.value = []
total.value = 0
allAvailableTasks = []
return
}
// 如果不是强制刷新且已有缓存数据,直接使用缓存进行分页
if (!forceReload && allAvailableTasks.length > 0) {
const start = (pagination.page - 1) * pagination.pageSize
const end = start + pagination.pageSize
taskList.value = allAvailableTasks.slice(start, end)
return
}
// 构建筛选参数(不传分页参数,获取所有数据)
const params: any = {
page: pagination.page,
pageSize: pagination.pageSize
page: 1,
pageSize: 1000 // 获取足够多的数据用于前端分页
}
// 添加筛选条件
@ -3242,20 +3267,7 @@ const loadTasks = async () => {
if (filters.timeRange) params.timeRange = filters.timeRange
if (filters.sortBy) params.sortBy = filters.sortBy
// 获取用户部门类型(使用部门视角切换后的有效类型)
const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
const currentDeptType = effectiveDeptType.value // 使用切换后的部门类型
const isAdmin = userInfo.isAdmin === 1 || userInfo.isAdmin === true
let allTasks: any[] = []
let totalCount = 0
// 需求部门用户不应看到任务大厅的任务
if (currentDeptType === 'demand' && !isAdmin) {
taskList.value = []
total.value = 0
return
}
// 同时加载普通任务和子任务(流水线任务)
const [taskResponse, itemResponse] = await Promise.all([
@ -3310,9 +3322,6 @@ const loadTasks = async () => {
allTasks = [...allTasks, ...items]
}
// 计算总数(普通任务 + 子任务)
totalCount = (taskResponse.data?.total || 0) + (itemResponse.data?.total || 0)
// 按创建时间排序
allTasks.sort((a, b) => {
const timeA = new Date(a.createTime || 0).getTime()
@ -3320,11 +3329,14 @@ const loadTasks = async () => {
return timeB - timeA // 降序
})
// 前端分页(因为合并了两个接口的结果,需要在前端分页)
// 缓存所有数据
allAvailableTasks = allTasks
total.value = allTasks.length
// 前端分页
const start = (pagination.page - 1) * pagination.pageSize
const end = start + pagination.pageSize
taskList.value = allTasks.slice(start, end)
total.value = totalCount
} catch (error) {
console.error('加载任务失败:', error)
@ -3337,7 +3349,7 @@ const loadTasks = async () => {
// 筛选变更
const handleFilterChange = () => {
pagination.page = 1
loadTasks()
loadTasks(true) // 强制重新加载
}
// 重置筛选
@ -3355,12 +3367,12 @@ const handleReset = () => {
filters.timeRange = undefined
filters.sortBy = 'latest'
pagination.page = 1
loadTasks()
loadTasks(true) // 强制重新加载
}
// 页码变更
const handlePageChange = () => {
loadTasks()
loadTasks() // 使用缓存数据进行分页
}
// 加载已完成任务列表
@ -3497,7 +3509,7 @@ const handleTabChange = (key: string) => {
} else if (key === 'review') {
loadReviewTasks()
} else {
loadTasks()
loadTasks(true) // 切换Tab时强制刷新
}
}
@ -5309,7 +5321,7 @@ const handleGrab = async (task: TaskOrSubTask) => {
if (response.code === 200) {
message.success(`${taskType}抢单成功!`)
loadTasks() // 刷新列表
loadTasks(true) // 强制刷新列表
loadAllTabCounts() // 刷新所有Tab数量
}
} catch (error: any) {
@ -5655,7 +5667,7 @@ onMounted(async () => {
loadConfigs()
loadDashboardStats()
loadAllTabCounts() // 加载所有Tab的数量统计
loadTasks()
loadTasks(true) // 初始加载时强制获取数据
// 监听配置更新事件
window.addEventListener('configUpdated', handleConfigUpdate as EventListener)
})