From ad65f0cc75f54d0c4af15195421364fe839da8fe Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Thu, 8 Oct 2026 15:31:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E4=BB=BB=E5=8A=A1=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=8C=E5=8F=96=E6=B6=88=E3=80=8D=EF=BC=88?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=A3=80=E6=B5=8B=E5=8F=96=E6=B6=88=E6=A0=87?= =?UTF-8?q?=E8=AE=B0=E5=90=8E=E5=81=9C=E6=AD=A2=E5=B9=B6=E5=88=A0=E5=8D=8A?= =?UTF-8?q?=E6=88=90=E5=93=81=E6=96=87=E4=BB=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/RunAccountBillsExportTask.php | 16 ++++++++++++++++ .../Http/Controllers/AccountBills.php | 17 +++++++++++++++++ .../Services/AccountBillsExportTasks.php | 2 ++ .../js/components/business/AccountBills.vue | 16 ++++++++++++++-- .../components/business/TaobaoAlipayBills.vue | 16 ++++++++++++++-- routes/admin.php | 2 ++ 6 files changed, 65 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/RunAccountBillsExportTask.php b/app/Console/Commands/RunAccountBillsExportTask.php index 8c44d253..a18bf71a 100644 --- a/app/Console/Commands/RunAccountBillsExportTask.php +++ b/app/Console/Commands/RunAccountBillsExportTask.php @@ -40,6 +40,12 @@ class RunAccountBillsExportTask extends Command $task = $tasks->patch($task, ['status' => 'running', 'started_at' => AccountBillsExportTasks::beijingNow()]); try { + if (! empty($task['cancel'])) { + $tasks->patch($task, ['status' => 'cancelled', 'finished_at' => AccountBillsExportTasks::beijingNow()]); + $this->info('任务已取消。'); + + return self::SUCCESS; + } $columns = array_values((array) ($task['columns'] ?? [])); if ($columns === []) { throw new \RuntimeException('导出任务没有可用字段。'); @@ -62,6 +68,16 @@ class RunAccountBillsExportTask extends Command $written++; if ($written % 2000 === 0) { $task = $tasks->patch($task, ['status' => 'running', 'rows' => $written]); + // 用户在弹窗里点了「取消」:停止导出并删掉半成品文件。 + $fresh = $tasks->find((int) $task['user_id'], (string) $task['token']); + if (! empty($fresh['cancel'])) { + fclose($csv); + @unlink($tasks->csvPath($task)); + $tasks->patch($task, ['status' => 'cancelled', 'rows' => $written, 'finished_at' => AccountBillsExportTasks::beijingNow(), 'size' => 0]); + $this->info('任务已取消(已写入 ' . $written . ' 行后停止)。'); + + return self::SUCCESS; + } } } fclose($csv); diff --git a/modules/EcommerceFinance/Http/Controllers/AccountBills.php b/modules/EcommerceFinance/Http/Controllers/AccountBills.php index 89dd2a58..a3361f78 100644 --- a/modules/EcommerceFinance/Http/Controllers/AccountBills.php +++ b/modules/EcommerceFinance/Http/Controllers/AccountBills.php @@ -439,6 +439,23 @@ class AccountBills extends Controller return response()->download($path, $name, ['Content-Type' => 'text/csv; charset=UTF-8']); } + /** 取消导出任务:写入取消标记,后台进程检测到后停止并删除半成品文件。 */ + public function cancelExportTask(Request $request, string $token) + { + $this->authorizeStoreReceivingBills(); + + $tasks = app(\Modules\EcommerceFinance\Services\AccountBillsExportTasks::class); + $task = $tasks->find((int) user()->id, $token); + abort_if($task === null, 404, '导出任务不存在或已过期。'); + + $status = (string) ($task['status'] ?? ''); + abort_if(in_array($status, ['done', 'failed', 'cancelled'], true), 409, '该任务已结束,无需取消。'); + + $tasks->patch($task, ['cancel' => true]); + + return response()->json(['status' => 'ok'], 200, ['Cache-Control' => 'private, no-store']); + } + /** * 该页签可导出的字段:field => label。 * diff --git a/modules/EcommerceFinance/Services/AccountBillsExportTasks.php b/modules/EcommerceFinance/Services/AccountBillsExportTasks.php index 5beb8ce1..7c1b2048 100644 --- a/modules/EcommerceFinance/Services/AccountBillsExportTasks.php +++ b/modules/EcommerceFinance/Services/AccountBillsExportTasks.php @@ -66,6 +66,7 @@ class AccountBillsExportTasks 'started_at' => null, 'finished_at' => null, 'size' => 0, + 'cancel' => false, ]; $this->write($task); @@ -156,6 +157,7 @@ class AccountBillsExportTasks 'total' => (int) ($task['total'] ?? 0), 'size' => (int) ($task['size'] ?? 0), 'error' => $task['error'] ?? null, + 'cancel' => (bool) ($task['cancel'] ?? false), 'planned_at' => $task['planned_at'] ?? null, 'started_at' => $task['started_at'] ?? null, 'finished_at' => $task['finished_at'] ?? null, diff --git a/resources/assets/js/components/business/AccountBills.vue b/resources/assets/js/components/business/AccountBills.vue index 679b2f56..83369f15 100644 --- a/resources/assets/js/components/business/AccountBills.vue +++ b/resources/assets/js/components/business/AccountBills.vue @@ -82,7 +82,9 @@