我如何在 Laravel 中使用 Inertia.js 构建多部门工作流路由系统

发布: (2026年5月3日 GMT+8 21:54)
3 分钟阅读
原文: Dev.to

Source: Dev.to

我想要解决的问题

当我开始构建 VMMS —— 一个凭证管理系统时,最大的问题是工作流路由。
一条凭证请求不会只发送到一个部门;它会经过多个部门。任何时候,部门都可以:

  • 完成自己的步骤并将请求向前传递
  • 拒绝整个请求
  • 标记缺少文件并暂停处理

我需要一个能够干净地处理这些情况的系统。

可配置的处理流程

每种凭证类型都有一个可配置的处理流程,存储在 transaction_flows 表中:

Schema::create('transaction_flows', function (Blueprint $table) {
    $table->id();
    $table->foreignId('voucher_id')->constrained()->cascadeOnDelete();
    $table->string('department');
    $table->integer('order_number');
    $table->timestamps();
});

当客户端提交请求时,系统读取此表来确定部门的顺序。

审计轨迹

每当一个部门处理请求时,都会在 audit_trails 表中创建一条审计记录:

Schema::create('audit_trails', function (Blueprint $table) {
    $table->id();
    $table->foreignId('transaction_id')->constrained()->cascadeOnDelete();
    $table->string('processing_offices');
    $table->timestamp('process_initiate')->nullable();
    $table->timestamp('process_accomplished')->nullable();
    $table->date('deadline')->nullable();
    $table->timestamps();
});
  • process_initiate 在部门开始处理时设置。
  • process_accomplished 在部门完成时设置。

确定当前步骤

查找活动部门(或下一个部门)的逻辑如下:

$activeAudit = $auditTrails->first(
    fn($a) => $a->process_initiate && !$a->process_accomplished
);

if ($activeAudit) {
    return $activeAudit->processing_offices;
}

// If no active audit, find the next step
$doneOrders = $auditTrails
    ->filter(fn($a) => $a->process_accomplished)
    ->map(fn($a) => $flow->firstWhere('department', $a->processing_offices)?->order_number ?? 0);

$lastDone = $doneOrders->max() ?? 0;
$nextStep = $flow->firstWhere('order_number', $lastDone + 1);

return $nextStep?->department ?? 'Completed';

处理的边缘情况

  • 跳过的部门 – 代码会查找下一个顺序号。
  • 缺少文件 – 请求会暂停,直到提供所需文件。
  • 最后一个部门完成 – 返回 'Completed'

Vue 3 步进器组件

在前端,我构建了一个步进器组件来可视化进度:

const stepStatus = (req, step, stepIndex) => {
    const audits = req.audit_trails ?? [];
    const accomplishedCount = audits.filter(
        a => a.process_accomplished !== null
    ).length;

    if (stepIndex whereNull('resume_transaction')
    ->pluck('transaction_id')
    ->toArray();

resume_transaction 被填入后,工作流会自动恢复。

关键要点

  1. 将流程模型与事务分离 —— 将 transaction_flowsvoucher_transactions 分开,使得修改路由逻辑变得容易。
  2. 审计轨迹是必不可少的 —— 它们为当前状态和历史提供可靠的真相来源。
  3. 为边缘情况做好规划 —— 跳过的步骤、文件暂停以及最终完成都需要显式处理。

演示与获取方式

  • 在线演示: 🔴
  • 购买 VMMS: 在 Gumroad 上可获取 –

欢迎在评论中提问!

0 浏览
Back to Blog

相关文章

阅读更多 »

在 Laravel 中集成 SB Admin 2

Laravel 11 要求 bash php -v >= 8.2 composer -v node -v >= v14.16 npm -v 在你的 web 服务器上启动 Apache 与 MySQL。 安装 Laravel 11 bash composer create…

让客户交接轻松的文件夹结构

每家机构都有这样一个版本的故事:团队成员离职、客户升级,或者你在替病假的同事顶班——于是你花了20分钟去搜索……