修改crud生成文件

This commit is contained in:
liaofei 2023-04-11 18:44:08 +08:00
parent 1091fd4590
commit 1e783774c5
7 changed files with 54 additions and 14 deletions

View File

@ -110,8 +110,11 @@ class SystemCrud extends AuthController
}
$tableField = [];
if (!$isTable) {
if ($isTable) {
$field = $this->services->getColumnNamesList($tableName);
if (!$field) {
return app('json')->fail('表不存在');
}
foreach ($field as $item) {
$tableField[] = ['value' => $item['name'], 'comment' => $item['comment'], 'label' => $item['name']];
}

View File

@ -18,6 +18,7 @@ use app\dao\system\SystemCrudDao;
use app\services\BaseServices;
use crmeb\services\crud\Controller;
use crmeb\services\crud\Dao;
use crmeb\services\crud\Make;
use crmeb\services\crud\Model;
use crmeb\services\crud\Route;
use crmeb\services\crud\Service;
@ -318,6 +319,8 @@ class SystemCrudServices extends BaseServices
*/
public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [])
{
$options['fromField'] = is_array($options['fromField']) ? $options['fromField'] : [];
$options['columnField'] = is_array($options['columnField']) ? $options['columnField'] : [];
//生成控制器
$controller = app()->make(Controller::class);
[$controllerContent, $controllerPath] = $controller->setFilePathName($filePath['controller'] ?? '')->isMake($isMake)->handle($tableName, '/');
@ -336,7 +339,7 @@ class SystemCrudServices extends BaseServices
//生成service
$service = app()->make(Service::class);
[$serviceContent, $servicePath] = $service->setFilePathName($filePath['service'] ?? '')->isMake($isMake)->handle($tableName, '/', [
'field' => $options['fromField']
'field' => $options['fromField'],
]);
//生成验证器
$validate = app()->make(Validate::class);
@ -351,6 +354,7 @@ 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, '/', [
@ -359,44 +363,49 @@ class SystemCrudServices extends BaseServices
return [
'controller' => [
'path' => $controllerContent,
'content' => $controllerPath
'path' => $this->replace($controllerPath),
'content' => $controllerContent
],
'model' => [
'path' => $modelPath,
'path' => $this->replace($modelPath),
'content' => $modelContent
],
'dao' => [
'path' => $daoPath,
'path' => $this->replace($daoPath),
'content' => $daoContent
],
'route' => [
'path' => $routePath,
'path' => $this->replace($routePath),
'content' => $routeContent
],
'service' => [
'path' => $servicePath,
'path' => $this->replace($servicePath),
'content' => $serviceContent
],
'validate' => [
'path' => $validatePath,
'path' => $this->replace($validatePath),
'content' => $validateContent
],
'router' => [
'path' => $routerPath,
'path' => $this->replace($routerPath),
'content' => $routerContent
],
'api' => [
'path' => $apiPath,
'path' => $this->replace($apiPath),
'content' => $apiContent
],
'pages' => [
'path' => $pagesPath,
'path' => $this->replace($pagesPath),
'content' => $pagesContent
],
];
}
protected function replace(string $path)
{
return str_replace([app()->getRootPath(), Make::adminTemplatePath()], '', $path);
}
/**
* @param string $tableName
* @param bool $fullName

View File

@ -48,4 +48,6 @@ return [
'console_remind' => true,
// admin路由前缀
'admin_prefix' => 'admin',
//后台前端模板根路径
'admin_template_path' => dirname(app()->getRootPath()) . DS . 'template' . DS . 'admin' . DS . 'src' . DS,
];

View File

@ -99,7 +99,7 @@ abstract class Make
public function __construct(App $app)
{
$this->app = $app;
$this->adminTemplatePath = dirname($this->app->getRootPath()) . DS . 'template' . DS . 'admin' . DS . 'src' . DS;
$this->adminTemplatePath = self::adminTemplatePath();
$this->basePath = $this->app->getRootPath();
$this->baseDir = $this->setBaseDir();
$this->var = $this->authDrawVar();
@ -107,6 +107,17 @@ abstract class Make
$this->setDefaultValue();
}
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public static function adminTemplatePath()
{
return config('app.admin_template_path');
}
/**
* 设置默认保存目录
* @author 等风来

View File

@ -85,6 +85,21 @@ class Route extends Make
return [$this->makeFile($filePath, $contentStr), $filePath];
}
/**
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
* @param string $path
* @param string $name
* @return string
*/
protected function getFilePathName(string $path, string $name): string
{
$path = ltrim(str_replace('\\', '/', $path), '/');
return $this->getBasePath($path) . $name . '.' . $this->fileMime;
}
/**
* 设置模板
* @param string $type

View File

@ -45,6 +45,6 @@ class Validate extends Make
*/
protected function getStub(string $type = '')
{
return __DIR__ . DS . 'stubs' . DS . 'validate' . DS . 'CrudValidate.stub';
return __DIR__ . DS . 'stubs' . DS . 'validate' . DS . 'crudValidate.stub';
}
}