Explorar o código

Merge branch 'v5.0.0dev' of https://gitee.com/ZhongBangKeJi/CRMEB into v5.0.0dev

From-wh %!s(int64=2) %!d(string=hai) anos
pai
achega
0ee8db4273

+ 2 - 2
crmeb/app/adminapi/controller/v1/setting/SystemCrud.php

@@ -198,7 +198,7 @@ class SystemCrud extends AuthController
         $softDelete = false;
 
         foreach ((array)$info->field['tableField'] as $item) {
-            if ($item['field_type'] === 'addSoftDelete') {
+            if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
                 $softDelete = true;
                 break;
             }
@@ -350,7 +350,7 @@ class SystemCrud extends AuthController
             'softDelete' => $softDelete,
             'fromField' => $info->field['fromField'] ?? [],
             'columnField' => $info->field['columnField'] ?? [],
-        ], $makePath);
+        ], $makePath, $zipPath);
 
         if (!extension_loaded('zip')) {
             return app('json')->fail(500040);

+ 39 - 32
crmeb/app/services/system/SystemCrudServices.php

@@ -27,6 +27,7 @@ use crmeb\services\crud\Validate;
 use crmeb\services\crud\ViewApi;
 use crmeb\services\crud\ViewPages;
 use crmeb\services\crud\ViewRouter;
+use crmeb\services\FileService;
 use Phinx\Db\Adapter\AdapterFactory;
 use think\facade\Db;
 use think\helper\Str;
@@ -504,99 +505,105 @@ class SystemCrudServices extends BaseServices
      * @param bool $isMake
      * @param array $options
      * @param array $filePath
+     * @param string $basePath
      * @return array[]
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/4/7
      */
-    public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [])
+    public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [], string $basePath = '')
     {
         $options['fromField'] = is_array($options['fromField']) ? $options['fromField'] : [];
         $options['columnField'] = is_array($options['columnField']) ? $options['columnField'] : [];
         //生成模型
         $model = app()->make(Model::class);
-        [$modelContent, $modelPath, $usePath] = $model->setFilePathName($filePath['model'] ?? '')->isMake($isMake)->handle($tableName, $options);
+        $model->setFilePathName($filePath['model'] ?? '')->setbasePath($basePath)->handle($tableName, $options);
         //生成dao
         $dao = app()->make(Dao::class);
-        [$daoContent, $daoPath, $usePath] = $dao->setFilePathName($filePath['dao'] ?? '')->isMake($isMake)->handle($tableName, [
-            'usePath' => $usePath,
+        $dao->setFilePathName($filePath['dao'] ?? '')->setbasePath($basePath)->handle($tableName, [
+            'usePath' => $model->getUsePath(),
         ]);
         //生成service
         $service = app()->make(Service::class);
-        [$serviceContent, $servicePath, $usePath] = $service->setFilePathName($filePath['service'] ?? '')->isMake($isMake)->handle($tableName, [
+        $service->setFilePathName($filePath['service'] ?? '')->setbasePath($basePath)->handle($tableName, [
             'field' => $options['fromField'],
-            'usePath' => $usePath,
+            'usePath' => $dao->getUsePath(),
         ]);
         //生成验证器
         $validate = app()->make(Validate::class);
-        [$validateContent, $validatePath] = $validate->setFilePathName($filePath['validate'] ?? '')->isMake($isMake)->handle($tableName);
+        $validate->setFilePathName($filePath['validate'] ?? '')->setbasePath($basePath)->handle($tableName);
         //生成控制器
         $controller = app()->make(Controller::class);
-        [$controllerContent, $controllerPath] = $controller->setFilePathName($filePath['controller'] ?? '')->isMake($isMake)->handle($tableName, [
-            'usePath' => $usePath,
+        $controller->setFilePathName($filePath['controller'] ?? '')->setbasePath($basePath)->handle($tableName, [
+            'usePath' => $service->getUsePath(),
             'field' => array_column($options['fromField'], 'field'),
         ]);
         //生成路由
         $route = app()->make(Route::class);
-        [$routeContent, $routePath] = $route->setFilePathName($filePath['route'] ?? '')->isMake($isMake)->handle($tableName, [
+        $route->setFilePathName($filePath['route'] ?? '')->setbasePath($basePath)->handle($tableName, [
             'menus' => $options['menuName'],
             'route' => $routeName
         ]);
         //生成前台路由
         $viewRouter = app()->make(ViewRouter::class);
-        [$routerContent, $routerPath] = $viewRouter->setFilePathName($filePath['router'] ?? '')->isMake($isMake)->handle($tableName, [
+        $viewRouter->setFilePathName($filePath['router'] ?? '')->setbasePath($basePath)->handle($tableName, [
             'route' => $routeName,
             'menuName' => $options['menuName'],
         ]);
         //生成前台接口
         $viewApi = app()->make(ViewApi::class);
-        [$apiContent, $apiPath] = $viewApi->setFilePathName($filePath['api'] ?? '')->isMake($isMake)->handle($tableName, [
+        $viewApi->setFilePathName($filePath['api'] ?? '')->setbasePath($basePath)->handle($tableName, [
             'route' => $routeName,
         ]);
         //生成前台页面
         $viewPages = app()->make(ViewPages::class);
-        [$pagesContent, $pagesPath] = $viewPages->setFilePathName($filePath['pages'] ?? '')->isMake($isMake)->handle($tableName, [
+        $viewPages->setFilePathName($filePath['pages'] ?? '')->setbasePath($basePath)->handle($tableName, [
             'field' => $options['columnField'],
             'route' => $routeName,
-            'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $apiPath)),
+            'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $viewApi->getPath())),
         ]);
 
+        //创建文件
+        if ($isMake) {
+            FileService::batchMakeFiles([$model, $dao, $service, $controller, $route, $viewApi, $viewPages, $viewRouter]);
+        }
+
         return [
             'controller' => [
-                'path' => $this->replace($controllerPath),
-                'content' => $controllerContent
+                'path' => $this->replace($controller->getPath()),
+                'content' => $controller->getContent()
             ],
             'model' => [
-                'path' => $this->replace($modelPath),
-                'content' => $modelContent
+                'path' => $this->replace($model->getPath()),
+                'content' => $model->getContent()
             ],
             'dao' => [
-                'path' => $this->replace($daoPath),
-                'content' => $daoContent
+                'path' => $this->replace($dao->getPath()),
+                'content' => $dao->getContent()
             ],
             'route' => [
-                'path' => $this->replace($routePath),
-                'content' => $routeContent
+                'path' => $this->replace($route->getPath()),
+                'content' => $route->getContent()
             ],
             'service' => [
-                'path' => $this->replace($servicePath),
-                'content' => $serviceContent
+                'path' => $this->replace($service->getPath()),
+                'content' => $service->getContent()
             ],
             'validate' => [
-                'path' => $this->replace($validatePath),
-                'content' => $validateContent
+                'path' => $this->replace($validate->getPath()),
+                'content' => $validate->getContent()
             ],
             'router' => [
-                'path' => $this->replace($routerPath),
-                'content' => $routerContent
+                'path' => $this->replace($viewRouter->getPath()),
+                'content' => $viewRouter->getContent()
             ],
             'api' => [
-                'path' => $this->replace($apiPath),
-                'content' => $apiContent
+                'path' => $this->replace($viewApi->getPath()),
+                'content' => $viewApi->getContent()
             ],
             'pages' => [
-                'path' => $this->replace($pagesPath),
-                'content' => $pagesContent
+                'path' => $this->replace($viewPages->getPath()),
+                'content' => $viewPages->getContent()
             ],
         ];
     }

+ 43 - 0
crmeb/crmeb/services/FileService.php

@@ -11,6 +11,8 @@
 namespace crmeb\services;
 
 use crmeb\exceptions\AdminException;
+use crmeb\exceptions\CrudException;
+use crmeb\services\crud\Make;
 
 /**
  * 文件操作类
@@ -1048,4 +1050,45 @@ class FileService
         return $zip->extractTo($folder);
     }
 
+    /**
+     * 批量写入文件
+     * @param array $make
+     * @return bool
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    public static function batchMakeFiles(array $make)
+    {
+
+        $files = [];
+        $dirnames = [];
+        foreach ($make as $item) {
+            if ($item instanceof Make) {
+                $files[] = $item = $item->toArray();
+            }
+            try {
+                $dirnames[] = $dirname = dirname($item['path']);
+                if (!is_dir($dirname)) {
+                    mkdir($dirname, 0755, true);
+                }
+            } catch (\Throwable $e) {
+                if ($dirnames) {
+                    foreach ($dirnames as $dirname) {
+                        if (strstr($dirname, app()->getRootPath() . 'backup') !== false) {
+                            rmdir($dirname);
+                        }
+                    }
+                }
+                throw new \RuntimeException($e->getMessage());
+            }
+        }
+        $res = true;
+        foreach ($files as $item) {
+            $res = $res && file_put_contents($item['path'], $item['content'], LOCK_EX);
+        }
+
+        return $res;
+    }
+
 }

+ 5 - 3
crmeb/crmeb/services/crud/Controller.php

@@ -44,7 +44,7 @@ class Controller extends Make
     }
 
     /**
-     * @return mixed|void
+     * @return Controller
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/3/13
@@ -99,8 +99,10 @@ class Controller extends Make
         $contentStr = str_replace($this->var, $this->value, $contentController);
 
         $filePath = $this->getFilePathName($path, $this->value['nameCamel']);
-
-        return [$this->makeFile($filePath, $contentStr), $this->filePathName ?: $filePath, $this->value['path']];
+        $this->usePath = $this->value['path'];
+        $this->setPathname($filePath);
+        $this->setContent($contentStr);
+        return $this;
     }
 
     /**

+ 101 - 82
crmeb/crmeb/services/crud/Make.php

@@ -58,10 +58,16 @@ abstract class Make
     protected $content = '';
 
     /**
-     * 文件存放
-     * @var null
+     * 实际文件存放
+     * @var string
      */
-    protected $filePath = null;
+    protected $pathname = '';
+
+    /**
+     * 命名空间路径
+     * @var string
+     */
+    protected $usePath = '';
 
     /**
      * 变量名称
@@ -75,17 +81,6 @@ abstract class Make
      */
     protected $value = [];
 
-    /**
-     * @var bool
-     */
-    protected $isMake = true;
-
-    /**
-     * 文件存在是否生成
-     * @var bool
-     */
-    protected $isExistsMake = true;
-
     /**
      * 后台前端模板根路径
      * @var string
@@ -125,6 +120,22 @@ abstract class Make
         $this->setDefaultValue();
     }
 
+    /**
+     * 设置默认路径
+     * @param string $basePath
+     * @return $this
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    public function setbasePath(string $basePath)
+    {
+        if ($basePath) {
+            $this->basePath = $basePath;
+        }
+        return $this;
+    }
+
     /**
      * @return string
      * @author 等风来
@@ -176,17 +187,6 @@ abstract class Make
         return $this->fileBasePath;
     }
 
-    /**
-     * @return string
-     * @author 等风来
-     * @email 136327134@qq.com
-     * @date 2023/4/7
-     */
-    public function getFileBasePath()
-    {
-        return $this->fileBasePath;
-    }
-
     /**
      * 设置文件保存就路径名称
      * @param string $filePathName
@@ -216,23 +216,9 @@ abstract class Make
         return str_pad('', 4 * $num);
     }
 
-    /**
-     * 是否生成文件
-     * @param bool $isMake
-     * @return $this
-     * @author 等风来
-     * @email 136327134@qq.com
-     * @date 2023/4/3
-     */
-    public function isMake(bool $isMake = true)
-    {
-        $this->isMake = $isMake;
-        return $this;
-    }
-
     /**
      * 执行创建
-     * @return mixed|void
+     * @return Make
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/3/13
@@ -256,11 +242,11 @@ abstract class Make
         $contentStr = str_replace($this->var, $this->value, $content);
         $filePath = $this->getFilePathName($path, $this->value['nameCamel']);
 
-        return [
-            $this->makeFile($filePath, $contentStr),
-            $this->filePathName ?: $filePath,
-            $this->baseDir . '\\' . $this->value['nameCamel']
-        ];
+        $this->usePath = $this->baseDir . '\\' . $this->value['nameCamel'];
+        $this->setPathname($filePath);
+        $this->setContent($contentStr);
+
+        return $this;
     }
 
     /**
@@ -434,37 +420,6 @@ abstract class Make
         return 'app' . ($app ? '\\' . $app : '');
     }
 
-
-    public function make(string $pathname)
-    {
-        $pathname = $this->filePathName ?: ($this->filePath ?: $pathname);
-
-        if ($this->isMake) {
-
-            if (is_file($pathname)) {
-                if ($this->isExistsMake) {
-                    unlink($pathname);
-                } else {
-                    throw new CrudException(500052, ['filename' => $this->name . ':' . $pathname]);
-                }
-            }
-
-            try {
-                if (!is_dir(dirname($pathname))) {
-                    mkdir(dirname($pathname), 0755, true);
-                }
-            } catch (\Throwable $e) {
-                throw new CrudException(500050, ['pathname' => dirname($pathname)]);
-            }
-
-            try {
-                file_put_contents($pathname, $this->content);
-            } catch (\Throwable $e) {
-                throw new CrudException(500051, ['pathname' => $pathname]);
-            }
-        }
-    }
-
     /**
      * 设置内容
      * @param string $content
@@ -476,26 +431,90 @@ abstract class Make
     protected function setContent(string $content)
     {
         $this->content = str_replace('', '', $content);
-
         return $this->content;
     }
 
     /**
-     * 执行创建文件
+     * @param string $pathname
+     * @return $this
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    protected function setPathname(string $pathname)
+    {
+        $this->pathname = $this->filePathName ?: $pathname;
+        return $this;
+    }
+
+    /**
+     * @param string $key
+     * @return mixed|null
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    public function getValue(string $key)
+    {
+        return $this->value[$key] ?? null;
+    }
+
+    /**
+     * 获取命名空间路径
      * @return string
      * @author 等风来
      * @email 136327134@qq.com
-     * @date 2023/3/13
+     * @date 2023/4/18
      */
-    protected function makeFile(string $pathname, string $content)
+    public function getUsePath()
+    {
+        return $this->usePath;
+    }
+
+    /**
+     * 获取内容
+     * @return string
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    public function getContent()
     {
-        $this->content = str_replace('', '', $content);
-        $this->make($pathname);
         return $this->content;
     }
 
+    /**
+     * @return string
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    public function getPath()
+    {
+        return $this->pathname;
+    }
+
+    /**
+     * @return array
+     * @author 等风来
+     * @email 136327134@qq.com
+     * @date 2023/4/18
+     */
+    public function toArray()
+    {
+        return [
+            'path' => $this->pathname,
+            'content' => $this->content,
+            'value' => $this->value,
+            'var' => $this->var,
+            'usePath' => $this->usePath,
+        ];
+    }
+
     public function __destruct()
     {
         $this->content = '';
+        $this->pathname = '';
+        $this->usePath = '';
     }
 }

+ 1 - 1
crmeb/crmeb/services/crud/Model.php

@@ -44,7 +44,7 @@ class Model extends Make
     /**
      * @param string $name
      * @param array $options
-     * @return array|mixed|void
+     * @return Model
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/4/12

+ 5 - 2
crmeb/crmeb/services/crud/Route.php

@@ -38,7 +38,7 @@ class Route extends Make
     /**
      * @param string $name
      * @param array $options
-     * @return mixed|void
+     * @return Route
      */
     public function handle(string $name, array $options = [])
     {
@@ -86,7 +86,10 @@ class Route extends Make
 
         $filePath = $this->getFilePathName($path, strtolower($name));
 
-        return [$this->makeFile($filePath, $contentStr), $this->filePathName ?: $filePath];
+        $this->setPathname($filePath);
+        $this->setContent($contentStr);
+
+        return $this;
     }
 
     /**

+ 5 - 9
crmeb/crmeb/services/crud/Service.php

@@ -10,8 +10,6 @@
 // +----------------------------------------------------------------------
 namespace crmeb\services\crud;
 
-use crmeb\services\FormBuilder as Form;
-use think\facade\Route as Url;
 use think\helper\Str;
 
 /**
@@ -39,7 +37,7 @@ class Service extends Make
     /**
      * @param string $name
      * @param array $options
-     * @return mixed|void
+     * @return Service
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/3/23
@@ -106,12 +104,10 @@ class Service extends Make
         $contentStr = str_replace($this->var, $this->value, $content);
 
         $filePath = $this->getFilePathName($path, $this->value['nameCamel']);
-
-        return [
-            $this->makeFile($filePath, $contentStr),
-            $this->filePathName ?: $filePath,
-            $this->baseDir . '\\' . $this->value['nameCamel']
-        ];
+        $this->usePath = $this->baseDir . '\\' . $this->value['nameCamel'];
+        $this->setContent($contentStr);
+        $this->setPathname($filePath);
+        return $this;
     }
 
     /**

+ 4 - 4
crmeb/crmeb/services/crud/ViewApi.php

@@ -61,7 +61,7 @@ class ViewApi extends Make
     /**
      * @param string $name
      * @param array $options
-     * @return array
+     * @return ViewApi
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/4/4
@@ -100,9 +100,9 @@ class ViewApi extends Make
         $contentStr = str_replace($this->var, $this->value, $content);
         $filePath = $this->getFilePathName($path, Str::camel($name));
 
-        $content = $this->makeFile($filePath, $contentStr);
-
-        return [$content, $this->filePathName ?: $filePath];
+        $this->setPathname($filePath);
+        $this->setContent($contentStr);
+        return $this;
     }
 
     /**

+ 1 - 1
crmeb/crmeb/services/crud/ViewPages.php

@@ -61,7 +61,7 @@ class ViewPages extends Make
      * @param string $name
      * @param string $path
      * @param array $options
-     * @return false|mixed|void
+     * @return ViewPages
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/4/3

+ 4 - 4
crmeb/crmeb/services/crud/ViewRouter.php

@@ -61,7 +61,7 @@ class ViewRouter extends Make
      * @param string $name
      * @param string $path
      * @param array $options
-     * @return false|mixed|void
+     * @return ViewRouter
      * @author 等风来
      * @email 136327134@qq.com
      * @date 2023/4/4
@@ -90,9 +90,9 @@ class ViewRouter extends Make
 
         $filePath = $this->getFilePathName($path, Str::camel($name));
 
-        $makeContent = $this->makeFile($filePath, $contentStr);
-
-        return [$makeContent, $filePath];
+        $this->setContent($contentStr);
+        $this->setPathname($filePath);
+        return $this;
     }
 
     /**