开始引入curd

This commit is contained in:
2022-04-20 11:24:54 +08:00
parent 45f923b652
commit c2b7671f7e
31 changed files with 1752 additions and 262 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
<?php
namespace app\admin\service\curd\exceptions;
class CurdException extends \Exception
{
}

View File

@@ -0,0 +1,10 @@
<?php
namespace app\admin\service\curd\exceptions;
class FileException extends \Exception
{
}

View File

@@ -0,0 +1,10 @@
<?php
namespace app\admin\service\curd\exceptions;
class TableException extends \Exception
{
}

View File

@@ -0,0 +1,27 @@
<?php
namespace {{controllerNamespace}};
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
/**
* @ControllerAnnotation(title="{{controllerAnnotation}}")
*/
class {{controllerName}} extends AdminController
{
use \app\admin\traits\Curd;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new {{modelFilename}}();
{{selectList}}
}
{{indexMethod}}
}

View File

@@ -0,0 +1,31 @@
/**
* @NodeAnotation(title="列表")
*/
public function index()
{
if ($this->request->isAjax()) {
if (input('selectFields')) {
return $this->selectList();
}
list($page, $limit, $where) = $this->buildTableParames();
$count = $this->model
{{relationIndexMethod}}
->where($where)
->count();
$list = $this->model
{{relationIndexMethod}}
->where($where)
->page($page, $limit)
->order($this->sort)
->select();
$data = [
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list,
];
return json($data);
}
return $this->fetch();
}

View File

@@ -0,0 +1,2 @@
$this->assign('{{name}}', $this->model->{{name}}());

View File

@@ -0,0 +1,17 @@
<?php
namespace {{modelNamespace}};
use app\common\model\TimeModel;
class {{modelName}} extends TimeModel
{
protected $name = "{{table}}";
protected $deleteTime = {{deleteTime}};
{{relationList}}
{{selectList}}
}

View File

@@ -0,0 +1,5 @@
public function {{relationMethod}}()
{
return $this->belongsTo('{{relationModel}}', '{{foreignKey}}', '{{primaryKey}}');
}

View File

@@ -0,0 +1,5 @@
public function {{name}}()
{
return \app\admin\model\{{relation}}::column('{{values}}', 'id');
}

View File

@@ -0,0 +1,5 @@
public function {{name}}()
{
return {{values}};
}

View File

@@ -0,0 +1,34 @@
define(["jquery", "easy-admin"], function ($, ea) {
var init = {
table_elem: '#currentTable',
table_render_id: 'currentTableRenderId',
index_url: '{{controllerUrl}}/index',
add_url: '{{controllerUrl}}/add',
edit_url: '{{controllerUrl}}/edit',
delete_url: '{{controllerUrl}}/delete',
export_url: '{{controllerUrl}}/export',
modify_url: '{{controllerUrl}}/modify',
};
var Controller = {
index: function () {
ea.table.render({
init: init,
cols: [[
{{indexCols}}
]],
});
ea.listen();
},
add: function () {
ea.listen();
},
edit: function () {
ea.listen();
},
};
return Controller;
});

View File

@@ -0,0 +1,11 @@
<div class="layuimini-container">
<form id="app-form" class="layui-form layuimini-form">
{{formList}}
<div class="hr-line"></div>
<div class="layui-form-item text-center">
<button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit>确认</button>
<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">重置</button>
</div>
</form>
</div>

View File

@@ -0,0 +1,10 @@
<div class="layuimini-container">
<div class="layuimini-main">
<table id="currentTable" class="layui-table layui-hide"
data-auth-add="{:auth('{{controllerUrl}}/add')}"
data-auth-edit="{:auth('{{controllerUrl}}/edit')}"
data-auth-delete="{:auth('{{controllerUrl}}/delete')}"
lay-filter="currentTable">
</table>
</div>
</div>

View File

@@ -0,0 +1,7 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
{{define}}
</div>
</div>

View File

@@ -0,0 +1,3 @@
{foreach ${{name}} as $k=>$v}
<input type="checkbox" name="{{field}}[]" value="{$k}" lay-skin="primary" title="{$v}" {{select}}>
{/foreach}

View File

@@ -0,0 +1,7 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
<input type="text" name="{{field}}" data-date="" data-date-type="{{define}}" class="layui-input" {{required}} placeholder="请输入{{comment}}" value="{{value}}">
</div>
</div>

View File

@@ -0,0 +1,7 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
<textarea name="{{field}}" rows="20" class="layui-textarea editor" {{comment}} placeholder="请输入{{comment}}">{{value}}</textarea>
</div>
</div>

View File

