From a3e1abbe404749aa840086989214b6fbc126e3d5 Mon Sep 17 00:00:00 2001 From: zhanghao <774378400@qq.com> Date: Fri, 16 Oct 2026 16:49:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=94=B9=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E9=9D=99=E9=BB=98=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/project/manage/api/errors.ts | 4 + .../src/views/project/manage/api/index.ts | 2 +- .../composables/useProjectCollaboration.ts | 176 ++++++++++++------ .../work-items/components/ItemDetail.vue | 33 +++- .../project/manage/shared/utils/snapshot.ts | 24 +++ .../project/manage/views/board/WorkBoard.vue | 4 +- .../project/manage/views/flow/WorkFlow.vue | 4 +- .../views/flow/components/FlowReworkEdge.vue | 16 +- .../project/manage/views/flow/utils/graph.ts | 5 +- .../manage/views/list/WorkItemList.vue | 32 +++- .../views/workspace/ProjectWorkspace.vue | 2 + 11 files changed, 226 insertions(+), 76 deletions(-) create mode 100644 ruoyi-ui-vue3/src/views/project/manage/shared/utils/snapshot.ts diff --git a/ruoyi-ui-vue3/src/views/project/manage/api/errors.ts b/ruoyi-ui-vue3/src/views/project/manage/api/errors.ts index a254000..eec03f2 100644 --- a/ruoyi-ui-vue3/src/views/project/manage/api/errors.ts +++ b/ruoyi-ui-vue3/src/views/project/manage/api/errors.ts @@ -17,6 +17,10 @@ export class PmApiError extends Error { this.details = body as typeof this.details } } +/** 区分确定的访问失效与短暂连接失败;静默同步仍须清除已撤权对象,不能沿用旧权限。 */ +export function isAccessFailure(error: unknown): boolean { + return error instanceof PmApiError && ['401', '403', '404', 'UNAUTHORIZED', 'FORBIDDEN', 'NOT_FOUND'].includes(error.code) +} /** 将业务原因转成简洁文本,保留受影响工作项和目标状态而不只显示 HTTP 错误码。 */ export function errorText(error: unknown): string { diff --git a/ruoyi-ui-vue3/src/views/project/manage/api/index.ts b/ruoyi-ui-vue3/src/views/project/manage/api/index.ts index 745106e..161f979 100644 --- a/ruoyi-ui-vue3/src/views/project/manage/api/index.ts +++ b/ruoyi-ui-vue3/src/views/project/manage/api/index.ts @@ -17,5 +17,5 @@ export const api = { ...notificationApi, } -export { PmApiError, errorText } from './errors' +export { PmApiError, errorText, isAccessFailure } from './errors' export { downloadAttachment } from './attachments' diff --git a/ruoyi-ui-vue3/src/views/project/manage/composables/useProjectCollaboration.ts b/ruoyi-ui-vue3/src/views/project/manage/composables/useProjectCollaboration.ts index 19c3fcb..dc541d2 100644 --- a/ruoyi-ui-vue3/src/views/project/manage/composables/useProjectCollaboration.ts +++ b/ruoyi-ui-vue3/src/views/project/manage/composables/useProjectCollaboration.ts @@ -2,13 +2,16 @@ import { computed, onMounted, onBeforeUnmount, onActivated, onDeactivated, react import { useRoute, useRouter } from 'vue-router' import { ElMessage, ElMessageBox } from 'element-plus' import useUserStore from '@/store/modules/user' -import { api, errorText } from '../api' +import { api, errorText, isAccessFailure } from '../api' import { plannedDate, planBoundary } from '../shared/utils/time' +import { reconcileRows, retainSnapshot } from '../shared/utils/snapshot' import type { Project, Member, WorkItem, ItemType, Lifecycle, FlowView, Iteration, ItemQuery } from '../types' import { iterationReadOnly, iterationCanExecute } from '../features/iterations/utils/scope' import type IterationControls from '../features/iterations/components/IterationControls.vue' import type WorkFlow from '../views/flow/WorkFlow.vue' import type WorkGantt from '../views/gantt/WorkGantt.vue' +import type WorkBoard from '../views/board/WorkBoard.vue' +import type ItemDetail from '../features/work-items/components/ItemDetail.vue' /** * 协调项目协作页面的项目/迭代范围、加载、路由及编辑状态。 @@ -16,6 +19,7 @@ import type WorkGantt from '../views/gantt/WorkGantt.vue' * 防止拆分视图后出现重复轮询或旧迭代响应覆盖新范围。 * 首次进入默认展示排在首位的流程视图,使项目阶段进度优先可见。 * 甘特成员选择仅作为读取条件,切换迭代清空,继续由后端完整图校验排期。 + * 轮询静默合并变化的快照,保留选择和已加载分页;编辑及拖动期间不提交后台结果。 * @returns 页面装配使用的响应式状态、组件引用和业务动作;解构后仍保持 Ref。 * 注意:必须在页面 setup 内调用;卸载时清除定时器并使在途刷新失效。 */ @@ -32,6 +36,8 @@ export function useProjectCollaboration() { const workFlow = useTemplateRef>('workFlow') const workGantt = useTemplateRef>('workGantt') + const workBoard = useTemplateRef>('workBoard') + const itemDetail = useTemplateRef>('itemDetail') const current = ref(null) const sourceProjectId = ref(requestedSource) @@ -95,93 +101,128 @@ export function useProjectCollaboration() { let refreshSequence = 0, workspaceSequence = 0, navigationSequence = 0, iterationSequence = 0, initializationSequence = 0 let timer: ReturnType | undefined let active = true, initialized = false, initializing = false, restoringRoute = false + let polling = false, applyingBackgroundMembers = false - /** 只读取路由明确指定的项目;旧链接经服务鉴权解析绑定,不回退选择其他项目。 */ - async function loadWorkspace(initialize = false) { + /** 固定读取路由项目;后台结果仅在页面仍空闲时应用,同内容沿用旧引用以避免重绘。 */ + async function loadWorkspace(initialize = false, background = false) { const sequence = ++workspaceSequence let context: Project if (sourceProjectId.value) { context = initialize ? await api.openProject(sourceProjectId.value) : await api.projectContext(sourceProjectId.value) } else if (projectId.value) context = await api.legacyProjectContext(projectId.value) else throw new Error('项目链接缺少项目标识') - if (sequence !== workspaceSequence || !active) return false - current.value = context + if (sequence !== workspaceSequence || !active || background && !canSynchronize()) return false + current.value = retainSnapshot(current.value, context) projectId.value = context.id sourceProjectId.value = context.sourceProjectId return true } - /** 轮询重读固定项目的身份和迭代;失败时清除详情、草稿及旧权限,不显示其他项目。 */ + /** 手动重读固定项目身份和迭代,保留显式加载及失败提示,不接受模板事件作为同步选项。 */ async function refreshWorkspace() { + await synchronizeWorkspace(false) + } + + /** + * 协调项目身份、迭代和数据刷新;后台瞬时失败留待下一次重试,访问失效仍立即清理。 + * @param background 是否来自后台轮询;此路径不显示遮罩,也不能覆盖后来发起的前台操作。 + */ + async function synchronizeWorkspace(background: boolean) { if (!active || restoringRoute) return if (!initialized) { if (!initializing) await initializeWorkspace() return } let navigation = navigationSequence + const refreshAtStart = refreshSequence try { const iteration = iterationId.value - if (!await loadWorkspace() || navigation !== navigationSequence) return - if (!await loadIterations() || navigation !== navigationSequence) return + if (!await loadWorkspace(false, background) || navigation !== navigationSequence || background && refreshAtStart !== refreshSequence) return + if (!await loadIterations(undefined, background) || navigation !== navigationSequence || background && refreshAtStart !== refreshSequence) return if (iteration !== iterationId.value) { const people = members.value clearScope() navigation = navigationSequence members.value = people } - await refresh() + await refreshData(background) } catch (error) { if (!active || navigation !== navigationSequence) return - clearScope() - current.value = null - iterations.value = [] - iterationId.value = null - failure.value = errorText(error) + if (!background || isAccessFailure(error)) invalidateWorkspace(error) } } + /** 权限、对象或会话明确失效时移除旧内容和草稿,后台同步也不能保留已失效的访问资格。 */ + function invalidateWorkspace(error: unknown) { + clearScope() + current.value = null + iterations.value = [] + iterationId.value = null + failure.value = errorText(error) + } + /** 分页筛选交由服务端;甘特独有的成员条件不传给列表、看板或前置选择器。 */ function query(view: ItemQuery['view']): ItemQuery { return { view, page: page.value, pageSize, keyword: keyword.value, type: typeFilter.value, blockedOnly: blockedOnly.value, ...(view === 'gantt' ? { from: ganttFrom.value, to: ganttTo.value, memberIds: [...ganttMemberIds.value] } : {}) } } - /** 只接受当前项目和迭代代次的结果,流程画布仅请求阶段摘要。 */ + /** 用户主动刷新或业务操作完成时显示加载状态;独立包装避免分页事件的页码被当作后台标志。 */ async function refresh() { + await refreshData(false) + } + + /** + * 只合并当前范围、请求代次的结果,流程仅请求阶段摘要,后台保留选择和看板已加载页数。 + * @param background 后台刷新不显示加载状态,瞬时失败不清空已有内容,编辑开始后丢弃其响应。 + */ + async function refreshData(background: boolean) { const id = projectId.value, iteration = iterationId.value, sequence = ++refreshSequence if (!id || !iteration || !current.value?.enabled) { - loading.value = false + if (!background) loading.value = false return } - loading.value = true - failure.value = '' + if (!background) { loading.value = true; failure.value = '' } try { const states: Lifecycle[] = ['READY', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'CLOSED'] - const request = tab.value === 'board' ? Promise.all(states.map(status => api.items(id, iteration, { ...query('board'), page: 1, status }))) : ['flow', 'notifications'].includes(tab.value) ? Promise.resolve([]) : Promise.all([api.items(id, iteration, query(tab.value === 'gantt' ? 'gantt' : 'list'))]) + const loadedPages = Object.fromEntries(states.map(status => [status, background ? boardPages[status] || 1 : 1])) + // 各列读取已加载的全部页再一起替换,避免轮询把“加载更多”的内容截回第一页。 + // 页数在等待期间变化则放弃本轮结果,让用户当前的续页优先完成。 + const request = tab.value === 'board' ? Promise.all(states.map(async status => { + const results = await Promise.all(Array.from({ length: loadedPages[status] }, (_, index) => api.items(id, iteration, { ...query('board'), page: index + 1, status }))) + return { rows: results.flatMap(result => result.rows), total: results[0]?.total || 0 } + })) : ['flow', 'notifications'].includes(tab.value) ? Promise.resolve([]) : Promise.all([api.items(id, iteration, query(tab.value === 'gantt' ? 'gantt' : 'list'))]) const [graph, people, notices, stats, pages] = await Promise.all([api.flowView(id, iteration), api.members(id), api.notifications(), api.statistics(id, iteration), request]) - if (sequence !== refreshSequence || id !== projectId.value || iteration !== iterationId.value) return - items.value = pages.flatMap(result => result.rows) + if (!active || sequence !== refreshSequence || id !== projectId.value || iteration !== iterationId.value || background && (!canSynchronize() || tab.value === 'board' && states.some(status => loadedPages[status] !== (boardPages[status] || 1)))) return + items.value = reconcileRows(items.value, pages.flatMap(result => result.rows)) total.value = pages[0]?.total || 0 if (tab.value === 'board') { - boardCounts.value = Object.fromEntries(states.map((status, index) => [status, pages[index]?.total || 0])) - for (const status of states) boardPages[status] = 1 + boardCounts.value = retainSnapshot(boardCounts.value, Object.fromEntries(states.map((status, index) => [status, pages[index]?.total || 0]))) + if (!background) for (const status of states) boardPages[status] = 1 } - members.value = people - flowView.value = graph - iterations.value = iterations.value.map(row => row.id === iteration ? { ...row, graphVersion: graph.graphVersion ?? row.graphVersion } : row) - statistics.value = stats - notifications.value = notices - selection.value = [] - refreshKey.value++ + updateMembers(people, background) + flowView.value = retainSnapshot(flowView.value, graph) + iterations.value = retainSnapshot(iterations.value, iterations.value.map(row => row.id === iteration ? { ...row, graphVersion: graph.graphVersion ?? row.graphVersion } : row)) + statistics.value = retainSnapshot(statistics.value, stats) + notifications.value = retainSnapshot(notifications.value, notices) + failure.value = '' + if (background) { + const available = new Map(items.value.map(row => [row.id, row])) + // 保留有效勾选,但同步其版本;删除或离开当前筛选范围的行不能继续参与批量操作。 + selection.value = retainSnapshot(selection.value, selection.value.flatMap(row => available.get(row.id) ? [available.get(row.id)!] : [])) + await itemDetail.value?.synchronize() + } else { selection.value = []; refreshKey.value++ } } catch (error) { - if (sequence === refreshSequence) { + if (sequence === refreshSequence && active && isAccessFailure(error)) invalidateWorkspace(error) + else if (sequence === refreshSequence && !background) { failure.value = errorText(error) items.value = [] + selection.value = [] flowView.value = null statistics.value = {} detailId.value = null } } finally { - if (sequence === refreshSequence) loading.value = false + if (!background && sequence === refreshSequence) loading.value = false } } @@ -225,6 +266,7 @@ export function useProjectCollaboration() { statistics.value = {} boardCounts.value = {} boardBusy.clear() + for (const status of Object.keys(boardPages) as Lifecycle[]) delete boardPages[status] flowView.value = null selection.value = [] members.value = [] @@ -239,11 +281,12 @@ export function useProjectCollaboration() { inviteUser.value = null userCandidates.value = [] failure.value = '' + loading.value = false page.value = 1 } - /** 选择可见迭代并返回本次结果是否仍有效;元数据请求独立计次,失效调用者不得继续刷新视图。 */ - async function loadIterations(preferred?: number | null) { + /** 可见迭代元数据独立计次;后台同值不重置引用,用户开始编辑或切换范围后不再应用结果。 */ + async function loadIterations(preferred?: number | null, background = false) { const id = projectId.value, sequence = ++iterationSequence, navigation = navigationSequence if (!id) { iterations.value = [] @@ -251,13 +294,20 @@ export function useProjectCollaboration() { return false } const [result, people] = await Promise.all([api.iterations(id, includeArchived.value), api.members(id)]) - if (!active || id !== projectId.value || sequence !== iterationSequence || navigation !== navigationSequence) return false - iterations.value = result - members.value = people + if (!active || id !== projectId.value || sequence !== iterationSequence || navigation !== navigationSequence || background && !canSynchronize()) return false + iterations.value = retainSnapshot(iterations.value, result) + updateMembers(people, background) iterationId.value = (result.find(row => row.id === (preferred || iterationId.value)) || result.find(row => row.status === 'ACTIVE' && !row.archivedAt) || result.find(row => !row.archivedAt) || result[0])?.id || null return true } + /** 成员失效时修剪甘特筛选,后台元数据更新不应间接触发带遮罩的前台刷新。 */ + function updateMembers(people: Member[], background: boolean) { + applyingBackgroundMembers = background + try { members.value = retainSnapshot(members.value, people) } + finally { applyingBackgroundMembers = false } + } + /** 同项目切换迭代同样清理草稿并失效在途请求。 */ async function changeIteration(id: number) { if (id === iterationId.value) return @@ -338,34 +388,38 @@ export function useProjectCollaboration() { return plannedDate(iso, zone) } - /** 开始只使用统一后端接口;拒绝后刷新真实状态。 */ + /** 开始只使用统一后端接口;提交期间暂停轮询并防止重复点击,拒绝后刷新真实状态。 */ async function startOne(item: WorkItem) { - if (!canExecute.value) return + if (!canExecute.value || busy.value) return + busy.value = true try { await api.start(item.id, item.version) await refresh() } catch (error) { ElMessage({ type: 'error', message: errorText(error), duration: 8000 }) await refresh() - } + } finally { busy.value = false } } - /** 逐项开始明确报告部分成功,范围切换后停止继续提交。 */ + /** 逐项开始明确报告部分成功,整批提交期间暂停轮询,范围切换后停止继续提交。 */ async function startSelection() { - if (!canExecute.value) return + if (!canExecute.value || busy.value) return + busy.value = true const iteration = iterationId.value, errors: string[] = [] let succeeded = 0 - for (const item of [...selection.value]) { - if (iteration !== iterationId.value || !canExecute.value) break - try { - await api.start(item.id, item.version) - succeeded++ - } catch (error) { - errors.push(`${item.title}:${errorText(error)}`) + try { + for (const item of [...selection.value]) { + if (iteration !== iterationId.value || !canExecute.value) break + try { + await api.start(item.id, item.version) + succeeded++ + } catch (error) { + errors.push(`${item.title}:${errorText(error)}`) + } } - } - await refresh() - ElMessage({ type: errors.length ? 'warning' : 'success', message: `已开始 ${succeeded} 项${errors.length ? `\n${errors.join('\n')}` : ''}`, duration: 10000 }) + await refresh() + ElMessage({ type: errors.length ? 'warning' : 'success', message: `已开始 ${succeeded} 项${errors.length ? `\n${errors.join('\n')}` : ''}`, duration: 10000 }) + } finally { busy.value = false } } /** 用户搜索结果仅回填当前项目,避免跨项目显示旧候选。 */ @@ -409,9 +463,17 @@ export function useProjectCollaboration() { if (canWrite.value) batchOpen.value = true } - /** 有未提交编辑时暂停轮询,避免破坏用户输入。 */ - function poll() { - if (active && initialized && !restoringRoute && document.visibilityState === 'visible' && !loading.value && !editorOpen.value && !batchOpen.value && !busy.value && !membersOpen.value && !iterationControls.value?.isEditing && !workFlow.value?.isEditing && !workGantt.value?.isEditing) void refreshWorkspace() + /** 后台请求开始和提交时都检查交互状态,防止请求途中打开的编辑或拖动被旧快照打断。 */ + function canSynchronize() { + return active && initialized && !restoringRoute && document.visibilityState === 'visible' && !loading.value && !editorOpen.value && !batchOpen.value && !busy.value && !membersOpen.value && boardBusy.size === 0 && !iterationControls.value?.isEditing && !workFlow.value?.isEditing && !workGantt.value?.isEditing && !workBoard.value?.isEditing && !itemDetail.value?.isEditing + } + + /** 串行执行十五秒静默同步;网络超过一个周期也不叠加请求,前台操作仍可使在途结果失效。 */ + async function poll() { + if (polling || !canSynchronize()) return + polling = true + try { await synchronizeWorkspace(true) } + finally { polling = false } } watch([tab, keyword, typeFilter, blockedOnly], () => { @@ -420,10 +482,10 @@ export function useProjectCollaboration() { }) /** 成员多选按并集重新查询第一页,防止只在已加载50项里筛选或沿用旧页码。 */ - watch(ganttMemberIds, () => { if (tab.value === 'gantt') { page.value = 1; void refresh() } }, { deep: true }) + watch(ganttMemberIds, () => { if (tab.value === 'gantt') { page.value = 1; if (!applyingBackgroundMembers) void refresh() } }, { deep: true, flush: 'sync' }) /** 轮询更新项目成员后移除失效选择;保留仍有效的选择,避免列表退化成不可识别的用户ID。 */ - watch(members, people => { const available = new Set(people.map(member => member.userId)); const retained = ganttMemberIds.value.filter(id => available.has(id)); if (retained.length !== ganttMemberIds.value.length) ganttMemberIds.value = retained }) + watch(members, people => { const available = new Set(people.map(member => member.userId)); const retained = ganttMemberIds.value.filter(id => available.has(id)); if (retained.length !== ganttMemberIds.value.length) ganttMemberIds.value = retained }, { flush: 'sync' }) watch(detailId, () => { // 接受外部深链接时,清空旧抽屉不能把尚未加载完成的新迭代路由回写为旧范围。 diff --git a/ruoyi-ui-vue3/src/views/project/manage/features/work-items/components/ItemDetail.vue b/ruoyi-ui-vue3/src/views/project/manage/features/work-items/components/ItemDetail.vue index fd0407a..b0c8ead 100644 --- a/ruoyi-ui-vue3/src/views/project/manage/features/work-items/components/ItemDetail.vue +++ b/ruoyi-ui-vue3/src/views/project/manage/features/work-items/components/ItemDetail.vue @@ -72,7 +72,8 @@ diff --git a/ruoyi-ui-vue3/src/views/project/manage/views/flow/utils/graph.ts b/ruoyi-ui-vue3/src/views/project/manage/views/flow/utils/graph.ts index ff0c1a0..2675d9a 100644 --- a/ruoyi-ui-vue3/src/views/project/manage/views/flow/utils/graph.ts +++ b/ruoyi-ui-vue3/src/views/project/manage/views/flow/utils/graph.ts @@ -1,5 +1,5 @@ import type { Node, Edge } from '@vue-flow/core' -import { MarkerType } from '@vue-flow/core' +import { ConnectionLineType, MarkerType } from '@vue-flow/core' import { FLOW_REWORK_COLOR } from '../constants/theme' import type { FlowView, Point, WorkItem } from '../../../types' @@ -19,6 +19,7 @@ export function workItemHeight(item: WorkItem, childCount = 0): number { const c /** * 只以阶段及显式阶段边组成画布,工作项留在按当前版本分组的抽屉中。 * 阶段大小固定且数量取完整服务端快照;搜索或筛选不会改变流程拓扑和显示总数。 + * 执行连线使用内置三次贝塞尔曲线,回退线继续走独立历史通道并保留状态样式。 * @param view 已通过项目成员鉴权的当前版本流程快照 * @param positions 当前浏览器保存的阶段坐标,旧工作项坐标自然忽略 * @return 只有阶段节点及阶段间连线的画布数据 @@ -69,7 +70,7 @@ export function buildFlowGraph(view: FlowView, positions: Record) return { id: 'stage:' + link.id, source: 's:' + link.predecessorStageId, target: 's:' + link.successorStageId, sourceHandle: 'out', targetHandle: 'in', // 颜色只编码后端当前执行事实,迁移放行与真正完成必须有不同视觉语义。 - type: 'smoothstep', updatable: false, selectable: false, markerEnd: { type: MarkerType.ArrowClosed, color }, + type: ConnectionLineType.Bezier, updatable: false, selectable: false, markerEnd: { type: MarkerType.ArrowClosed, color }, style: { stroke: color, strokeWidth: link.satisfied ? 2.4 : 1.8, strokeDasharray: link.legacyPassed ? '6 4' : undefined }, data: { linkId: link.id, version: link.version }, } diff --git a/ruoyi-ui-vue3/src/views/project/manage/views/list/WorkItemList.vue b/ruoyi-ui-vue3/src/views/project/manage/views/list/WorkItemList.vue index 6e56455..871215c 100644 --- a/ruoyi-ui-vue3/src/views/project/manage/views/list/WorkItemList.vue +++ b/ruoyi-ui-vue3/src/views/project/manage/views/list/WorkItemList.vue @@ -6,16 +6,19 @@ @click="emit('startSelection')" >开始所选(逐项校验)