You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
3.6 KiB
PHTML
185 lines
3.6 KiB
PHTML
2 years ago
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Helper;
|
||
|
|
||
|
use Closure;
|
||
|
use Hyperf\Database\Model\Collection;
|
||
|
|
||
|
class Export
|
||
|
{
|
||
|
const MODE_QUERY = 1;
|
||
|
const MODE_ARRAY = 2;
|
||
|
const MODE_COLLECTION = 3;
|
||
|
|
||
|
public $name;
|
||
|
|
||
|
protected $params;
|
||
|
protected $basePath = BASE_PATH . '/public';
|
||
|
protected $writerType = '.xlsx';
|
||
|
protected $fileName;
|
||
|
protected $filePath;
|
||
|
protected $fileUrl;
|
||
|
protected $diskOptions;
|
||
|
protected $list;
|
||
|
public $chunkCompareEnable = true;
|
||
|
|
||
|
public $withSummary = false;
|
||
|
|
||
|
public $chunkCompareOptions = [
|
||
|
'key' => 'id',
|
||
|
'symbol' => '<'
|
||
|
];
|
||
|
|
||
|
public function __construct(array $params = [])
|
||
|
{
|
||
|
$this->params = $params;
|
||
|
$this->init();
|
||
|
}
|
||
|
|
||
|
protected function init()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
public function query()
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public function array(): array
|
||
|
{
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
public function collection(): Collection
|
||
|
{
|
||
|
return new Collection([]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 导出时每行的数据
|
||
|
*/
|
||
|
public function map($item): array
|
||
|
{
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 导出时的表头
|
||
|
*/
|
||
|
public function headings(): array
|
||
|
{
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
public function summary(): array
|
||
|
{
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
public function rangeQueryItems($items)
|
||
|
{
|
||
|
return $items;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 导出时文件后缀
|
||
|
*/
|
||
|
public function getExtension()
|
||
|
{
|
||
|
/* $extensions = [
|
||
|
Excel::XLSX => '.xlsx',
|
||
|
Excel::CSV => '.csv',
|
||
|
// Excel::TSV => '.tsv',
|
||
|
Excel::ODS => '.ods',
|
||
|
Excel::XLS => '.xls',
|
||
|
Excel::HTML => '.html',
|
||
|
Excel::MPDF => '.pdf',
|
||
|
Excel::DOMPDF => '.pdf',
|
||
|
Excel::TCPDF => '.pdf',
|
||
|
];
|
||
|
|
||
|
return $extensions[$this->writerType] ?? '.xlsx'; */
|
||
|
return '.xlsx';
|
||
|
}
|
||
|
|
||
|
public function generateFileName()
|
||
|
{
|
||
|
$uniqueCode = Excel::generateUniqueCode($this->params, '', 8);
|
||
|
if (!$this->name) {
|
||
|
$this->fileName = date('YmdHis') . $uniqueCode;
|
||
|
} else {
|
||
|
$this->fileName = date('YmdHis') . $uniqueCode . '_' . $this->name;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected function generateFilePath()
|
||
|
{
|
||
|
if (!$this->fileName) {
|
||
|
$this->generateFileName();
|
||
|
}
|
||
|
$excelPath = getRoutePrefix() . '/excels/';
|
||
|
if (!is_dir($this->basePath . $excelPath)) {
|
||
|
mkdir($this->basePath . $excelPath);
|
||
|
}
|
||
|
$this->fileUrl = $excelPath . $this->fileName. $this->getExtension();
|
||
|
$this->filePath = $this->basePath . $this->fileUrl;
|
||
|
}
|
||
|
|
||
|
public function getMode()
|
||
|
{
|
||
|
return self::MODE_QUERY;
|
||
|
}
|
||
|
|
||
|
protected function prepare()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
public function store()
|
||
|
{
|
||
|
$this->prepare();
|
||
|
$this->generateFilePath();
|
||
|
$exporter = new Exporter();
|
||
|
$exporter->store($this, $this->filePath);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 返回设置单元格样式的函数 function(Export, Worksheet)
|
||
|
*/
|
||
|
public function getStyleSetting(): ?Closure
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public function setFileName($fileName)
|
||
|
{
|
||
|
return $this->fileName = $fileName;
|
||
|
}
|
||
|
|
||
|
public function getFileName()
|
||
|
{
|
||
|
return $this->fileName;
|
||
|
}
|
||
|
|
||
|
public function getFileUrl()
|
||
|
{
|
||
|
return $this->fileUrl;
|
||
|
}
|
||
|
|
||
|
protected function check()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
public static function identity()
|
||
|
{
|
||
|
return Str::encrypt(static::class);
|
||
|
}
|
||
|
|
||
|
public static function getClassByIdentity($identity)
|
||
|
{
|
||
|
return Str::decrypt($identity);
|
||
|
}
|
||
|
}
|