@@ -0,0 +1,11 @@
<div class="layui-form-item">
<label class="layui-form-label required">{{comment}}</label>
<div class="layui-input-block layuimini-upload">
<input name="{{field}}" class="layui-input layui-col-xs6" {{required}} placeholder="请上传{{comment}}" value="{{value}}">
<div class="layuimini-upload-btn">
<span><a class="layui-btn" data-upload="{{field}}" data-upload-number="one" data-upload-exts="*" data-upload-icon="file"><i class="fa fa-upload"></i> 上传</a></span>
<span><a class="layui-btn layui-btn-normal" id="select_{{field}}" data-upload-select="{{field}}" data-upload-number="one" data-upload-mimetype="*"><i class="fa fa-list"></i> 选择</a></span>
</div>
</div>
</div>

View File

@@ -0,0 +1,11 @@
<div class="layui-form-item">
<label class="layui-form-label required">{{comment}}</label>
<div class="layui-input-block layuimini-upload">
<input name="{{field}}" class="layui-input layui-col-xs6" {{required}} placeholder="请上传{{comment}}" value="{{value}}">
<div class="layuimini-upload-btn">
<span><a class="layui-btn" data-upload="{{field}}" data-upload-number="more" data-upload-exts="*" data-upload-icon="file"><i class="fa fa-upload" data-upload-sign="{{define}}"></i> 上传</a></span>
<span><a class="layui-btn layui-btn-normal" id="select_{{field}}" data-upload-select="{{field}}" data-upload-number="more" data-upload-mimetype="*" data-upload-sign="{{define}}"><i class="fa fa-list"></i> 选择</a></span>
</div>
</div>
</div>

View File

@@ -0,0 +1,11 @@
<div class="layui-form-item">
<label class="layui-form-label required">{{comment}}</label>
<div class="layui-input-block layuimini-upload">
<input name="{{field}}" class="layui-input layui-col-xs6" {{required}} placeholder="请上传{{comment}}" value="{{value}}">
<div class="layuimini-upload-btn">
<span><a class="layui-btn" data-upload="{{field}}" data-upload-number="one" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
<span><a class="layui-btn layui-btn-normal" id="select_{{field}}" data-upload-select="{{field}}" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span>
</div>
</div>
</div>

View File

@@ -0,0 +1,11 @@
<div class="layui-form-item">
<label class="layui-form-label required">{{comment}}</label>
<div class="layui-input-block layuimini-upload">
<input name="{{field}}" class="layui-input layui-col-xs6" {{required}} placeholder="请上传{{comment}}" value="{{value}}">
<div class="layuimini-upload-btn">
<span><a class="layui-btn" data-upload="{{field}}" data-upload-number="more" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image" data-upload-sign="{{define}}"><i class="fa fa-upload"></i> 上传</a></span>
<span><a class="layui-btn layui-btn-normal" id="select_{{field}}" data-upload-select="{{field}}" data-upload-number="more" data-upload-mimetype="image/*" data-upload-sign="{{define}}"><i class="fa fa-list"></i> 选择</a></span>
</div>
</div>
</div>

View File

@@ -0,0 +1,7 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
<input type="text" name="{{field}}" class="layui-input" {{required}} placeholder="请输入{{comment}}" value="{{value}}">
</div>
</div>

View File

@@ -0,0 +1,4 @@
<option value=''></option>
{foreach ${{name}} as $k=>$v}
<option value='{$k}' {{select}}>{$v}</option>
{/foreach}

View File

@@ -0,0 +1,7 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
{{define}}
</div>
</div>

View File

@@ -0,0 +1,3 @@
{foreach ${{name}} as $k=>$v}
<input type="radio" name="{{field}}" value="{$k}" title="{$v}" {{select}}>
{/foreach}

View File

@@ -0,0 +1,9 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
<select name="{{field}}" {{required}}>
{{define}}
</select>
</div>
</div>

View File

@@ -0,0 +1,7 @@
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
<textarea name="{{field}}" class="layui-textarea" {{required}} placeholder="请输入{{comment}}">{{value}}</textarea>
</div>
</div>

View File

