add:新增导入组件
This commit is contained in:
parent
ae53d4e404
commit
f67a4f33d5
47
catch/cms/tables/Category.php
Normal file
47
catch/cms/tables/Category.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace catchAdmin\cms\table;
|
||||
|
||||
use catcher\CatchTable;
|
||||
use catcher\library\table\Actions;
|
||||
use catcher\library\table\HeaderItem;
|
||||
use catcher\library\table\Search;
|
||||
|
||||
class Category extends CatchTable
|
||||
{
|
||||
public function table()
|
||||
{
|
||||
// TODO: Implement table() method.
|
||||
return $this->getTable('category')
|
||||
->header([
|
||||
HeaderItem::label('分类名称')->prop('name'),
|
||||
HeaderItem::label('自定义链接')->prop('url'),
|
||||
HeaderItem::label('栏目类型')->prop('type')->component('type'),
|
||||
HeaderItem::label('投稿')->prop('is_can_contribute')->withSwitchComponent(),
|
||||
HeaderItem::label('评论')->prop('is_can_comment')->withSwitchComponent(),
|
||||
HeaderItem::label('状态')->prop('status')->withSwitchComponent(),
|
||||
HeaderItem::label('权重')->prop('weight')->withEditNumberComponent(),
|
||||
HeaderItem::label('创建时间')->prop('created_at'),
|
||||
HeaderItem::label('操作')->actions([
|
||||
Actions::update(),
|
||||
Actions::delete()
|
||||
])
|
||||
])
|
||||
->withActions([
|
||||
Actions::create()
|
||||
])
|
||||
->withSearch([
|
||||
Search::label('分类名称')->name('请输入分类名称'),
|
||||
Search::label('状态')->name('请选择状态'),
|
||||
])
|
||||
->toTreeTable()
|
||||
->render();
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
// TODO: Implement form() method.
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
50
extend/catcher/library/excel/reader/Factory.php
Normal file
50
extend/catcher/library/excel/reader/Factory.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace catcher\library\excel\reader;
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Ods;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Slk;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xml;
|
||||
|
||||
class Factory
|
||||
{
|
||||
/**
|
||||
* make reader
|
||||
*
|
||||
* @time 2021年04月01日
|
||||
* @param $filename
|
||||
* @return mixed
|
||||
*/
|
||||
public static function make($filename)
|
||||
{
|
||||
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
||||
|
||||
if (isset(self::readers()[$ext])) {
|
||||
return app()->make(self::readers()[$ext]);
|
||||
}
|
||||
|
||||
throw new FailedException('Dont Support The File Extension');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* readers
|
||||
*
|
||||
* @time 2021年04月01日
|
||||
* @return string[]
|
||||
*/
|
||||
protected static function readers(): array
|
||||
{
|
||||
return [
|
||||
'xlsx' => Xlsx::class,
|
||||
'xml' => Xml::class,
|
||||
'ods' => Ods::class,
|
||||
'slk' => Slk::class,
|
||||
'csv' => Csv::class,
|
||||
];
|
||||
}
|
||||
}
|
64
extend/catcher/library/excel/reader/Macro.php
Normal file
64
extend/catcher/library/excel/reader/Macro.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace catcher\library\excel\reader;
|
||||
|
||||
trait Macro
|
||||
{
|
||||
/**
|
||||
* 移除不需要的列
|
||||
*
|
||||
* @time 2021年04月21日
|
||||
* @param ...$indexes
|
||||
* @return mixed
|
||||
*/
|
||||
public function remove(...$indexes)
|
||||
{
|
||||
foreach ($indexes as $index) {
|
||||
unset($this->sheets[$index]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 memory
|
||||
*
|
||||
* @time 2021年04月21日
|
||||
* @param int $memory
|
||||
* @return mixed
|
||||
*/
|
||||
public function memory(int $memory)
|
||||
{
|
||||
ini_set('memory_limit', $memory . 'M');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理
|
||||
*
|
||||
* @time 2021年04月23日
|
||||
* @return array
|
||||
*/
|
||||
protected function dealWith(): array
|
||||
{
|
||||
$headers = $this->headers();
|
||||
|
||||
$data = [];
|
||||
|
||||
foreach ($this->sheets as &$sheet) {
|
||||
$d = [];
|
||||
foreach ($headers as $k => $header) {
|
||||
$d[$header] = method_exists($this, 'deal' . ucfirst($header)) ?
|
||||
|
||||
$this->{'deal' . ucfirst($header)}($sheet) : $sheet[$k];
|
||||
}
|
||||
|
||||
$data[] = $d;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
74
extend/catcher/library/excel/reader/Reader.php
Normal file
74
extend/catcher/library/excel/reader/Reader.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace catcher\library\excel\reader;
|
||||
|
||||
use catcher\CatchUpload;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
abstract class Reader
|
||||
{
|
||||
use Macro;
|
||||
|
||||
/**
|
||||
* 当前的 sheet
|
||||
*
|
||||
* false 代表获取全部 sheets
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $active = true;
|
||||
|
||||
|
||||
protected $sheets;
|
||||
|
||||
/**
|
||||
* 导入
|
||||
*
|
||||
* @time 2021年04月21日
|
||||
* @param $file
|
||||
* @return Reader
|
||||
*/
|
||||
public function import($file): Reader
|
||||
{
|
||||
$file = (new CatchUpload)->setPath('excel')->toLocal($file);
|
||||
|
||||
$reader = Factory::make($file);
|
||||
// 设置只读
|
||||
$reader->setReadDataOnly(true);
|
||||
|
||||
/* @var $spreadsheet Spreadsheet */
|
||||
$spreadsheet = $reader->load($file);
|
||||
|
||||
if ($this->active) {
|
||||
$this->sheets = $spreadsheet->getActiveSheet()->toArray();
|
||||
} else {
|
||||
foreach ($spreadsheet->getAllSheets() as $sheet) {
|
||||
$this->sheets[] = $sheet->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 必须实现的方法
|
||||
*
|
||||
* @time 2021年04月21日
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function headers();
|
||||
|
||||
|
||||
/**
|
||||
* 数据处理
|
||||
*
|
||||
* @time 2021年04月23日
|
||||
* @param callable $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function then(callable $callback)
|
||||
{
|
||||
return $callback($this->dealWith());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user