crud接口处理

This commit is contained in:
liaofei 2023-04-11 16:19:18 +08:00
parent fecf722ded
commit 36127a088b
4 changed files with 196 additions and 11 deletions

View File

@ -17,6 +17,7 @@ namespace app\adminapi\controller\v1\setting;
use app\adminapi\controller\AuthController;
use app\services\system\SystemCrudServices;
use think\facade\App;
use think\helper\Str;
/**
* Class SystemCrud
@ -39,11 +40,23 @@ class SystemCrud extends AuthController
$this->services = $services;
}
/**
* @return \think\Response
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function index()
{
return app('json')->success($this->services->getList());
}
/**
* @return \think\Response
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function save()
{
$data = $this->request->postMore([
@ -61,7 +74,69 @@ class SystemCrud extends AuthController
]);
$this->services->createCrud($data);
return app('json')->success('创建成功');
}
/**
* 获取创建文件的目录存放位置
* @return \think\Response
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function getFilePath()
{
[$menuName, $tableName, $fromField, $columnField] = $this->request->postMore([
['menuName', ''],
['tableName', ''],
['fromField', []],
['columnField', []],
], true);
$routeName = 'crud/' . Str::snake($tableName);
$make = $this->services->makeFile($tableName, $routeName, false, [
'menuName' => $menuName,
'fromField' => $fromField,
'columnField' => $columnField,
]);
$makePath = [];
foreach ($make as $key => $item) {
$makePath[$key] = $item['path'];
}
return app('json')->success($makePath);
}
/**
* 获取创建表数据类型
* @return \think\Response
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function columnType()
{
return app('json')->success($this->services->getTabelRule());
}
/**
* @param $id
* @return \think\Response
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function delete($id)
{
if (!$id) {
return app('json')->fail('缺少参数');
}
$this->services->delete($id);
return app('json')->success('删除成功');
}
}

View File

@ -131,7 +131,16 @@ Route::group('system', function () {
'delete' => '删除路由分类'
],
]);
//获取CRUD列表
Route::get('crud/column_type', 'v1.setting.SystemCrud/columnType')->option(['real_name' => '获取CRUD列表']);
//获取CRUD文件存放
Route::post('crud/file_path', 'v1.setting.SystemCrud/getFilePath')->option(['real_name' => '获取CRUD文件存放']);
//删除CRUD
Route::delete('crud/:id', 'v1.setting.SystemCrud/delete')->option(['real_name' => '删除CRUD']);
//获取CRUD列表
Route::get('crud', 'v1.setting.SystemCrud/index')->option(['real_name' => '获取CRUD列表']);
//保存生成CRUD
Route::post('crud', 'v1.setting.SystemCrud/save')->option(['real_name' => '保存生成CRUD']);
})->middleware([
\app\http\middleware\AllowOriginMiddleware::class,
\app\adminapi\middleware\AdminAuthTokenMiddleware::class,

View File

@ -27,6 +27,7 @@ use crmeb\services\crud\ViewPages;
use crmeb\services\crud\ViewRouter;
use think\exception\ValidateException;
use think\facade\Db;
use think\helper\Str;
use think\migration\Migrator;
use Phinx\Db\Adapter\MysqlAdapter;
@ -49,6 +50,21 @@ class SystemCrudServices extends BaseServices
$this->dao = $dao;
}
/**
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function getList()
{
[$page, $limit] = $this->getPageValue();
$list = $this->dao->selectList([], '*', $page, $limit, 'id desc');
$count = $this->dao->count();
return compact('list', 'count');
}
public function getTabelRule()
{
$adapter = app()->make(MysqlAdapter::class);
@ -90,6 +106,14 @@ class SystemCrudServices extends BaseServices
return $columns;
}
/**
* 创建
* @param array $data
* @return mixed
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/11
*/
public function createCrud(array $data)
{
$tableName = $data['tableName'];
@ -108,9 +132,8 @@ class SystemCrudServices extends BaseServices
throw new ValidateException('请先创建' . $tableName . '表');
}
//TODO 没写完
$routeName = '';
$uniqueAuth = '';
$routeName = 'crud/' . Str::snake($tableName);
$uniqueAuth = $routeName . '-index-list';
$make = $this->makeFile($tableName, $routeName, true, $data, $filePath);
$makePath = [];
@ -133,10 +156,72 @@ class SystemCrudServices extends BaseServices
];
$menuInfo = app()->make(SystemMenusServices::class)->save($data);
//写入路由权限
//TODO 没写完
$cateId = app()->make(SystemRouteServices::class)->topCateId('adminapi');
$ruleData = [
[
'path' => $routeName,
'method' => 'GET',
'name' => $data['menuName'] . '列表接口',
'app_name' => 'adminapi',
'cate_id' => $cateId,
'add_time' => date('Y-m-d H:i:s')
],
[
'path' => $routeName . '/create',
'method' => 'GET',
'name' => $data['menuName'] . '获取创建表单接口',
'app_name' => 'adminapi',
'cate_id' => $cateId,
'add_time' => date('Y-m-d H:i:s')
],
[
'path' => $routeName,
'method' => 'POST',
'name' => $data['menuName'] . '保存数据接口',
'app_name' => 'adminapi',
'cate_id' => $cateId,
'add_time' => date('Y-m-d H:i:s')
],
[
'path' => $routeName . '/<id>/edit',
'method' => 'GET',
'name' => $data['menuName'] . '获取修改表单接口',
'app_name' => 'adminapi',
'cate_id' => $cateId,
'add_time' => date('Y-m-d H:i:s')
],
[
'path' => $routeName . '/<id>',
'method' => 'PUT',
'name' => $data['menuName'] . '修改数据接口',
'app_name' => 'adminapi',
'cate_id' => $cateId,
'add_time' => date('Y-m-d H:i:s')
],
[
'path' => $routeName . '/<id>',
'method' => 'DELETE',
'name' => $data['menuName'] . '删除数据接口',
'app_name' => 'adminapi',
'cate_id' => $cateId,
'add_time' => date('Y-m-d H:i:s')
],
];
app()->make(SystemRouteServices::class)->saveAll($ruleData);
//记录权限加入菜单表
$menuData = [];
foreach ($ruleData as $item) {
$menuData[] = [
'pid' => $menuInfo->id,
'method' => $item['method'],
'api_url' => $item['path'],
'name' => $item['name'],
'is_del' => 0,
];
}
app()->make(SystemMenusServices::class)->saveAll($menuData);
//记录crud生成
$this->dao->save([
$res = $this->dao->save([
'pid' => $data['pid'],
'name' => $data['menuName'],
'table_name' => $tableName,
@ -144,6 +229,7 @@ class SystemCrudServices extends BaseServices
'make_path' => json_encode($makePath),
'add_time' => time()
]);
return $res->toArray();
}
/**

View File

@ -151,12 +151,14 @@ class SystemRouteServices extends BaseServices
}
/**
* 同步路由
* 获取顶级id
* @param string $app
* @return mixed
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/6
* @date 2023/4/11
*/
public function syncRoute(string $app = 'adminapi')
public function topCateId(string $app)
{
$id = app()->make(SystemRouteCateServices::class)->value(['app_name' => $app, 'name' => '全部权限', 'pid' => 0], 'id');
if (!$id) {
@ -168,6 +170,19 @@ class SystemRouteServices extends BaseServices
]);
$id = $res->id;
}
return $id;
}
/**
* 同步路由
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/6
*/
public function syncRoute(string $app = 'adminapi')
{
$id = $this->topCateId($app);
$listAll = $this->getRouteListAll($app);
//保持新增的权限路由
$data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();