diff --git a/deploy/activate-incremental-release.sh b/deploy/activate-incremental-release.sh index 81457cf9..21e8f1ca 100644 --- a/deploy/activate-incremental-release.sh +++ b/deploy/activate-incremental-release.sh @@ -249,7 +249,7 @@ chmod 644 /etc/cron.d/caiwu-jiansun-snapshots cat > /etc/cron.d/caiwu-ding-process-details <<'EOF' CRON_TZ=Asia/Shanghai 2-59/5 * * * * root CAIWU_BATCH_LOCK_WAIT=900 /usr/bin/flock -n /run/caiwu-ding-process-details.lock /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-ding-process-details --quiet >/dev/null 2>&1 -# 页面打开时只提交当前页审批中的流程;有请求时才启动定向刷新批处理。 +# OA 页面每小时最多提交一次当前页审批中的流程;有请求时才启动定向刷新批处理。 * * * * * root CAIWU_BATCH_LOCK_WAIT=900 /usr/bin/flock -n /run/caiwu-ding-process-refresh.lock /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:process-ding-refresh-requests --quiet >/dev/null 2>&1 EOF chmod 644 /etc/cron.d/caiwu-ding-process-details diff --git a/deploy/activate-release.sh b/deploy/activate-release.sh index 789529e8..a2e595ac 100644 --- a/deploy/activate-release.sh +++ b/deploy/activate-release.sh @@ -253,7 +253,7 @@ chmod 644 /etc/cron.d/caiwu-jiansun-snapshots cat > /etc/cron.d/caiwu-ding-process-details <<'EOF' CRON_TZ=Asia/Shanghai 2-59/5 * * * * root CAIWU_BATCH_LOCK_WAIT=900 /usr/bin/flock -n /run/caiwu-ding-process-details.lock /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-ding-process-details --quiet >/dev/null 2>&1 -# 页面打开时只提交当前页审批中的流程;有请求时才启动定向刷新批处理。 +# OA 页面每小时最多提交一次当前页审批中的流程;有请求时才启动定向刷新批处理。 * * * * * root CAIWU_BATCH_LOCK_WAIT=900 /usr/bin/flock -n /run/caiwu-ding-process-refresh.lock /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:process-ding-refresh-requests --quiet >/dev/null 2>&1 EOF chmod 644 /etc/cron.d/caiwu-ding-process-details diff --git a/docs/上游业务镜像清单.md b/docs/上游业务镜像清单.md index de923552..28cbba3a 100644 --- a/docs/上游业务镜像清单.md +++ b/docs/上游业务镜像清单.md @@ -35,7 +35,7 @@ | 待有效凭据后首次同步 | 普通商品当前成本价、商品类型和更新时间继续读取 `operations_replica.rpt.jst_sku`;`rpt.fys_skuCostPriceUpdate` 是一 SKU 一行的旧快照,不能充当变价历史。完整成本价变更仍读取聚水潭经典开放平台“商品操作日志查询”接口 `worklog.query`,只镜像操作内容含“采购价”的记录到 `jushuitan_cost_price_history_rows`;经典接口必须在服务器私有配置中使用同一应用的 `JUSHUITAN_PARTNER_ID / JUSHUITAN_PARTNER_KEY / JUSHUITAN_TOKEN`,不得把新开放平台的 App Key / App Secret / Access Token 混用。页面在首次同步完成前明确显示待同步或失败,不把 0 条解释为真实无历史。 | | 本次接入 | 流程明细读取 `operations_replica/rpt.ding_Workflow + rpt.ding_WorkflowScheme` 的结构化审批主表、费用明细和付款明细,写入 `ding_process_detail_mirror` 与 `ding_process_payment_mirror`。禁止先把明细表和付款表铺平成 CSV 后逐行求和;付款明细必须按“对应序号”关联到代表明细行,但合计始终读取独立付款事实。每个纳入范围的源审批至少保留一条页面记录,没有费用金额的审批以空金额显示,不静默隐藏。首次全量先运行 `business-mirror:sync-ding-process-details --full --dry-run` 校验,再使用 `business-mirror:sync-ding-process-details --full` 发布;日常通过 `updateAt/createAt` 48 小时重叠增量同步。当前纳入日常报销、应付、线上代发、种菜、快递面单、固定资产、日常物品、样品、一次性采买、板材、工资、外贸费用和种菜充值等 14 个财务流程;发现新的明细流程编码时全量同步报错并保留上一版。 | -流程明细另设独立定时任务,避免与每分钟科目映射消费争用同一上游目录时被静默跳过。正式发布脚本会覆盖安装 `/etc/cron.d/caiwu-ding-process-details`:每 5 分钟尝试运行 `business-mirror:sync-ding-process-details`,使用独立调度锁并在共用上游锁上最多等待 15 分钟;`scripts/install-ding-process-sync-cron.ps1` 仅作为手工修复入口。任务时间和镜像状态统一使用平台固定时区,审批等待时长按实际小时展示。打开 OA 页面时只把当前页“审批中”的审批编号写入定向刷新队列,由每分钟检查一次的批处理命令消费;已完成流程不进入即时刷新链路,仍由常规增量和每周全量校准处理付款等后续变化。增量同步除跟进审批主表更新时间外,还以 `ding_WorkflowScheme` 的数字 `id` 高水位发现后补表单行,避免付款明细等新组件写入后主审批 `updateAt` 不变而被漏掉;原地改写同一组件仍由每周全量校准兜底。付款状态只按钉钉“出纳打款”节点判断,分为“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”。金额差异不参与付款状态判断,另以“付款差额 = 费用合计 − 已付款合计”展示。不能因为金额存在少量差异就覆盖出纳节点事实,也不能因为出纳已通过就隐藏金额差异。 +流程明细另设独立定时任务,避免与每分钟科目映射消费争用同一上游目录时被静默跳过。正式发布脚本会覆盖安装 `/etc/cron.d/caiwu-ding-process-details`:每 5 分钟尝试运行 `business-mirror:sync-ding-process-details`,使用独立调度锁并在共用上游锁上最多等待 15 分钟;`scripts/install-ding-process-sync-cron.ps1` 仅作为手工修复入口。任务时间和镜像状态统一使用平台固定时区,审批等待时长按实际小时展示。OA 页面每小时最多把一次当前页“审批中”的审批编号写入定向刷新队列,由每分钟检查一次的批处理命令消费;其他时间直接展示最近镜像,已完成流程不进入即时刷新链路,仍由常规增量和每周全量校准处理付款等后续变化。增量同步除跟进审批主表更新时间外,还以 `ding_WorkflowScheme` 的数字 `id` 高水位发现后补表单行,避免付款明细等新组件写入后主审批 `updateAt` 不变而被漏掉;原地改写同一组件仍由每周全量校准兜底。付款状态只按钉钉“出纳打款”节点判断,分为“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”。金额差异不参与付款状态判断,另以“付款差额 = 费用合计 − 已付款合计”展示。不能因为金额存在少量差异就覆盖出纳节点事实,也不能因为出纳已通过就隐藏金额差异。 `operations_replica` 缺失付款组件或进行中流程需要刷新出纳节点时,同步器复用现有钉钉应用凭据调用 `https://api.dingtalk.com/v1.0/workflow/processInstances`。官方接口同时返回表单、`tasks.taskGroupName` 和 `operationRecords.showName`,用于识别“出纳打款”节点及退回/待处理/已通过状态。付款来源标记为 `api`,付款事实仍进入 `ding_process_payment_mirror`;不会用官方接口覆盖副本已有的业务明细,只补缺失或空的付款字段,并独立保存“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”,`cashier_checked_at` 用于记录近期已检查时间。自动增量每 5 分钟按 `process_instance_id` 游标轮转最多 50 个缺失或需要刷新出纳状态的流程,避免单个失败记录阻塞队列或瞬时打满接口;人工核查可执行 `business-mirror:sync-ding-process-details --business-id=审批编号1,审批编号2 --dry-run`,确认后再去掉 `--dry-run`。单次最多 50 个审批编号。 diff --git a/resources/views/business/pages/financial-operations/process-details-content.blade.php b/resources/views/business/pages/financial-operations/process-details-content.blade.php index 28344e02..3a979d46 100644 --- a/resources/views/business/pages/financial-operations/process-details-content.blade.php +++ b/resources/views/business/pages/financial-operations/process-details-content.blade.php @@ -903,6 +903,7 @@ data-oa-refresh-business-ids="{{ implode(',', $refreshBusinessIds) }}" data-oa-refresh-url="{{ $refreshUrl }}" data-oa-refresh-status-url="{{ $refreshStatusUrl }}" + data-oa-refresh-user-id="{{ auth()->id() }}" >
@@ -1437,10 +1438,18 @@ const refreshUrl = root.dataset.oaRefreshUrl || ''; const statusUrlTemplate = root.dataset.oaRefreshStatusUrl || ''; const statusElement = root.querySelector('[data-oa-refresh-state]'); - const pageKey = `oa-refresh-token:${window.location.pathname}${window.location.search}`; - const completedKey = `oa-refresh-completed:${window.location.pathname}${window.location.search}`; - const completedAt = Number(window.sessionStorage.getItem(completedKey) || 0); - if (completedAt && Date.now() - completedAt < 120000) return; + const refreshIntervalMs = 60 * 60 * 1000; + const pollIntervalMs = 2000; + const pollLimit = 90; + const storage = window.localStorage; + const storageScope = [ + window.location.pathname, + root.dataset.oaRefreshUserId || 'anonymous', + ].join(':'); + const tokenKey = `oa-refresh-token:${storageScope}`; + const attemptKey = `oa-refresh-attempt-at:${storageScope}`; + const completedKey = `oa-refresh-completed-at:${storageScope}`; + let nextReloadTimer = null; const setStatus = (message, isError = false) => { if (! statusElement) return; @@ -1453,14 +1462,30 @@ encodeURIComponent(token), ); + const scheduleNextReload = () => { + if (nextReloadTimer) window.clearTimeout(nextReloadTimer); + const lastAttemptAt = Number(storage.getItem(attemptKey) || 0); + const completedAt = Number(storage.getItem(completedKey) || 0); + const referenceAt = Math.max(lastAttemptAt, completedAt); + const remainingMs = referenceAt + ? Math.max(1000, refreshIntervalMs - (Date.now() - referenceAt)) + : refreshIntervalMs; + nextReloadTimer = window.setTimeout( + () => window.location.reload(), + remainingMs + 500, + ); + }; + const fail = (message, token = null) => { - if (token) window.sessionStorage.removeItem(pageKey); - setStatus(message, true); + if (token) storage.removeItem(tokenKey); + storage.setItem(attemptKey, String(Date.now())); + setStatus(message, false); + scheduleNextReload(); }; const poll = (token, attempt = 0) => { - if (attempt >= 90) { - fail('审批状态刷新仍在处理中,请稍后重新打开页面查看。'); + if (attempt >= pollLimit) { + fail('本次自动刷新暂未完成,本页继续显示最近镜像;1小时后自动重试。'); return; } @@ -1476,33 +1501,45 @@ .then((payload) => { const state = payload.refresh?.status; if (state === 'completed') { - window.sessionStorage.setItem(completedKey, String(Date.now())); - window.sessionStorage.removeItem(pageKey); - setStatus('当前页审批状态已刷新,正在载入最新数据…'); + const completedAt = String(Date.now()); + storage.setItem(attemptKey, completedAt); + storage.setItem(completedKey, completedAt); + storage.removeItem(tokenKey); + setStatus('本小时审批状态已刷新,正在载入最新数据…'); window.setTimeout(() => window.location.reload(), 400); return; } if (state === 'failed') { fail( - payload.refresh?.error || '当前页审批状态刷新失败,请稍后重试。', + '本次自动刷新暂未完成,本页继续显示最近镜像;1小时后自动重试。', token, ); return; } - setStatus('正在刷新当前页审批中的流程…'); - window.setTimeout(() => poll(token, attempt + 1), 2000); + setStatus('正在按小时刷新当前页审批中的流程…'); + window.setTimeout(() => poll(token, attempt + 1), pollIntervalMs); }) - .catch(() => fail('当前页审批状态刷新失败,请稍后重试。', token)); + .catch(() => fail( + '本次自动刷新暂未完成,本页继续显示最近镜像;1小时后自动重试。', + token, + )); }; - const existingToken = window.sessionStorage.getItem(pageKey); + const existingToken = storage.getItem(tokenKey); if (existingToken) { - setStatus('正在刷新当前页审批中的流程…'); + setStatus('正在按小时刷新当前页审批中的流程…'); poll(existingToken); return; } - setStatus('正在刷新当前页审批中的流程…'); + const lastAttemptAt = Number(storage.getItem(attemptKey) || 0); + if (lastAttemptAt && Date.now() - lastAttemptAt < refreshIntervalMs) { + scheduleNextReload(); + return; + } + + storage.setItem(attemptKey, String(Date.now())); + setStatus('正在按小时刷新当前页审批中的流程…'); const requestUrl = `${refreshUrl}&business_ids=${encodeURIComponent(businessIds.join(','))}`; window.fetch(requestUrl, { credentials: 'same-origin', @@ -1515,12 +1552,17 @@ }) .then((payload) => { if (! payload.queued || ! payload.token) { + const completedAt = String(Date.now()); + storage.setItem(completedKey, completedAt); setStatus(payload.message || '当前页没有需要刷新的进行中流程。'); + scheduleNextReload(); return; } - window.sessionStorage.setItem(pageKey, payload.token); + storage.setItem(tokenKey, payload.token); poll(payload.token); }) - .catch(() => fail('当前页审批状态刷新失败,请稍后重试。')); + .catch(() => fail( + '本次自动刷新暂未完成,本页继续显示最近镜像;1小时后自动重试。', + )); })(); diff --git a/tests/Feature/Business/ProcessDetailsPageTest.php b/tests/Feature/Business/ProcessDetailsPageTest.php index b05a4de7..fe22e178 100644 --- a/tests/Feature/Business/ProcessDetailsPageTest.php +++ b/tests/Feature/Business/ProcessDetailsPageTest.php @@ -125,6 +125,8 @@ class ProcessDetailsPageTest extends FeatureTestCase ->assertSee('已等待') ->assertSee('data-oa-refresh-url', false) ->assertSee('data-oa-refresh-status-url', false) + ->assertSee('data-oa-refresh-user-id', false) + ->assertSee('页面每小时最多定向刷新当前页审批中的流程一次', false) ->assertDontSee('当前节点') ->assertDontSee('出纳节点') ->assertDontSee('财务用途审批') diff --git a/tests/Unit/Deployment/BusinessMirrorScheduleTest.php b/tests/Unit/Deployment/BusinessMirrorScheduleTest.php index 49fabe8b..8bb5f6b3 100644 --- a/tests/Unit/Deployment/BusinessMirrorScheduleTest.php +++ b/tests/Unit/Deployment/BusinessMirrorScheduleTest.php @@ -146,7 +146,7 @@ class BusinessMirrorScheduleTest extends TestCase . "CRON_TZ=Asia/Shanghai\n" . '2-59/5 * * * * root CAIWU_BATCH_LOCK_WAIT=900 /usr/bin/flock -n /run/caiwu-ding-process-details.lock ' . "/bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-ding-process-details --quiet >/dev/null 2>&1\n" - . "# 页面打开时只提交当前页审批中的流程;有请求时才启动定向刷新批处理。\n" + . "# OA 页面每小时最多提交一次当前页审批中的流程;有请求时才启动定向刷新批处理。\n" . '* * * * * root CAIWU_BATCH_LOCK_WAIT=900 /usr/bin/flock -n /run/caiwu-ding-process-refresh.lock ' . "/bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:process-ding-refresh-requests --quiet >/dev/null 2>&1\n" . 'EOF', diff --git a/tests/Unit/EcommerceFinance/ProcessDetailsPageDataTest.php b/tests/Unit/EcommerceFinance/ProcessDetailsPageDataTest.php index bba2adb0..2c8bbf85 100644 --- a/tests/Unit/EcommerceFinance/ProcessDetailsPageDataTest.php +++ b/tests/Unit/EcommerceFinance/ProcessDetailsPageDataTest.php @@ -35,6 +35,14 @@ class ProcessDetailsPageDataTest extends TestCase $this->assertStringContainsString('$exportUrl ??=', $content); $this->assertStringContainsString('$statusClass ??=', $content); $this->assertStringNotContainsString('$processRows->links()', $content); + $this->assertStringContainsString('const refreshIntervalMs = 60 * 60 * 1000;', $content); + $this->assertStringContainsString('oa-refresh-attempt-at:', $content); + $this->assertStringContainsString('window.localStorage', $content); + $this->assertStringContainsString('scheduleNextReload', $content); + $this->assertStringNotContainsString( + '`oa-refresh-token:${window.location.pathname}${window.location.search}`', + $content, + ); } public function test_page_data_contains_real_paginator_filters_and_summary(): void