diff --git a/app/Controller/Admin/Tools.php b/app/Controller/Admin/Tools.php index feb2644..ebd41ca 100644 --- a/app/Controller/Admin/Tools.php +++ b/app/Controller/Admin/Tools.php @@ -3,6 +3,7 @@ namespace App\Controller\Admin; use App\Annotation\Auth; +use App\Model\Attachment as aModel; use App\Model\GenTable as gtModel; use App\Request\GenTable as gtRequest; use App\Model\Message as mModel; @@ -269,4 +270,22 @@ class Tools extends Base if (!$data['id']) return $this->error(); return $this->toAjax(mModel::edit(['message_id' => $data['id'], 'recalled_flag' => 1])); } + + #[GetMapping(path: "file/export")] + #[Auth(auth: "file:export")] + public function messageExport() + { + $param = Param::only(["module" => "demo", "name" => "数据导出", "params" => []]); + $res = aModel::add([ + 'account_id' => $this->accountId(), + 'name' => $param['name'], + 'module' => $param['module'], + 'params' => json_encode($param['params'], true), + 'status' => 0, + 'create_time' => date("Y-m-d H:i:s") + ]); + if (!$res) return $this->error("导出任务创建失败"); + QueueClient::push("App\Job\FileExportJob", ['messageId' => $res, 'channels' => ['websocket', 'email']]); + return $this->success("导出任务创建成功"); + } } \ No newline at end of file diff --git a/app/Job/FileExportJob.php b/app/Job/FileExportJob.php new file mode 100644 index 0000000..34d57eb --- /dev/null +++ b/app/Job/FileExportJob.php @@ -0,0 +1,46 @@ +attachmentId = $params['attachmentId']; + } + + public function handle() + { + try { + $attach = aModel::find($this->attachmentId); + if (empty($attach)) return false; + switch ($attach->module) { + case "post_export": + $res = pModel::exportData($attach->params); + $res = true; + break; + default: + throw new \Exception("导出模块不存在"); + } + aModel::edit([ + 'attachment_id' => $this->attachmentId, + 'status' => $res ? 1 : 2, + 'fail_reason' => $res ? "" : "导出失败" + ]); + } catch (\Exception $e) { + aModel::edit([ + 'attachment_id' => $this->attachmentId, + 'status' => 2, + 'fail_reason' => $e->getMessage() + ]); + } + return true; + } +} \ No newline at end of file diff --git a/app/Model/Attachment.php b/app/Model/Attachment.php new file mode 100644 index 0000000..0906340 --- /dev/null +++ b/app/Model/Attachment.php @@ -0,0 +1,39 @@ + 'integer', 'account_id' => 'integer', 'status' => 'integer']; +} diff --git a/app/Utils/Excel.php b/app/Utils/Excel.php new file mode 100644 index 0000000..b6426f9 --- /dev/null +++ b/app/Utils/Excel.php @@ -0,0 +1,65 @@ +setReadDataOnly(true); // 只读数据,忽略公式 + $reader->setReadEmptyCells(false); // 跳过空单元格处理 + $reader->setLoadSheetsOnly(["Sheet1"]); // 明确加载指定工作表 + $spreadsheet = $reader->load($file); + return $spreadsheet->getSheet(0)->toArray(); + } catch (Exception $e) { + return $e->getMessage(); + } + } + + /** + * 导出文件 + * Author: cfn + * @param $name + * @param $header + * @param $data + * @return false|string + */ + public static function exportData($name, $header, $data) + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->setTitle('sheet1'); + //表头 + $sheet->fromArray($header, '', 'A1'); + $sheet->fromArray($data, '', 'A2'); + $sheet->getDefaultColumnDimension()->setWidth(12); + $date = date("Ymd"); + $saveDir = BASE_PATH . '/static' . "/export/{$date}/"; + if (!is_dir($saveDir)) { + mkdir($saveDir, 0777, true); + } + $saveFile = "{$saveDir}{$name}.xlsx"; + //按照指定格式生成Excel文件 + $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); + $writer->save($saveFile); + if (is_file($saveFile)) { + return config("app.domain") . "xlsx/{$date}/{$name}.xlsx"; + } + return false; + } +}