diff --git a/app/admin/service/curd/BuildCurdService.php b/app/admin/service/curd/BuildCurdService.php index e17593c..6be68cb 100644 --- a/app/admin/service/curd/BuildCurdService.php +++ b/app/admin/service/curd/BuildCurdService.php @@ -224,7 +224,7 @@ class BuildCurdService * 表单类型 * @var array */ - protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation', 'table', 'city','tag']; + protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation', 'table', 'city', 'tag']; /** * 初始化 @@ -1060,6 +1060,7 @@ class BuildCurdService $controllerFile = "{$this->rootDir}app{$this->DS}admin{$this->DS}controller{$this->DS}{$this->controllerFilename}.php"; if (empty($this->relationArray)) { $controllerIndexMethod = ''; + $controllerExportMethod = ''; } else { $relationCode = ''; foreach ($this->relationArray as $key => $val) { @@ -1072,6 +1073,12 @@ class BuildCurdService 'relationIndexMethod' => $relationCode, ] ); + $controllerExportMethod = $this->replaceTemplate( + $this->getTemplate("controller{$this->DS}exportMethod"), + [ + 'relationIndexMethod' => trim($relationCode), + ] + ); } $selectList = ''; @@ -1091,6 +1098,7 @@ class BuildCurdService 'controllerAnnotation' => $this->tableComment, 'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}", 'indexMethod' => $controllerIndexMethod, + 'exportMethod' => $controllerExportMethod, 'selectList' => $selectList, ] ); @@ -1276,7 +1284,7 @@ class BuildCurdService } elseif ($val['formType'] == 'city') { $templateFile = "view{$this->DS}module{$this->DS}city"; $define = $this->buildCityView($field, $val, $val['default']); - }elseif ($val['formType'] == 'tag') { + } elseif ($val['formType'] == 'tag') { $templateFile = "view{$this->DS}module{$this->DS}tag"; } @@ -1375,7 +1383,7 @@ class BuildCurdService } elseif ($val['formType'] == 'city') { $templateFile = "view{$this->DS}module{$this->DS}city"; $define = $this->buildCityView($field, $val, $value); - }elseif ($val['formType'] == 'tag') { + } elseif ($val['formType'] == 'tag') { $templateFile = "view{$this->DS}module{$this->DS}tag"; } diff --git a/app/admin/service/curd/templates/controller/controller.code b/app/admin/service/curd/templates/controller/controller.code index 36f33f2..a9421a9 100644 --- a/app/admin/service/curd/templates/controller/controller.code +++ b/app/admin/service/curd/templates/controller/controller.code @@ -24,4 +24,7 @@ class {{controllerName}} extends AdminController } {{indexMethod}} + + + {{exportMethod}} } \ No newline at end of file diff --git a/app/admin/service/curd/templates/controller/exportMethod.code b/app/admin/service/curd/templates/controller/exportMethod.code new file mode 100644 index 0000000..e0cf6ca --- /dev/null +++ b/app/admin/service/curd/templates/controller/exportMethod.code @@ -0,0 +1,25 @@ + + /** + * @NodeAnotation(title="导出") + */ + public function export() + { + list($page, $limit, $where) = $this->buildTableParames(); + + + $this->model = $this->model{{relationIndexMethod}}; + + $fields = $this->request->param('fields', '{}', null); + $image_fields = $this->request->param('image_fields', '{}', null); + $select_fields = $this->request->param('select_fields', '{}', null); + $date_fields = $this->request->param('date_fields', '{}', null); + + $fields = json_decode($fields, true); + $image_fields = json_decode($image_fields, true); + $select_fields = json_decode($select_fields, true); + $date_fields = json_decode($date_fields, true); + + $content = \app\common\tools\ExcelTools::exportModel($this->model, $where, $fields, $image_fields, $select_fields, $date_fields); + + return download($content, $this->model->getName() . date('YmdHis') . '.xlsx', true); + } \ No newline at end of file diff --git a/app/common/command/admin/Version.php b/app/common/command/admin/Version.php index 8caf25e..2a0d69d 100644 --- a/app/common/command/admin/Version.php +++ b/app/common/command/admin/Version.php @@ -12,10 +12,10 @@ use think\console\Output; class Version extends Command { - const VERSION = 'v2.0.3'; + const VERSION = 'v2.0.4'; const COMMENT = [ - '优化导出功能,兼容自定义参数、兼容关联查询用法', + '自动生成CURD导出生成关联查询', ]; protected function configure() diff --git a/app/common/tools/ExcelTools.php b/app/common/tools/ExcelTools.php index cc6c7b4..c392509 100644 --- a/app/common/tools/ExcelTools.php +++ b/app/common/tools/ExcelTools.php @@ -72,8 +72,8 @@ class ExcelTools } } else if (array_key_exists($field_key, $select_fields)) { // 需要设置选项 - - $cel = $select_fields[$field_key][$value]; + + $cel = $select_fields[$field_key][$value] ?? ''; } else if (in_array($field_key, $date_fields)) { if (empty($value)) { $cel = '';