mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
added 新增自动构建脚本
This commit is contained in:
parent
a303444eaa
commit
b9b33772b9
@ -8,6 +8,9 @@
|
|||||||
namespace app\util;
|
namespace app\util;
|
||||||
|
|
||||||
|
|
||||||
|
use app\model\AdminMenu;
|
||||||
|
use think\facade\Db;
|
||||||
|
|
||||||
class AutoBuild {
|
class AutoBuild {
|
||||||
|
|
||||||
private $config = [
|
private $config = [
|
||||||
@ -22,16 +25,34 @@ class AutoBuild {
|
|||||||
'fid' => 0 // 父级ID
|
'fid' => 0 // 父级ID
|
||||||
];
|
];
|
||||||
|
|
||||||
private $basePath = '';
|
/**
|
||||||
|
* 自动构建
|
||||||
|
* @param array $config
|
||||||
|
* @throws \think\db\exception\DataNotFoundException
|
||||||
|
* @throws \think\db\exception\DbException
|
||||||
|
* @throws \think\db\exception\ModelNotFoundException
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||||
|
*/
|
||||||
public function run($config = []) {
|
public function run($config = []) {
|
||||||
$this->config = array_merge($this->config, $config);
|
$this->config = array_merge($this->config, $config);
|
||||||
|
|
||||||
if ($this->config['module'] == 1) {
|
if ($this->config['model'] == 1) {
|
||||||
|
$this->buildModel();
|
||||||
|
|
||||||
|
if ($this->config['table'] == 1) {
|
||||||
|
$this->createTable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($this->config['control'] && $this->config['name']) {
|
if ($this->config['control'] && $this->config['name']) {
|
||||||
$this->buildControl();
|
$this->buildControl();
|
||||||
|
|
||||||
|
if ($this->config['menu'] && $this->config['module'] == 1) {
|
||||||
|
$this->buildMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->config['route'] && $this->config['module'] == 1) {
|
||||||
|
$this->buildRoute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,23 +67,137 @@ class AutoBuild {
|
|||||||
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
|
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建控制器
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||||
|
*/
|
||||||
private function buildControl() {
|
private function buildControl() {
|
||||||
|
$tplPath = root_path() . 'install' . DIRECTORY_SEPARATOR;
|
||||||
|
if ($this->config['module'] == 1) {
|
||||||
|
$module = 'admin';
|
||||||
|
} else {
|
||||||
|
$module = 'api';
|
||||||
|
}
|
||||||
|
|
||||||
|
$controlStr = str_replace(
|
||||||
|
['{$MODULE}', '{$NAME}'],
|
||||||
|
[$module, $this->config['name']],
|
||||||
|
file_get_contents($tplPath . 'control.tpl')
|
||||||
|
);
|
||||||
|
file_put_contents(
|
||||||
|
base_path() . 'controller' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . $this->config['name'] . '.php',
|
||||||
|
$controlStr
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建模型
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||||
|
*/
|
||||||
private function buildModel() {
|
private function buildModel() {
|
||||||
|
$modelStr = '<?php' . PHP_EOL;
|
||||||
|
$modelStr .= '/**' . PHP_EOL;
|
||||||
|
$modelStr .= ' * 由ApiAdmin自动构建' . PHP_EOL;
|
||||||
|
$modelStr .= ' * @author apiadmin <apiadmin.org>' . PHP_EOL;
|
||||||
|
$modelStr .= ' */' . PHP_EOL;
|
||||||
|
$modelStr .= 'namespace app\model;' . PHP_EOL;
|
||||||
|
$modelStr .= 'class ' . $this->config['modelName'] . ' extends Base {' . PHP_EOL;
|
||||||
|
$modelStr .= '}' . PHP_EOL;
|
||||||
|
|
||||||
|
file_put_contents(
|
||||||
|
base_path() . 'model' . DIRECTORY_SEPARATOR . $this->config['modelName'] . '.php',
|
||||||
|
$modelStr
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建表
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||||
|
*/
|
||||||
private function createTable() {
|
private function createTable() {
|
||||||
|
$tableName = $this->unCamelize($this->config['modelName']);
|
||||||
|
$cmd = "CREATE TABLE `{$tableName}` (`id` int NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`)) COMMENT='由ApiAdmin自动构建';";
|
||||||
|
Db::execute($cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建菜单
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||||
|
*/
|
||||||
private function buildMenu() {
|
private function buildMenu() {
|
||||||
|
$menus = [
|
||||||
|
[
|
||||||
|
'title' => '新增',
|
||||||
|
'fid' => $this->config['fid'],
|
||||||
|
'url' => "admin/{$this->config['name']}/add",
|
||||||
|
'auth' => 1,
|
||||||
|
'sort' => 0,
|
||||||
|
'show' => 1,
|
||||||
|
'icon' => '',
|
||||||
|
'level' => 3,
|
||||||
|
'component' => '',
|
||||||
|
'router' => '',
|
||||||
|
'log' => 1,
|
||||||
|
'permission' => 1,
|
||||||
|
'method' => 2
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => '编辑',
|
||||||
|
'fid' => $this->config['fid'],
|
||||||
|
'url' => "admin/{$this->config['name']}/edit",
|
||||||
|
'auth' => 1,
|
||||||
|
'sort' => 0,
|
||||||
|
'show' => 1,
|
||||||
|
'icon' => '',
|
||||||
|
'level' => 3,
|
||||||
|
'component' => '',
|
||||||
|
'router' => '',
|
||||||
|
'log' => 1,
|
||||||
|
'permission' => 1,
|
||||||
|
'method' => 2
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => '删除',
|
||||||
|
'fid' => $this->config['fid'],
|
||||||
|
'url' => "admin/{$this->config['name']}/del",
|
||||||
|
'auth' => 1,
|
||||||
|
'sort' => 0,
|
||||||
|
'show' => 1,
|
||||||
|
'icon' => '',
|
||||||
|
'level' => 3,
|
||||||
|
'component' => '',
|
||||||
|
'router' => '',
|
||||||
|
'log' => 1,
|
||||||
|
'permission' => 1,
|
||||||
|
'method' => 1
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => '列表',
|
||||||
|
'fid' => $this->config['fid'],
|
||||||
|
'url' => "admin/{$this->config['name']}/index",
|
||||||
|
'auth' => 1,
|
||||||
|
'sort' => 0,
|
||||||
|
'show' => 1,
|
||||||
|
'icon' => '',
|
||||||
|
'level' => 3,
|
||||||
|
'component' => '',
|
||||||
|
'router' => '',
|
||||||
|
'log' => 1,
|
||||||
|
'permission' => 1,
|
||||||
|
'method' => 1
|
||||||
|
]
|
||||||
|
];
|
||||||
|
(new AdminMenu())->insertAll($menus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建路由
|
||||||
|
* @throws \think\db\exception\DataNotFoundException
|
||||||
|
* @throws \think\db\exception\DbException
|
||||||
|
* @throws \think\db\exception\ModelNotFoundException
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||||
|
*/
|
||||||
private function buildRoute() {
|
private function buildRoute() {
|
||||||
|
RouterTool::buildAdminRouter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
80
install/control.tpl
Normal file
80
install/control.tpl
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types=1);
|
||||||
|
/**
|
||||||
|
* 由ApiAdmin自动构建,请处理{$MODEL_NAME}!
|
||||||
|
* @author apiadmin <apiadmin.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace app\controller\{$MODULE};
|
||||||
|
|
||||||
|
use app\model\{$MODEL_NAME};
|
||||||
|
use app\util\ReturnCode;
|
||||||
|
use think\Response;
|
||||||
|
|
||||||
|
class {$NAME} extends Base {
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return \think\Response
|
||||||
|
* @throws \think\db\exception\DbException
|
||||||
|
* @author apiadmin <apiadmin.org>
|
||||||
|
*/
|
||||||
|
public function index(): Response {
|
||||||
|
$limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
|
||||||
|
$start = $this->request->get('page', 1);
|
||||||
|
|
||||||
|
$obj = new {$MODEL_NAME}();
|
||||||
|
$listObj = $obj->paginate(['page' => $start, 'list_rows' => $limit])->toArray();
|
||||||
|
|
||||||
|
return $this->buildSuccess([
|
||||||
|
'list' => $listObj['data'],
|
||||||
|
'count' => $listObj['total']
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加
|
||||||
|
* @return Response
|
||||||
|
* @author apiadmin <apiadmin.org>
|
||||||
|
*/
|
||||||
|
public function add(): Response {
|
||||||
|
$postData = $this->request->post();
|
||||||
|
$res = {$MODEL_NAME}::create($postData);
|
||||||
|
if ($res === false) {
|
||||||
|
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->buildSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑
|
||||||
|
* @return Response
|
||||||
|
* @author apiadmin <apiadmin.org>
|
||||||
|
*/
|
||||||
|
public function edit(): Response {
|
||||||
|
$postData = $this->request->post();
|
||||||
|
$res = {$MODEL_NAME}::update($postData);
|
||||||
|
if ($res === false) {
|
||||||
|
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->buildSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除
|
||||||
|
* @return Response
|
||||||
|
* @author apiadmin <apiadmin.org>
|
||||||
|
*/
|
||||||
|
public function del(): Response {
|
||||||
|
$id = $this->request->get('id');
|
||||||
|
if (!$id) {
|
||||||
|
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 请处理部分删除数据
|
||||||
|
{$MODEL_NAME}::destroy(['id' => $id]);
|
||||||
|
|
||||||
|
return $this->buildSuccess();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user