123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Admin\Exports;
- use Encore\Admin\Grid\Exporters\AbstractExporter;
- use Illuminate\Contracts\View\View;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Storage;
- abstract class ViewExport extends AbstractExporter
- {
- /**
- * @var Collection
- */
- private $customerData = null;
- /**
- * @return mixed
- */
- public function getCustomerData()
- {
- return $this->customerData;
- }
- /**
- * @param Collection $customerData
- * @return ViewExport
- */
- public function setCustomerData(Collection $customerData)
- {
- $this->customerData = $customerData;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function export()
- {
- $temp_path=$this->renderFile();
- response()->download(Storage::disk('public')
- ->path($temp_path), $this->getFilename())
- ->deleteFileAfterSend(true)
- ->send();
- exit;
- }
- public function store()
- {
- return $this->renderFile();
- }
- private function renderFile()
- {
- $view = $this->getView($this->getCustomerData()?:$this->getData(false));
- $temp_path="temp/".$this->getFilename();
- Storage::disk('public')->put($temp_path, $view->render());
- return $temp_path;
- }
- /**
- * 设置文件名
- * @return string
- */
- abstract public function getFilename(): string ;
- /**
- * 渲染对应的视图
- * @param Collection $data 导出的数据
- * @return View
- */
- abstract public function getView(Collection $data): View ;
- }
|