diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php b/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php index 47cd222d0..f2ecd08d2 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php @@ -41,41 +41,163 @@ class SystemRoute extends AuthController /** * 同步路由权限 - * @param $appName + * @param string $appName * @return \think\Response * @author 等风来 * @email 136327134@qq.com * @date 2023/4/6 */ - public function syncRoute($appName) + public function syncRoute(string $appName = 'adminapi') { $this->services->syncRoute($appName); return app('json')->success(); } + /** + * 列表数据 + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ public function index() { + $where = $this->request->getMore([ + ['name_like', ''], + ['app_name', 'adminapi'] + ]); + return app('json')->success($this->services->getList($where)); } + /** + * tree数据 + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function tree() + { + $where = $this->request->getMore([ + ['name_like', ''], + ['app_name', 'adminapi'] + ]); + + return app('json')->success($this->services->getTreeList($where)); + } + + /** + * 创建 + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ public function create() { - + return app('json')->success($this->services->getFrom(0, $this->request->get('app_name', 'adminapi'))); } + /** + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ public function save() { + $data = $this->request->getMore([ + ['cate_id', 0], + ['name', ''], + ['path', ''], + ['method', ''], + ['type', 0], + ['app_name', ''], + ]); + if (!$data['name']) { + return app('json')->fail('请输入路由名称'); + } + if (!$data['path']) { + return app('json')->fail('请输入路由路径'); + } + if (!$data['method']) { + return app('json')->fail('请选择路由请求方式'); + } + if (!$data['app_name']) { + return app('json')->fail('缺少应用名参数'); + } + + $this->services->save($data); + + return app('json')->success('添加成功'); } - public function update() + /** + * @param $id + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function edit($id) { - + return app('json')->success($this->services->getFrom($id, $this->request->get('app_name', 'adminapi'))); } - public function delete() + /** + * @param $id + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function update($id) { + $data = $this->request->getMore([ + ['cate_id', 0], + ['name', ''], + ['path', ''], + ['method', ''], + ['type', 0], + ['app_name', ''], + ]); + if (!$data['name']) { + return app('json')->fail('请输入路由名称'); + } + if (!$data['path']) { + return app('json')->fail('请输入路由路径'); + } + if (!$data['method']) { + return app('json')->fail('请选择路由请求方式'); + } + if (!$data['app_name']) { + return app('json')->fail('缺少应用名参数'); + } + + $this->services->update($id, $data); + + return app('json')->success('修改成功'); + } + + /** + * @param $id + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function delete($id) + { + if (!$id) { + return app('json')->fail('缺少参数'); + } + + $this->services->delete($id); + + return app('json')->success('删除成功'); } diff --git a/crmeb/app/adminapi/route/system.php b/crmeb/app/adminapi/route/system.php index 2d0160d2a..cb1aa25a4 100644 --- a/crmeb/app/adminapi/route/system.php +++ b/crmeb/app/adminapi/route/system.php @@ -110,6 +110,32 @@ Route::group('system', function () { Route::delete('crontab/del/:id', 'v1.system.SystemCrontab/delTimer')->option(['real_name' => '删除定时任务']); //定时任务是否开启开关 Route::get('crontab/set_open/:id/:is_open', 'v1.system.SystemCrontab/setTimerStatus')->option(['real_name' => '定时任务是否开启开关']); + //同步路由接口 + Route::get('route/sync_route/[:appName]', 'v1.system.SystemRoute/syncRoute')->option(['real_name' => '同步路由']); + //获取路由tree行数据 + Route::get('route/tree', 'v1.system.SystemRoute/tree')->option(['real_name' => '获取路由tree']); + //权限路由 + Route::resource('route', 'v1.system.SystemRoute')->option([ + 'real_name' => [ + 'index' => '获取路由列表', + 'create' => '获取创建路由表单', + 'save' => '保存路由', + 'edit' => '获取修改路由表单', + 'update' => '修改路由', + 'delete' => '删除路由' + ], + ]); + //路由分类 + Route::resource('route_cate', 'v1.system.SystemRouteCate')->option([ + 'real_name' => [ + 'index' => '获取路由分类列表', + 'create' => '获取创建路由分类表单', + 'save' => '保存路由分类', + 'edit' => '获取修改路由分类表单', + 'update' => '修改路由分类', + 'delete' => '删除路由分类' + ], + ]); })->middleware([ \app\http\middleware\AllowOriginMiddleware::class, diff --git a/crmeb/app/dao/BaseDao.php b/crmeb/app/dao/BaseDao.php index ae7c1629e..8cfb30181 100644 --- a/crmeb/app/dao/BaseDao.php +++ b/crmeb/app/dao/BaseDao.php @@ -68,7 +68,7 @@ abstract class BaseDao * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function selectList(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = '', bool $search = false) + public function selectList(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = '', array $with = [], bool $search = false) { if ($search) { $model = $this->search($where); @@ -79,6 +79,8 @@ abstract class BaseDao $query->page($page, $limit); })->when($order !== '', function ($query) use ($order) { $query->order($order); + })->when($with, function ($query) use ($with) { + $query->with($with); })->select(); } diff --git a/crmeb/app/model/system/SystemRoute.php b/crmeb/app/model/system/SystemRoute.php index 1347e6632..9465b46cc 100644 --- a/crmeb/app/model/system/SystemRoute.php +++ b/crmeb/app/model/system/SystemRoute.php @@ -38,4 +38,11 @@ class SystemRoute extends BaseModel * @var string */ protected $key = 'id'; + + public function searchNameLikeAttr($query, $value) + { + if ('' !== $value) { + $query->where('name|path', 'LIKE', '%' . $value . '%'); + } + } } diff --git a/crmeb/app/model/system/SystemRouteCate.php b/crmeb/app/model/system/SystemRouteCate.php index 93a1f36a6..e0e4970a7 100644 --- a/crmeb/app/model/system/SystemRouteCate.php +++ b/crmeb/app/model/system/SystemRouteCate.php @@ -28,4 +28,15 @@ class SystemRouteCate extends BaseModel * @var string */ protected $pk = 'id'; + + /** + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + * @return \think\model\relation\HasMany + */ + public function children() + { + return $this->hasMany(SystemRoute::class, 'id', 'cate_id')->field(['id', 'cate_id', 'name', 'path', 'method'])->order('add_time desc'); + } } diff --git a/crmeb/app/services/system/SystemRouteCateServices.php b/crmeb/app/services/system/SystemRouteCateServices.php index 7c9c1e771..f062bd415 100644 --- a/crmeb/app/services/system/SystemRouteCateServices.php +++ b/crmeb/app/services/system/SystemRouteCateServices.php @@ -73,9 +73,9 @@ class SystemRouteCateServices extends BaseServices * @email 136327134@qq.com * @date 2023/4/6 */ - public function getAllList(string $appName = 'outapi') + public function getAllList(string $appName = 'outapi', string $field = '*') { - $list = $this->dao->selectList(['app_name' => $appName])->toArray(); + $list = $this->dao->selectList(['app_name' => $appName], $field)->toArray(); return get_tree_children($list); } diff --git a/crmeb/app/services/system/SystemRouteServices.php b/crmeb/app/services/system/SystemRouteServices.php index 76be595ea..41a82d8cd 100644 --- a/crmeb/app/services/system/SystemRouteServices.php +++ b/crmeb/app/services/system/SystemRouteServices.php @@ -16,8 +16,7 @@ namespace app\services\system; use app\dao\system\SystemRouteDao; use app\services\BaseServices; -use Darabonba\GatewaySpi\Models\InterceptorContext\response; -use think\facade\Route; +use crmeb\services\FormBuilder; use think\helper\Str; /** @@ -39,6 +38,44 @@ class SystemRouteServices extends BaseServices $this->dao = $dao; } + /** + * @param array $where + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function getList(array $where) + { + [$page, $limit] = $this->getPageValue(); + $list = $this->dao->selectList($where, 'name,path,method', $page, $limit)->toArray(); + $count = $this->dao->count($where); + return compact('list', 'count'); + } + + /** + * 获取tree数据 + * @param string $appName + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function getTreeList(array $where, string $appName = 'adminapi') + { + $list = app()->make(SystemRouteCateServices::class) + ->selectList(['app_name' => $appName], '*', 0, 0, 'add_time desc', [ + 'children' => function ($query) use ($where) { + $query->where('app_name', $where['app_name']) + ->when('' !== $where['name_like'], function ($q) use ($where) { + $q->where('name|path', 'LIKE', '%' . $where['name_like'] . '%'); + }); + } + ]) + ->toArray(); + return get_tree_children($list); + } + /** * 获取某个应用下的所有路由权限 * @param string $app @@ -60,7 +97,23 @@ class SystemRouteServices extends BaseServices include $path . $file; } } - return $this->app->route->getRuleList(); + + $route = $this->app->route->getRuleList(); + $action_arr = ['index', 'read', 'create', 'save', 'edit', 'update', 'delete']; + + foreach ($route as &$item) { + $real_name = $item['option']['real_name'] ?? ''; + if (is_array($real_name)) { + foreach ($action_arr as $action) { + if (Str::contains($item['route'], $action)) { + $real_name = $real_name[$action] ?? ''; + } + } + } + $item['option']['real_name'] = $real_name; + } + + return $route; } /** @@ -128,4 +181,45 @@ class SystemRouteServices extends BaseServices } return $res; } + + /** + * 添加和修改路由 + * @param int $id + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/7 + */ + public function getFrom(int $id = 0, string $appName = 'adminapi') + { + $cateList = app()->make(SystemRouteCateServices::class)->getAllList($appName, 'name as label,path as value'); + + $url = '/system/route'; + $routeInfo = []; + if ($id) { + $routeInfo = $this->dao->get($id); + $routeInfo = $routeInfo ? $routeInfo->toArray() : []; + $url .= '/' . $id; + } + + $rule = [ + FormBuilder::cascader('cate_id', '分类', $routeInfo['cate_id'] ?? 0)->data($cateList), + FormBuilder::input('name', '路由名称', $routeInfo['name'] ?? '')->required(), + FormBuilder::input('path', '路由路径', $routeInfo['path'] ?? '')->required(), + FormBuilder::select('method', '请求方式', $routeInfo['method'] ?? '')->options([ + ['value' => 'POST', 'label' => 'POST'], + ['value' => 'GET', 'label' => 'GET'], + ['value' => 'DELETE', 'label' => 'DELETE'], + ['value' => 'PUT', 'label' => 'PUT'], + ['value' => '*', 'label' => '*'], + ])->required(), + FormBuilder::radio('type', '类型', $routeInfo['type'] ?? 0)->options([ + ['value' => 0, 'lable' => '普通路由'], + ['value' => 1, 'lable' => '公共路由'], + ]), + FormBuilder::hidden('app_name', $appName), + ]; + + return create_form($id ? '修改路由' : '添加路由', $rule, $url, $id ? 'PUT' : 'POST'); + } }