From 3ad1b8f0467718f49449d1ec8c2380dc47e27c96 Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Sat, 15 Apr 2023 12:06:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8B=E8=BD=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../controller/v1/setting/SystemCrud.php | 97 ++++++++++++++++--- crmeb/app/adminapi/route/system.php | 6 +- .../services/system/SystemCrudServices.php | 3 +- crmeb/crmeb/services/FileService.php | 6 +- crmeb/crmeb/services/crud/Make.php | 76 ++++++++++++--- 6 files changed, 155 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 8295ea5c7..fce675338 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ /template/uni-app/unpackage/ /template/uni-app/.hbuilderx/ crmeb/.idea/ +crmeb/backup/ diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php index f22f1895d..e0a294ed7 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php @@ -17,6 +17,7 @@ namespace app\adminapi\controller\v1\setting; use app\adminapi\controller\AuthController; use app\services\system\SystemCrudServices; use app\services\system\SystemMenusServices; +use crmeb\services\CacheService; use crmeb\services\crud\Make; use crmeb\services\FileService; use crmeb\utils\Terminal; @@ -290,6 +291,79 @@ class SystemCrud extends AuthController return app('json')->success('删除成功'); } + /** + * 下载文件 + * @param $id + * @return Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/15 + */ + public function download($id) + { + if (!$id) { + return app('json')->fail('缺少参数'); + } + + $info = $this->services->get($id); + if (!$info) { + return app('json')->fail('下载的文件不存在'); + } + $zipPath = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name); + $zipName = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name) . '.zip'; + $makePath = $info->make_path ?? []; + foreach ($makePath as $key => $item) { + if (in_array($key, ['pages', 'router', 'api'])) { + $item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item; + } else { + $item = $zipPath . DS . $item; + } + $makePath[$key] = $item; + } + + $routeName = 'crud/' . Str::snake($info->table_name); + + $column = $this->services->getColumnNamesList($info->table_name); + $key = 'id'; + foreach ($column as $value) { + if ($value['primaryKey']) { + $key = $value['name']; + break; + } + } + + $softDelete = false; + + foreach ((array)$info->field['tableField'] as $item) { + if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') { + $softDelete = true; + break; + } + } + + $this->services->makeFile($info->table_name, $routeName, true, [ + 'menuName' => $info->name, + 'key' => $key, + 'softDelete' => $softDelete, + 'fromField' => $info->field['fromField'] ?? [], + 'columnField' => $info->field['columnField'] ?? [], + ], $makePath); + + if (!extension_loaded('zip')) { + return app('json')->fail('请安装zip扩展后在尝试下载'); + } + + $fileService = new FileService(); + $fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup'); + + $key = md5($zipName); + CacheService::set($key, [ + 'path' => $zipName, + 'fileName' => Str::camel($info->table_name) . '.zip' + ]); + return app()->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]); + } + /** * @return string * @author 等风来 @@ -304,26 +378,27 @@ class SystemCrud extends AuthController $adminPath = dirname($adminPath); - $dir = $adminPath . DS . 'node_modules'; - if (!is_dir($dir)) { - $terminal->run('npm-install'); + try { + $dir = $adminPath . DS . 'node_modules'; + if (!is_dir($dir)) { +// $terminal->run('npm-install'); + } + +// $res = $terminal->run('npm-build'); + } catch (\Throwable $e) { + $terminal->echoOutputFlag($e->getMessage()); } - $terminal->run('npm-build'); - if (!is_dir($adminPath . DS . 'dist')) { - return Response::create([ + echo Response::create([ 'message' => '打包失败', ], 'json')->getContent(); } $build = public_path() . config('app.admin_prefix'); - $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build); +// $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build); - return Response::create([ - 'message' => '打包成功', - 'success' => 'ok' - ], 'json')->getContent(); +// echo $res; } } diff --git a/crmeb/app/adminapi/route/system.php b/crmeb/app/adminapi/route/system.php index 77abb7dbb..b14441d89 100644 --- a/crmeb/app/adminapi/route/system.php +++ b/crmeb/app/adminapi/route/system.php @@ -8,8 +8,8 @@ // +---------------------------------------------------------------------- // | Author: CRMEB Team // +---------------------------------------------------------------------- -use think\facade\Route; +use think\facade\Route; /** * 维护 相关路由 @@ -133,8 +133,8 @@ Route::group('system', function () { 'delete' => '删除路由分类' ], ]); - //执行重新打包 - Route::get('crud/npm', 'v1.setting.SystemCrud/npm')->option(['real_name' => '执行重新打包']); + //下载生成的文件 + Route::get('crud/download', 'v1.setting.SystemCrud/download')->option(['real_name' => '下载生成的文件']); //获取CRUD列表 Route::get('crud/column_type', 'v1.setting.SystemCrud/columnType')->option(['real_name' => '获取CRUD列表']); //获取菜单TREE形数据 diff --git a/crmeb/app/services/system/SystemCrudServices.php b/crmeb/app/services/system/SystemCrudServices.php index afa802c1e..b3ce21e0f 100644 --- a/crmeb/app/services/system/SystemCrudServices.php +++ b/crmeb/app/services/system/SystemCrudServices.php @@ -369,7 +369,7 @@ class SystemCrudServices extends BaseServices $menuIds = array_column($menus->toArray(), 'id'); array_push($menuIds, $menuInfo->id); //生成文件 - $make = $this->makeFile($tableName, $routeName, true, $data, $filePath); + $make = $this->makeFile($tableName, $routeName, false, $data, $filePath); $makePath = []; foreach ($make as $key => $item) { $makePath[$key] = $item['path']; @@ -555,7 +555,6 @@ class SystemCrudServices extends BaseServices [$apiContent, $apiPath] = $viewApi->setFilePathName($filePath['api'] ?? '')->isMake($isMake)->handle($tableName, [ 'route' => $routeName, ]); - //生成前台页面 $viewPages = app()->make(ViewPages::class); [$pagesContent, $pagesPath] = $viewPages->setFilePathName($filePath['pages'] ?? '')->isMake($isMake)->handle($tableName, [ diff --git a/crmeb/crmeb/services/FileService.php b/crmeb/crmeb/services/FileService.php index 47b979b59..7b0a5d8bc 100644 --- a/crmeb/crmeb/services/FileService.php +++ b/crmeb/crmeb/services/FileService.php @@ -1018,9 +1018,11 @@ class FileService $file = str_replace('\\', '/', $file); if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) continue; if (is_dir($file) === true) { - $zip->addEmptyDir(str_replace($folder . '/', '', $file . '/')); + $fileName = $folder ? str_replace($folder . '/', '', $file . '/') : $file . '/'; + $zip->addEmptyDir($fileName); } else if (is_file($file) === true) { - $zip->addFromString(str_replace($folder . '/', '', $file), file_get_contents($file)); + $fileName = $folder ? str_replace($folder . '/', '', $file) : $file; + $zip->addFromString($fileName, file_get_contents($file)); } } } else if (is_file($source) === true) { diff --git a/crmeb/crmeb/services/crud/Make.php b/crmeb/crmeb/services/crud/Make.php index 740ab0e08..5dd8752dd 100644 --- a/crmeb/crmeb/services/crud/Make.php +++ b/crmeb/crmeb/services/crud/Make.php @@ -51,6 +51,18 @@ abstract class Make */ protected $fileBasePath; + /** + * 文件内容 + * @var string + */ + protected $content = ''; + + /** + * 文件存放 + * @var null + */ + protected $filePath = null; + /** * 变量名称 * @var array @@ -68,6 +80,12 @@ abstract class Make */ protected $isMake = true; + /** + * 文件存在是否生成 + * @var bool + */ + protected $isExistsMake = true; + /** * 后台前端模板根路径 * @var string @@ -416,25 +434,19 @@ abstract class Make return 'app' . ($app ? '\\' . $app : ''); } - /** - * 执行创建文件 - * @return string - * @author 等风来 - * @email 136327134@qq.com - * @date 2023/3/13 - */ - protected function makeFile(string $pathname, string $content) + + public function make(string $pathname) { - - $pathname = $this->filePathName ?: $pathname; - - - $content = str_replace('', '', $content); + $pathname = $this->filePathName ?: ($this->filePath ?: $pathname); if ($this->isMake) { if (is_file($pathname)) { - throw new CrudException($this->name . ':' . $pathname . ' already exists!'); + if ($this->isExistsMake) { + unlink($pathname); + } else { + throw new CrudException($this->name . ':' . $pathname . ' already exists!'); + } } try { @@ -446,12 +458,44 @@ abstract class Make } try { - file_put_contents($pathname, $content); + file_put_contents($pathname, $this->content); } catch (\Throwable $e) { throw new CrudException('CRUD生成文件报错,无法写入:' . $pathname); } } + } - return $content; + /** + * 设置内容 + * @param string $content + * @return array|string|string[] + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/14 + */ + protected function setContent(string $content) + { + $this->content = str_replace('', '', $content); + + return $this->content; + } + + /** + * 执行创建文件 + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/3/13 + */ + protected function makeFile(string $pathname, string $content) + { + $this->content = str_replace('', '', $content); + $this->make($pathname); + return $this->content; + } + + public function __destruct() + { + $this->content = ''; } }