This commit is contained in:
2022-09-21 11:44:43 +08:00
parent 4d3793e751
commit be5e6a3f1e
+80 -26
View File
@@ -110,7 +110,7 @@ use {$model_namespace}\\{$managerName};
$html .= $this->getClassMethods($class); $html .= $this->getClassMethods($class);
} }
$default = ['loadParam', 'actionAdd', 'actionUpdate', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList']; $default = ['loadParam', 'actionAdd', 'actionUpdate', 'actionAuditing', 'actionBatchAuditing', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList'];
foreach ($default as $key => $val) { foreach ($default as $key => $val) {
if (str_contains($html, ' function ' . $val . '(')) { if (str_contains($html, ' function ' . $val . '(')) {
@@ -122,6 +122,21 @@ use {$model_namespace}\\{$managerName};
$html .= ' $html .= '
}'; }';
$tableName = str_replace('_', '-', $this->input->getOption('table'));
Kiri::getLogger()->debug('add Route:');
Kiri::getLogger()->debug('
Router::group([\'prefix\' => \'' . $tableName . '\'], function () {
Router::get(\'add\', \'' . $controllerName . 'Controller@actionAdd\');
Router::get(\'list\', \'' . $controllerName . 'Controller@actionList\');
Router::post(\'update\', \'' . $controllerName . 'Controller@actionUpdate\');
Router::post(\'auditing\', \'' . $controllerName . 'Controller@actionAuditing\');
Router::post(\'batch-auditing\', \'' . $controllerName . 'Controller@actionBatchAuditing\');
Router::post(\'batch-delete\', \'' . $controllerName . 'Controller@actionBatchDelete\');
Router::post(\'delete\', \'' . $controllerName . 'Controller@actionDelete\');
Router::post(\'detail\', \'' . $controllerName . 'Controller@actionDetail\');
});');
$file = $path['path'] . '/' . $controllerName . 'Controller.php'; $file = $path['path'] . '/' . $controllerName . 'Controller.php';
if (file_exists($file)) { if (file_exists($file)) {
unlink($file); unlink($file);
@@ -189,12 +204,58 @@ use {$model_namespace}\\{$managerName};
$model = new ' . $className . '(); $model = new ' . $className . '();
$model->attributes = $this->loadParam(); $model->attributes = $this->loadParam();
if (!$model->save()) { if (!$model->save()) {
return JSON::to(500, $model->getLastError()); return Json::to(500, $model->getLastError());
} else {
return Json::to(0, $model->toArray());
} }
return JSON::to(0, $model->toArray());
}'; }';
} }
public function controllerMethodAuditing($fields, $className, $object, $path): string
{
return '
/**
* @return string
* @throws Exception
*/
public function actionAuditing(): string
{
$model = ' . $className . '::findOne($this->request->post(\'id\', 0));
if (empty($model)) {
return Json::to(500, SELECT_IS_NULL);
}
if (!$model->update([\'state\' => 1])) {
return Json::to(500, $model->getLastError());
} else {
return Json::to(0, $model->toArray());
}
}';
}
public function controllerMethodBatchAuditing($fields, $className, $object, $path): string
{
return '
/**
* @return string
* @throws Exception
*/
public function actionAuditing(): string
{
$ids = $this->request->array(\'ids\', []);
if (empty($ids)) {
return JSON::to(404, \'必填项不能为空\');
}
if (!' . $className . '::query()->whereIn(\'id\', $ids)->update([\'state\' => 1])) {
return Json::to(500, \'系统繁忙, 请稍后再试\');
} else {
return Json::to(0);
}
}';
}
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
@@ -237,20 +298,18 @@ use {$model_namespace}\\{$managerName};
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
#[Route(uri: "' . $_path . '/update", method: RequestMethod::REQUEST_POST)]
#[Middleware(middleware: [CoreMiddleware::class, OAuthMiddleware::class])]
public function actionUpdate(): string public function actionUpdate(): string
{ {
$model = ' . $className . '::findOne($this->request->post(\'id\', 0)); $model = ' . $className . '::findOne($this->request->post(\'id\', 0));
if (empty($model)) { if (empty($model)) {
return JSON::to(500, SELECT_IS_NULL); return Json::to(500, SELECT_IS_NULL);
} }
$model->attributes = $this->loadParam(); $model->attributes = $this->loadParam();
if (!$model->save()) { if (!$model->save()) {
return JSON::to(500, $model->getLastError()); return Json::to(500, $model->getLastError());
} else {
return Json::to(0, $model->toArray());
} }
return JSON::to(0, $model->toArray());
}'; }';
} }
@@ -274,20 +333,19 @@ use {$model_namespace}\\{$managerName};
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
#[Route(uri: "' . $_path . '/batch-delete", method: RequestMethod::REQUEST_POST)]
#[Middleware(middleware: [CoreMiddleware::class, OAuthMiddleware::class])]
public function actionBatchDelete(): string public function actionBatchDelete(): string
{ {
$_key = $this->request->array(\'ids\'); $_key = $this->request->array(\'ids\');
if (empty($_key)) { if (empty($_key)) {
return JSON::to(500, PARAMS_IS_NULL); return Json::to(500, PARAMS_IS_NULL);
} }
$model = ' . $className . '::query()->whereIn(\'id\', $_key); $model = ' . $className . '::query()->whereIn(\'id\', $_key);
if (!$model->delete()) { if (!$model->delete()) {
return JSON::to(500, DB_ERROR_BUSY); return Json::to(500, DB_ERROR_BUSY);
} else {
return Json::to(0, $_key);
} }
return JSON::to(0, $_key);
}'; }';
} }
@@ -311,15 +369,14 @@ use {$model_namespace}\\{$managerName};
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
#[Route(uri: "' . $_path . '/detail", method: RequestMethod::REQUEST_POST)]
#[Middleware(middleware: [CoreMiddleware::class, OAuthMiddleware::class])]
public function actionDetail(): string public function actionDetail(): string
{ {
$model = ' . $managerName . '::findOne($this->request->query(\'id\')); $model = ' . $managerName . '::findOne($this->request->query(\'id\'));
if (empty($model)) { if (empty($model)) {
return JSON::to(404, SELECT_IS_NULL); return Json::to(404, SELECT_IS_NULL);
} else {
return Json::to(0, $model->toArray());
} }
return JSON::to(0, $model->toArray());
}'; }';
} }
@@ -343,20 +400,19 @@ use {$model_namespace}\\{$managerName};
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
#[Route(uri: "' . $_path . '/delete", method: RequestMethod::REQUEST_POST)]
#[Middleware(middleware: [CoreMiddleware::class, OAuthMiddleware::class])]
public function actionDelete(): string public function actionDelete(): string
{ {
$_key = $this->request->int(\'id\', true); $_key = $this->request->int(\'id\', true);
$model = ' . $managerName . '::findOne($_key); $model = ' . $managerName . '::findOne($_key);
if (empty($model)) { if (empty($model)) {
return JSON::to(500, SELECT_IS_NULL); return Json::to(500, SELECT_IS_NULL);
} }
if (!$model->delete()) { if (!$model->delete()) {
return JSON::to(500, $model->getLastError()); return Json::to(500, $model->getLastError());
} else {
return Json::to(0);
} }
return JSON::to(0);
}'; }';
} }
@@ -380,8 +436,6 @@ use {$model_namespace}\\{$managerName};
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
#[Route(uri: "' . $_path . '/list", method: RequestMethod::REQUEST_GET)]
#[Middleware(middleware: [CoreMiddleware::class, OAuthMiddleware::class])]
public function actionList(): string public function actionList(): string
{ {
//分页处理 //分页处理
@@ -411,7 +465,7 @@ use {$model_namespace}\\{$managerName};
$data = $model->all()->toArray(); $data = $model->all()->toArray();
return JSON::to(0, $data, $count); return Json::to(0, $data, $count);
} }
'; ';
} }