Skip to content

Commit b9b3377

Browse files
author
zhaoxiang
committed
added 新增自动构建脚本
1 parent a303444 commit b9b3377

File tree

2 files changed

+222
-7
lines changed

2 files changed

+222
-7
lines changed

app/util/AutoBuild.php

Lines changed: 142 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
namespace app\util;
99

1010

11+
use app\model\AdminMenu;
12+
use think\facade\Db;
13+
1114
class AutoBuild {
1215

1316
private $config = [
@@ -22,16 +25,34 @@ class AutoBuild {
2225
'fid' => 0 // 父级ID
2326
];
2427

25-
private $basePath = '';
26-
28+
/**
29+
* 自动构建
30+
* @param array $config
31+
* @throws \think\db\exception\DataNotFoundException
32+
* @throws \think\db\exception\DbException
33+
* @throws \think\db\exception\ModelNotFoundException
34+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
35+
*/
2736
public function run($config = []) {
2837
$this->config = array_merge($this->config, $config);
2938

30-
if ($this->config['module'] == 1) {
39+
if ($this->config['model'] == 1) {
40+
$this->buildModel();
3141

42+
if ($this->config['table'] == 1) {
43+
$this->createTable();
44+
}
3245
}
3346
if ($this->config['control'] && $this->config['name']) {
3447
$this->buildControl();
48+
49+
if ($this->config['menu'] && $this->config['module'] == 1) {
50+
$this->buildMenu();
51+
}
52+
53+
if ($this->config['route'] && $this->config['module'] == 1) {
54+
$this->buildRoute();
55+
}
3556
}
3657
}
3758

@@ -46,23 +67,137 @@ private function unCamelize($camelCaps, $separator = '_'): string {
4667
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
4768
}
4869

70+
/**
71+
* 构建控制器
72+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
73+
*/
4974
private function buildControl() {
75+
$tplPath = root_path() . 'install' . DIRECTORY_SEPARATOR;
76+
if ($this->config['module'] == 1) {
77+
$module = 'admin';
78+
} else {
79+
$module = 'api';
80+
}
5081

82+
$controlStr = str_replace(
83+
['{$MODULE}', '{$NAME}'],
84+
[$module, $this->config['name']],
85+
file_get_contents($tplPath . 'control.tpl')
86+
);
87+
file_put_contents(
88+
base_path() . 'controller' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . $this->config['name'] . '.php',
89+
$controlStr
90+
);
5191
}
5292

93+
/**
94+
* 构建模型
95+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
96+
*/
5397
private function buildModel() {
54-
98+
$modelStr = '<?php' . PHP_EOL;
99+
$modelStr .= '/**' . PHP_EOL;
100+
$modelStr .= ' * 由ApiAdmin自动构建' . PHP_EOL;
101+
$modelStr .= ' * @author apiadmin <apiadmin.org>' . PHP_EOL;
102+
$modelStr .= ' */' . PHP_EOL;
103+
$modelStr .= 'namespace app\model;' . PHP_EOL;
104+
$modelStr .= 'class ' . $this->config['modelName'] . ' extends Base {' . PHP_EOL;
105+
$modelStr .= '}' . PHP_EOL;
106+
107+
file_put_contents(
108+
base_path() . 'model' . DIRECTORY_SEPARATOR . $this->config['modelName'] . '.php',
109+
$modelStr
110+
);
55111
}
56112

113+
/**
114+
* 构建表
115+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
116+
*/
57117
private function createTable() {
58-
118+
$tableName = $this->unCamelize($this->config['modelName']);
119+
$cmd = "CREATE TABLE `{$tableName}` (`id` int NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`)) COMMENT='由ApiAdmin自动构建';";
120+
Db::execute($cmd);
59121
}
60122

123+
/**
124+
* 构建菜单
125+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
126+
*/
61127
private function buildMenu() {
62-
128+
$menus = [
129+
[
130+
'title' => '新增',
131+
'fid' => $this->config['fid'],
132+
'url' => "admin/{$this->config['name']}/add",
133+
'auth' => 1,
134+
'sort' => 0,
135+
'show' => 1,
136+
'icon' => '',
137+
'level' => 3,
138+
'component' => '',
139+
'router' => '',
140+
'log' => 1,
141+
'permission' => 1,
142+
'method' => 2
143+
],
144+
[
145+
'title' => '编辑',
146+
'fid' => $this->config['fid'],
147+
'url' => "admin/{$this->config['name']}/edit",
148+
'auth' => 1,
149+
'sort' => 0,
150+
'show' => 1,
151+
'icon' => '',
152+
'level' => 3,
153+
'component' => '',
154+
'router' => '',
155+
'log' => 1,
156+
'permission' => 1,
157+
'method' => 2
158+
],
159+
[
160+
'title' => '删除',
161+
'fid' => $this->config['fid'],
162+
'url' => "admin/{$this->config['name']}/del",
163+
'auth' => 1,
164+
'sort' => 0,
165+
'show' => 1,
166+
'icon' => '',
167+
'level' => 3,
168+
'component' => '',
169+
'router' => '',
170+
'log' => 1,
171+
'permission' => 1,
172+
'method' => 1
173+
],
174+
[
175+
'title' => '列表',
176+
'fid' => $this->config['fid'],
177+
'url' => "admin/{$this->config['name']}/index",
178+
'auth' => 1,
179+
'sort' => 0,
180+
'show' => 1,
181+
'icon' => '',
182+
'level' => 3,
183+
'component' => '',
184+
'router' => '',
185+
'log' => 1,
186+
'permission' => 1,
187+
'method' => 1
188+
]
189+
];
190+
(new AdminMenu())->insertAll($menus);
63191
}
64192

193+
/**
194+
* 构建路由
195+
* @throws \think\db\exception\DataNotFoundException
196+
* @throws \think\db\exception\DbException
197+
* @throws \think\db\exception\ModelNotFoundException
198+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
199+
*/
65200
private function buildRoute() {
66-
201+
RouterTool::buildAdminRouter();
67202
}
68203
}

install/control.tpl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
declare (strict_types=1);
3+
/**
4+
* 由ApiAdmin自动构建,请处理{$MODEL_NAME}
5+
* @author apiadmin <apiadmin.org>
6+
*/
7+
8+
namespace app\controller\{$MODULE};
9+
10+
use app\model\{$MODEL_NAME};
11+
use app\util\ReturnCode;
12+
use think\Response;
13+
14+
class {$NAME} extends Base {
15+
/**
16+
* 获取
17+
* @return \think\Response
18+
* @throws \think\db\exception\DbException
19+
* @author apiadmin <apiadmin.org>
20+
*/
21+
public function index(): Response {
22+
$limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
23+
$start = $this->request->get('page', 1);
24+
25+
$obj = new {$MODEL_NAME}();
26+
$listObj = $obj->paginate(['page' => $start, 'list_rows' => $limit])->toArray();
27+
28+
return $this->buildSuccess([
29+
'list' => $listObj['data'],
30+
'count' => $listObj['total']
31+
]);
32+
}
33+
34+
/**
35+
* 添加
36+
* @return Response
37+
* @author apiadmin <apiadmin.org>
38+
*/
39+
public function add(): Response {
40+
$postData = $this->request->post();
41+
$res = {$MODEL_NAME}::create($postData);
42+
if ($res === false) {
43+
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
44+
}
45+
46+
return $this->buildSuccess();
47+
}
48+
49+
/**
50+
* 编辑
51+
* @return Response
52+
* @author apiadmin <apiadmin.org>
53+
*/
54+
public function edit(): Response {
55+
$postData = $this->request->post();
56+
$res = {$MODEL_NAME}::update($postData);
57+
if ($res === false) {
58+
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
59+
}
60+
61+
return $this->buildSuccess();
62+
}
63+
64+
/**
65+
* 删除
66+
* @return Response
67+
* @author apiadmin <apiadmin.org>
68+
*/
69+
public function del(): Response {
70+
$id = $this->request->get('id');
71+
if (!$id) {
72+
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
73+
}
74+
75+
// 请处理部分删除数据
76+
{$MODEL_NAME}::destroy(['id' => $id]);
77+
78+
return $this->buildSuccess();
79+
}
80+
}

0 commit comments

Comments
 (0)