@@ -14,6 +14,7 @@ namespace app\common\command;
use app\admin\model\SystemNode;
use app\admin\service\curd\BuildCurdService;
use EasyAdmin\console\CliEcho;
use app\common\tools\BuildCurdTools;
use think\console\Command;
@@ -55,7 +56,7 @@ class Curd extends Command
}
try {
$build = (new BuildCurdTools())
$build = (new BuildCurdService())
->setTable($table)
->setForce($force);

View File

@@ -1,260 +0,0 @@
<?php
namespace app\common\tools;
use EasyAdmin\curd\BuildCurd;
use EasyAdmin\tool\CommonTool;
class BuildCurdTools extends BuildCurd
{
/**
* 表单类型
* @var array
*/
protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation'];
public function getTableColumns()
{
return $this->tableColumns;
}
/**
* 构建初始化字段信息
* @param $colum
* @return mixed
*/
protected function buildColum(&$colum)
{
$string = $colum['comment'];
// 处理定义类型
preg_match('/{[\s\S]*?}/i', $string, $formTypeMatch);
if (!empty($formTypeMatch) && isset($formTypeMatch[0])) {
$colum['comment'] = str_replace($formTypeMatch[0], '', $colum['comment']);
$formType = trim(str_replace('}', '', str_replace('{', '', $formTypeMatch[0])));
if (in_array($formType, $this->formTypeArray)) {
$colum['formType'] = $formType;
}
}
// 处理默认定义
preg_match('/\([\s\S]*?\)/i', $string, $defineMatch);
if (!empty($formTypeMatch) && isset($defineMatch[0])) {
$colum['comment'] = str_replace($defineMatch[0], '', $colum['comment']);
if (isset($colum['formType']) && in_array($colum['formType'], ['images', 'files', 'select', 'switch', 'radio', 'checkbox', 'date', 'relation'])) {
$define = str_replace(')', '', str_replace('(', '', $defineMatch[0]));
if (in_array($colum['formType'], ['select', 'switch', 'radio', 'checkbox', 'relation'])) {
$formatDefine = [];
$explodeArray = explode(',', $define);
foreach ($explodeArray as $vo) {
$voExplodeArray = explode(':', $vo);
if (count($voExplodeArray) == 2) {
$formatDefine[trim($voExplodeArray[0])] = trim($voExplodeArray[1]);
}
}
!empty($formatDefine) && $colum['define'] = $formatDefine;
} else {
$colum['define'] = $define;
}
}
}
$colum['comment'] = trim($colum['comment']);
return $colum;
}
/**
* 初始化视图
* @return $this
*/
protected function renderView()
{
// 列表页面
$viewIndexFile = "{$this->rootDir}app{$this->DS}admin{$this->DS}view{$this->DS}{$this->viewFilename}{$this->DS}index.html";
$viewIndexValue = CommonTool::replaceTemplate(
$this->getTemplate("view{$this->DS}index"),
[
'controllerUrl' => $this->controllerUrl,
]
);
$this->fileList[$viewIndexFile] = $viewIndexValue;
// 添加页面
$viewAddFile = "{$this->rootDir}app{$this->DS}admin{$this->DS}view{$this->DS}{$this->viewFilename}{$this->DS}add.html";
$addFormList = '';
foreach ($this->tableColumns as $field => $val) {
if (in_array($field, ['id', 'create_time'])) {
continue;
}
$templateFile = "view{$this->DS}module{$this->DS}input";
$define = '';
// 根据formType去获取具体模板
if ($val['formType'] == 'image') {
$templateFile = "view{$this->DS}module{$this->DS}image";
} elseif ($val['formType'] == 'images') {
$templateFile = "view{$this->DS}module{$this->DS}images";
$define = isset($val['define']) ? $val['define'] : '|';
} elseif ($val['formType'] == 'file') {
$templateFile = "view{$this->DS}module{$this->DS}file";
} elseif ($val['formType'] == 'files') {
$templateFile = "view{$this->DS}module{$this->DS}files";
$define = isset($val['define']) ? $val['define'] : '|';
} elseif ($val['formType'] == 'editor') {
$templateFile = "view{$this->DS}module{$this->DS}editor";
} elseif ($val['formType'] == 'date') {
$templateFile = "view{$this->DS}module{$this->DS}date";
if (isset($val['define']) && !empty($val['define'])) {
$define = $val['define'];
} else {
$define = 'datetime';
}
if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) {
$define = 'datetime';
}
} elseif ($val['formType'] == 'radio') {
$templateFile = "view{$this->DS}module{$this->DS}radio";
if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildRadioView($field, '{in name="k" value="' . $val['default'] . '"}checked=""{/in}');
}
} elseif ($val['formType'] == 'checkbox') {
$templateFile = "view{$this->DS}module{$this->DS}checkbox";
if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildCheckboxView($field, '{in name="k" value="' . $val['default'] . '"}checked=""{/in}');
}
} elseif ($val['formType'] == 'select') {
$templateFile = "view{$this->DS}module{$this->DS}select";
if (isset($val['bindRelation'])) {
$define = $this->buildOptionView($val['bindRelation']);
} elseif (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildOptionView($field);
}
} elseif (in_array($field, ['remark']) || $val['formType'] == 'textarea') {
$templateFile = "view{$this->DS}module{$this->DS}textarea";
} elseif ($val['formType'] == 'relation') {
// 使用select生成
$templateFile = "view{$this->DS}module{$this->DS}select";
if (isset($val['bindRelation'])) {
$define = $this->buildOptionView($val['bindRelation']);
} elseif (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildOptionView($field);
}
}
$addFormList .= CommonTool::replaceTemplate(
$this->getTemplate($templateFile),
[
'comment' => $val['comment'],
'field' => $field,
'required' => $this->buildRequiredHtml($val['required']),
'value' => $val['default'],
'define' => $define,
]
);
}
$viewAddValue = CommonTool::replaceTemplate(
$this->getTemplate("view{$this->DS}form"),
[
'formList' => $addFormList,
]
);
$this->fileList[$viewAddFile] = $viewAddValue;
// 编辑页面
$viewEditFile = "{$this->rootDir}app{$this->DS}admin{$this->DS}view{$this->DS}{$this->viewFilename}{$this->DS}edit.html";
$editFormList = '';
foreach ($this->tableColumns as $field => $val) {
if (in_array($field, ['id', 'create_time'])) {
continue;
}
$templateFile = "view{$this->DS}module{$this->DS}input";
$define = '';
$value = '{$row.' . $field . '|default=\'\'}';
// 根据formType去获取具体模板
if ($val['formType'] == 'image') {
$templateFile = "view{$this->DS}module{$this->DS}image";
} elseif ($val['formType'] == 'images') {
$templateFile = "view{$this->DS}module{$this->DS}images";
} elseif ($val['formType'] == 'file') {
$templateFile = "view{$this->DS}module{$this->DS}file";
} elseif ($val['formType'] == 'files') {
$templateFile = "view{$this->DS}module{$this->DS}files";
} elseif ($val['formType'] == 'editor') {
$templateFile = "view{$this->DS}module{$this->DS}editor";
$value = '{$row.' . $field . '|raw|default=\'\'}';
} elseif ($val['formType'] == 'date') {
$templateFile = "view{$this->DS}module{$this->DS}date";
if (isset($val['define']) && !empty($val['define'])) {
$define = $val['define'];
} else {
$define = 'datetime';
}
if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) {
$define = 'datetime';
}
} elseif ($val['formType'] == 'radio') {
$templateFile = "view{$this->DS}module{$this->DS}radio";
if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildRadioView($field, '{in name="k" value="$row.' . $field . '"}checked=""{/in}');
}
} elseif ($val['formType'] == 'checkbox') {
$templateFile = "view{$this->DS}module{$this->DS}checkbox";
if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildCheckboxView($field, '{in name="k" value="$row.' . $field . '"}checked=""{/in}');
}
} elseif ($val['formType'] == 'select') {
$templateFile = "view{$this->DS}module{$this->DS}select";
if (isset($val['bindRelation'])) {
$define = $this->buildOptionView($val['bindRelation'], '{in name="k" value="$row.' . $field . '"}selected=""{/in}');
} elseif (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildOptionView($field, '{in name="k" value="$row.' . $field . '"}selected=""{/in}');
}
} elseif (in_array($field, ['remark']) || $val['formType'] == 'textarea') {
$templateFile = "view{$this->DS}module{$this->DS}textarea";
$value = '{$row.' . $field . '|raw|default=\'\'}';
} elseif ($val['formType'] == 'relation') {
// 使用select生成
$templateFile = "view{$this->DS}module{$this->DS}select";
if (isset($val['bindRelation'])) {
$define = $this->buildOptionView($val['bindRelation']);
} elseif (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildOptionView($field);
}
}
$editFormList .= CommonTool::replaceTemplate(
$this->getTemplate($templateFile),
[
'comment' => $val['comment'],
'field' => $field,
'required' => $this->buildRequiredHtml($val['required']),
'value' => $value,
'define' => $define,
]
);
}
$viewEditValue = CommonTool::replaceTemplate(
$this->getTemplate("view{$this->DS}form"),
[
'formList' => $editFormList,
]
);
$this->fileList[$viewEditFile] = $viewEditValue;
return $this;
}
}

View File

@@ -34,7 +34,7 @@ class MallGoods extends Migrator
->addColumn(Column::char('title', 20)->setDefault('')->setComment('商品名称'))
->addColumn(Column::char('logo')->setComment('商品logo {image}'))
->addColumn(Column::text('images')->setComment('商品图片 {images}'))
->addColumn(Column::text('describe')->setComment('商品描述'))
->addColumn(Column::text('describe')->setComment('商品描述 {editor}'))
->addColumn(Column::decimal('market_price')->setDefault(0)->setComment('市场价'))
->addColumn(Column::decimal('discount_price')->setDefault(0)->setComment('折扣价'))
->addColumn(Column::integer('sales')->setUnsigned()->setDefault(0)->setComment('销量'))