From de14acb0074cca292c916c3a1f70b313f6734052 Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Thu, 6 Apr 2023 18:49:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0crud=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8=E4=BB=A3=E7=A0=81=E5=92=8C=E8=B7=AF=E7=94=B1=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=8E=A5=E5=8F=A3=E8=A1=A8=E5=88=86=E7=B1=BB=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/setting/SystemCrud.php | 46 +++++ .../controller/v1/setting/SystemMenus.php | 12 +- .../controller/v1/setting/SystemRoute.php | 82 +++++++++ .../controller/v1/setting/SystemRouteCate.php | 163 ++++++++++++++++++ crmeb/app/adminapi/route/cms.php | 2 +- crmeb/app/adminapi/route/common.php | 10 +- crmeb/app/adminapi/route/file.php | 18 +- crmeb/app/adminapi/route/product.php | 7 +- crmeb/app/adminapi/route/route.php | 5 +- crmeb/app/dao/BaseDao.php | 21 ++- crmeb/app/dao/system/SystemCrudDao.php | 38 ++++ crmeb/app/dao/system/SystemRouteCateDao.php | 40 +++++ crmeb/app/dao/system/SystemRouteDao.php | 40 +++++ crmeb/app/model/system/SystemCrud.php | 38 ++++ crmeb/app/model/system/SystemRoute.php | 41 +++++ crmeb/app/model/system/SystemRouteCate.php | 31 ++++ .../services/system/SystemCrudServices.php | 38 ++++ .../system/SystemRouteCateServices.php | 109 ++++++++++++ .../services/system/SystemRouteServices.php | 131 ++++++++++++++ crmeb/crmeb/services/crud/Make.php | 15 +- crmeb/crmeb/services/crud/ViewApi.php | 20 ++- crmeb/crmeb/services/crud/ViewPages.php | 21 ++- crmeb/crmeb/services/crud/ViewRouter.php | 20 ++- 23 files changed, 911 insertions(+), 37 deletions(-) create mode 100644 crmeb/app/adminapi/controller/v1/setting/SystemCrud.php create mode 100644 crmeb/app/adminapi/controller/v1/setting/SystemRoute.php create mode 100644 crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php create mode 100644 crmeb/app/dao/system/SystemCrudDao.php create mode 100644 crmeb/app/dao/system/SystemRouteCateDao.php create mode 100644 crmeb/app/dao/system/SystemRouteDao.php create mode 100644 crmeb/app/model/system/SystemCrud.php create mode 100644 crmeb/app/model/system/SystemRoute.php create mode 100644 crmeb/app/model/system/SystemRouteCate.php create mode 100644 crmeb/app/services/system/SystemCrudServices.php create mode 100644 crmeb/app/services/system/SystemRouteCateServices.php create mode 100644 crmeb/app/services/system/SystemRouteServices.php diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php new file mode 100644 index 000000000..5f32e9852 --- /dev/null +++ b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php @@ -0,0 +1,46 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\adminapi\controller\v1\setting; + + +use app\adminapi\controller\AuthController; +use app\services\system\SystemCrudServices; +use think\facade\App; + +/** + * Class SystemCrud + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\adminapi\controller\v1\setting + */ +class SystemCrud extends AuthController +{ + + /** + * SystemCrud constructor. + * @param App $app + * @param SystemCrudServices $services + */ + public function __construct(App $app, SystemCrudServices $services) + { + parent::__construct($app); + $this->services = $services; + } + + public function index() + { + + } +} diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php b/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php index d54f65533..234bd459b 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php @@ -222,7 +222,17 @@ class SystemMenus extends AuthController public function ruleList() { //获取所有的路由 - $ruleList = Route::getRuleList(); + $this->app = app(); + $this->app->route->setTestMode(true); + $this->app->route->clear(); + $path = $this->app->getRootPath() . 'app' . DS . 'adminapi' . DS . 'route' . DS; + $files = is_dir($path) ? scandir($path) : []; + foreach ($files as $file) { + if (strpos($file, '.php')) { + include $path . $file; + } + } + $ruleList = $this->app->route->getRuleList(); $menuApiList = $this->services->getColumn(['auth_type' => 2, 'is_del' => 0], "concat(`api_url`,'_',lower(`methods`)) as rule"); if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule'); $list = []; diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php b/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php new file mode 100644 index 000000000..47cd222d0 --- /dev/null +++ b/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php @@ -0,0 +1,82 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\adminapi\controller\v1\setting; + + +use app\adminapi\controller\AuthController; +use app\services\system\SystemRouteServices; +use think\facade\App; + +/** + * Class SystemRoute + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\adminapi\controller\v1\setting + */ +class SystemRoute extends AuthController +{ + + /** + * SystemRoute constructor. + * @param App $app + * @param SystemRouteServices $services + */ + public function __construct(App $app, SystemRouteServices $services) + { + parent::__construct($app); + $this->services = $services; + } + + /** + * 同步路由权限 + * @param $appName + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function syncRoute($appName) + { + $this->services->syncRoute($appName); + return app('json')->success(); + } + + public function index() + { + + } + + public function create() + { + + } + + public function save() + { + + } + + public function update() + { + + } + + public function delete() + { + + } + + +} diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php b/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php new file mode 100644 index 000000000..6d6356059 --- /dev/null +++ b/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php @@ -0,0 +1,163 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\adminapi\controller\v1\setting; + + +use app\adminapi\controller\AuthController; +use app\services\system\SystemRouteCateServices; +use app\services\system\SystemRouteServices; +use think\facade\App; +use think\Request; + +/** + * Class SystemRouteCate + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\adminapi\controller\v1\setting + */ +class SystemRouteCate extends AuthController +{ + + /** + * SystemRouteCate constructor. + * @param App $app + * @param SystemRouteCateServices $services + */ + public function __construct(App $app, SystemRouteCateServices $services) + { + parent::__construct($app); + $this->services = $services; + } + + /** + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function index() + { + return app('json')->success($this->services->getAllList()); + } + + /** + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function create() + { + return app('json')->success($this->services->getFrom()); + } + + /** + * @param Request $request + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function save(Request $request) + { + $data = $request->postMore([ + ['pid', 0], + ['name', ''], + ['sort', 0], + ['app_name', ''], + ]); + + if (!$data['name']) { + return app('json')->fail('缺少分类名称'); + } + + $res = $this->services->save($data); + + $path = $this->services->getPathValue($data['pid']); + $path = $this->services->setPathValue($path, $res->id); + $res->path = $path; + $res->save(); + + return app('json')->success('保存成功'); + + } + + /** + * @param $id + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function edit($id) + { + return app('json')->success($this->services->getFrom($id)); + } + + /** + * @param Request $request + * @param $id + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function update(Request $request, $id) + { + $data = $request->postMore([ + ['pid', 0], + ['name', ''], + ['sort', 0], + ['app_name', ''], + ]); + + if (!$data['name']) { + return app('json')->fail('缺少分类名称'); + } + + $pid = $this->services->value($id, 'pid'); + if ($data['pid'] != $pid) { + $path = $this->services->getPathValue($data['pid']); + $data['path'] = $this->services->setPathValue($path, $id); + } + + $this->services->update($data); + + return app('json')->success('修改成功'); + } + + /** + * @param SystemRouteServices $service + * @param $id + * @return \think\Response + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function delete(SystemRouteServices $service, $id) + { + if (!$id) { + return app('json')->fail('缺少参数'); + } + + if ($service->count(['cate_id' => $id])) { + return app('json')->fail('请先删除分类下的接口'); + } + + $this->services->delete($id); + + return app('json')->success('删除成功'); + } +} diff --git a/crmeb/app/adminapi/route/cms.php b/crmeb/app/adminapi/route/cms.php index 1912b0017..4495ad388 100644 --- a/crmeb/app/adminapi/route/cms.php +++ b/crmeb/app/adminapi/route/cms.php @@ -25,7 +25,7 @@ Route::group('cms', function () { //修改状态 Route::put('category/set_status/:id/:status', 'v1.cms.ArticleCategory/set_status')->name('CategoryStatus')->option(['real_name' => '修改文章分类状态']); //分类列表 - Route::get('category_list', 'v1.cms.ArticleCategory/categoryList')->name('categoryList')->option(['real_name' => '分类列表']); + Route::get('category_list', 'v1.cms.ArticleCategory/categoryList')->name('categoryList')->option(['real_name' => '分类列表', 'is_common' => true]); //分类树形列表 Route::get('category_tree_list', 'v1.cms.ArticleCategory/getTreeList')->name('getTreeList')->option(['real_name' => '分类树形列表']); })->middleware([ diff --git a/crmeb/app/adminapi/route/common.php b/crmeb/app/adminapi/route/common.php index 23401f94a..67ca6b220 100644 --- a/crmeb/app/adminapi/route/common.php +++ b/crmeb/app/adminapi/route/common.php @@ -26,19 +26,19 @@ Route::group(function () { // Route::get('home/rank', 'Common/purchaseRanking')->option(['real_name' => '首页交易额排行']); // 消息提醒 - Route::get('jnotice', 'Common/jnotice')->option(['real_name' => '消息提醒']); + Route::get('jnotice', 'Common/jnotice')->option(['real_name' => '消息提醒', 'is_common' => true]); //验证授权 Route::get('check_auth', 'Common/auth')->option(['real_name' => '验证授权']); //申请授权 Route::post('auth_apply', 'Common/auth_apply')->option(['real_name' => '申请授权']); //授权 - Route::get('auth', 'Common/auth')->option(['real_name' => '授权信息']); + Route::get('auth', 'Common/auth')->option(['real_name' => '授权信息', 'is_common' => true]); //获取左侧菜单 - Route::get('menus', 'v1.setting.SystemMenus/menus')->option(['real_name' => '左侧菜单']); + Route::get('menus', 'v1.setting.SystemMenus/menus')->option(['real_name' => '左侧菜单', 'is_common' => true]); //获取搜索菜单列表 - Route::get('menusList', 'Common/menusList')->option(['real_name' => '搜索菜单列表']); + Route::get('menusList', 'Common/menusList')->option(['real_name' => '搜索菜单列表', 'is_common' => true]); //获取logo - Route::get('logo', 'Common/getLogo')->option(['real_name' => '获取logo']); + Route::get('logo', 'Common/getLogo')->option(['real_name' => '获取logo', 'is_common' => true]); //查询版权 Route::get('copyright', 'Common/copyright')->option(['real_name' => '申请版权']); //保存版权 diff --git a/crmeb/app/adminapi/route/file.php b/crmeb/app/adminapi/route/file.php index 4cc858907..5779549bc 100644 --- a/crmeb/app/adminapi/route/file.php +++ b/crmeb/app/adminapi/route/file.php @@ -15,23 +15,23 @@ use think\facade\Route; */ Route::group('file', function () { //附件列表 - Route::get('file', 'v1.file.SystemAttachment/index')->option(['real_name' => '图片附件列表']); + Route::get('file', 'v1.file.SystemAttachment/index')->option(['real_name' => '图片附件列表', 'is_common' => true]); //删除图片和数据记录 - Route::post('file/delete', 'v1.file.SystemAttachment/delete')->option(['real_name' => '删除图片']); + Route::post('file/delete', 'v1.file.SystemAttachment/delete')->option(['real_name' => '删除图片', 'is_common' => true]); //移动图片分来表单 - Route::get('file/move', 'v1.file.SystemAttachment/move')->option(['real_name' => '移动图片分类表单']); + Route::get('file/move', 'v1.file.SystemAttachment/move')->option(['real_name' => '移动图片分类表单', 'is_common' => true]); //移动图片分类 - Route::put('file/do_move', 'v1.file.SystemAttachment/moveImageCate')->option(['real_name' => '移动图片分类']); + Route::put('file/do_move', 'v1.file.SystemAttachment/moveImageCate')->option(['real_name' => '移动图片分类', 'is_common' => true]); //修改图片名称 - Route::put('file/update/:id', 'v1.file.SystemAttachment/update')->option(['real_name' => '修改图片名称']); + Route::put('file/update/:id', 'v1.file.SystemAttachment/update')->option(['real_name' => '修改图片名称', 'is_common' => true]); //上传图片 - Route::post('upload/[:upload_type]', 'v1.file.SystemAttachment/upload')->option(['real_name' => '上传图片']); + Route::post('upload/[:upload_type]', 'v1.file.SystemAttachment/upload')->option(['real_name' => '上传图片', 'is_common' => true]); //附件分类管理资源路由 - Route::resource('category', 'v1.file.SystemAttachmentCategory')->option(['real_name' => '附件分类管理']); + Route::resource('category', 'v1.file.SystemAttachmentCategory')->option(['real_name' => '附件分类管理', 'is_common' => true]); //获取上传类型 - Route::get('upload_type', 'v1.file.SystemAttachment/uploadType')->option(['real_name' => '上传类型']); + Route::get('upload_type', 'v1.file.SystemAttachment/uploadType')->option(['real_name' => '上传类型', 'is_common' => true]); //分片上传本地视频 - Route::post('video_upload', 'v1.file.SystemAttachment/videoUpload')->option(['real_name' => '分片上传本地视频']); + Route::post('video_upload', 'v1.file.SystemAttachment/videoUpload')->option(['real_name' => '分片上传本地视频', 'is_common' => true]); })->middleware([ \app\http\middleware\AllowOriginMiddleware::class, \app\adminapi\middleware\AdminAuthTokenMiddleware::class, diff --git a/crmeb/app/adminapi/route/product.php b/crmeb/app/adminapi/route/product.php index d73d2a0b9..59f21afda 100644 --- a/crmeb/app/adminapi/route/product.php +++ b/crmeb/app/adminapi/route/product.php @@ -14,8 +14,9 @@ Route::group('product', function () { Route::get('category', 'v1.product.StoreCategory/index')->option(['real_name' => '商品分类列表']); //商品树形列表 - Route::get('category/tree/:type', 'v1.product.StoreCategory/tree_list')->option(['real_name' => '商品分类树形列表']); - Route::get('category/cascader/:type', 'v1.product.StoreCategory/cascader_list')->option(['real_name' => '商品分类树形列表']); + Route::get('category/tree/:type', 'v1.product.StoreCategory/tree_list')->option(['real_name' => '商品分类树形列表', 'is_common' => true]); + //商品分类树形列表 + Route::get('category/cascader/:type', 'v1.product.StoreCategory/cascader_list')->option(['real_name' => '商品分类树形列表', 'is_common' => true]); //商品分类新增表单 Route::get('category/create', 'v1.product.StoreCategory/create')->option(['real_name' => '商品分类新增表单']); //商品分类新增 @@ -37,7 +38,7 @@ Route::group('product', function () { //1分钟保存一次数据 Route::post('cache', 'v1.product.StoreProduct/saveCacheData')->option(['real_name' => '保存还未提交数据']); //获取所有商品列表 - Route::get('product/list', 'v1.product.StoreProduct/search_list')->option(['real_name' => '获取所有商品列表']); + Route::get('product/list', 'v1.product.StoreProduct/search_list')->option(['real_name' => '获取所有商品列表', 'is_common' => true]); //获取商品规格 Route::get('product/attrs/:id/:type', 'v1.product.StoreProduct/get_attrs')->option(['real_name' => '获取商品规格']); //商品列表头 diff --git a/crmeb/app/adminapi/route/route.php b/crmeb/app/adminapi/route/route.php index 46d82dce8..cfda8dd4c 100644 --- a/crmeb/app/adminapi/route/route.php +++ b/crmeb/app/adminapi/route/route.php @@ -33,7 +33,10 @@ Route::group(function () { //一次验证 Route::post('ajcheck', 'Login/ajcheck')->name('ajcheck')->option(['real_name' => '一次验证']); //获取客服数据 - Route::get('get_workerman_url', 'PublicController/getWorkerManUrl')->option(['real_name' => '获取客服数据']); + Route::get('get_workerman_url', 'PublicController/getWorkerManUrl')->option([ + 'real_name' => '获取客服数据', + 'is_common' => true, + ]); //测试 Route::get('index', 'Test/index')->option(['real_name' => '测试地址']); })->middleware(AllowOriginMiddleware::class); diff --git a/crmeb/app/dao/BaseDao.php b/crmeb/app/dao/BaseDao.php index 9ad15edbf..ae7c1629e 100644 --- a/crmeb/app/dao/BaseDao.php +++ b/crmeb/app/dao/BaseDao.php @@ -193,14 +193,14 @@ abstract class BaseDao /** * 获取单个字段值 - * @param array $where + * @param $where * @param string|null $field * @return mixed */ - public function value(array $where, ?string $field = '') + public function value($where, ?string $field = '') { $pk = $this->getPk(); - return $this->getModel()->where($where)->value($field ?: $pk); + return $this->search($this->setWhere($where))->value($field ?: $pk); } /** @@ -247,6 +247,21 @@ abstract class BaseDao return $this->getModel()::update($data, $where); } + /** + * @param $where + * @return array|mixed + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + protected function setWhere($where, ?string $key = null) + { + if (!is_array($where)) { + $where = [is_null($key) ? $this->getPk() : $key => $where]; + } + return $where; + } + /** * 批量更新数据 * @param array $ids diff --git a/crmeb/app/dao/system/SystemCrudDao.php b/crmeb/app/dao/system/SystemCrudDao.php new file mode 100644 index 000000000..ffa0282b3 --- /dev/null +++ b/crmeb/app/dao/system/SystemCrudDao.php @@ -0,0 +1,38 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\dao\system; + + +use app\dao\BaseDao; +use app\model\system\SystemCrud; + +/** + * Class SystemCrudDao + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\dao\system + */ +class SystemCrudDao extends BaseDao +{ + + /** + * 获取当前模型 + * @return string + */ + protected function setModel(): string + { + return SystemCrud::class; + } +} diff --git a/crmeb/app/dao/system/SystemRouteCateDao.php b/crmeb/app/dao/system/SystemRouteCateDao.php new file mode 100644 index 000000000..dbaf646e0 --- /dev/null +++ b/crmeb/app/dao/system/SystemRouteCateDao.php @@ -0,0 +1,40 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\dao\system; + + +use app\dao\BaseDao; +use app\model\system\SystemRouteCate; + +/** + * Class SystemRouteCateDao + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\dao\system + */ +class SystemRouteCateDao extends BaseDao +{ + + /** + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + protected function setModel(): string + { + return SystemRouteCate::class; + } +} diff --git a/crmeb/app/dao/system/SystemRouteDao.php b/crmeb/app/dao/system/SystemRouteDao.php new file mode 100644 index 000000000..f222df701 --- /dev/null +++ b/crmeb/app/dao/system/SystemRouteDao.php @@ -0,0 +1,40 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\dao\system; + + +use app\dao\BaseDao; +use app\model\system\SystemRoute; + +/** + * Class SystemRouteDao + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\dao\system + */ +class SystemRouteDao extends BaseDao +{ + + /** + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + protected function setModel(): string + { + return SystemRoute::class; + } +} diff --git a/crmeb/app/model/system/SystemCrud.php b/crmeb/app/model/system/SystemCrud.php new file mode 100644 index 000000000..8f386053a --- /dev/null +++ b/crmeb/app/model/system/SystemCrud.php @@ -0,0 +1,38 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\model\system; + + +use crmeb\basic\BaseModel; + +/** + * Class SystemCrud + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\model\system + */ +class SystemCrud extends BaseModel +{ + + /** + * @var string + */ + protected $name = 'system_crud'; + + /** + * @var string + */ + protected $pk = 'id'; +} diff --git a/crmeb/app/model/system/SystemRoute.php b/crmeb/app/model/system/SystemRoute.php new file mode 100644 index 000000000..1347e6632 --- /dev/null +++ b/crmeb/app/model/system/SystemRoute.php @@ -0,0 +1,41 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\model\system; + + +use crmeb\basic\BaseModel; +use think\model\concern\SoftDelete; + +/** + * Class SystemRoute + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\model\system + */ +class SystemRoute extends BaseModel +{ + + use SoftDelete; + + /** + * @var string + */ + protected $name = 'system_route'; + + /** + * @var string + */ + protected $key = 'id'; +} diff --git a/crmeb/app/model/system/SystemRouteCate.php b/crmeb/app/model/system/SystemRouteCate.php new file mode 100644 index 000000000..93a1f36a6 --- /dev/null +++ b/crmeb/app/model/system/SystemRouteCate.php @@ -0,0 +1,31 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\model\system; + + +use crmeb\basic\BaseModel; + +class SystemRouteCate extends BaseModel +{ + + /** + * @var string + */ + protected $name = 'system_route_cate'; + + /** + * @var string + */ + protected $pk = 'id'; +} diff --git a/crmeb/app/services/system/SystemCrudServices.php b/crmeb/app/services/system/SystemCrudServices.php new file mode 100644 index 000000000..59b81c089 --- /dev/null +++ b/crmeb/app/services/system/SystemCrudServices.php @@ -0,0 +1,38 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\services\system; + + +use app\dao\system\SystemCrudDao; +use app\services\BaseServices; + +/** + * Class SystemCrudServices + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\services\system + */ +class SystemCrudServices extends BaseServices +{ + + /** + * SystemCrudServices constructor. + * @param SystemCrudDao $dao + */ + public function __construct(SystemCrudDao $dao) + { + $this->dao = $dao; + } +} diff --git a/crmeb/app/services/system/SystemRouteCateServices.php b/crmeb/app/services/system/SystemRouteCateServices.php new file mode 100644 index 000000000..7c9c1e771 --- /dev/null +++ b/crmeb/app/services/system/SystemRouteCateServices.php @@ -0,0 +1,109 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\services\system; + + +use app\dao\system\SystemRouteCateDao; +use app\services\BaseServices; +use crmeb\services\FormBuilder; + +/** + * Class SystemRouteCateServices + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\services\system + */ +class SystemRouteCateServices extends BaseServices +{ + + /** + * SystemRouteCateServices constructor. + * @param SystemRouteCateDao $dao + */ + public function __construct(SystemRouteCateDao $dao) + { + $this->dao = $dao; + } + + public function getPathValue(int $pid) + { + if (!$pid) { + return []; + } + $path = $this->dao->value($pid, 'path'); + $pathAttr = explode('/', $path); + $pathData = []; + foreach ($pathAttr as $item) { + if (!$item) { + $pathData[] = $item; + } + } + return $pathAttr; + } + + /** + * @param array $path + * @param int $id + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function setPathValue(array $path, int $id) + { + return '/' . implode('/', $path) . '/' . $id . '/'; + } + + /** + * @param string $appName + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function getAllList(string $appName = 'outapi') + { + $list = $this->dao->selectList(['app_name' => $appName])->toArray(); + return get_tree_children($list); + } + + /** + * @param int $id + * @param string $appName + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function getFrom(int $id = 0, string $appName = 'outapi') + { + $url = '/system/route_cate'; + $cateInfo = []; + if ($id) { + $cateInfo = $this->dao->get($id); + $cateInfo = $cateInfo ? $cateInfo->toArray() : []; + $url .= '/' . $id; + } + $options = $this->dao->selectList(['app_name' => $appName])->toArray(); + $rule = [ + FormBuilder::select('pid', '上级分类', (int)($cateInfo['pid'] ?? 0))->options($options), + FormBuilder::input('name', '分类名称', $cateInfo['name'] ?? '')->required(), + FormBuilder::number('sort', '排序', (int)($cateInfo['sort'] ?? 0)), + FormBuilder::hidden('app_name', $appName) + ]; + + return create_form($id ? '修改分类' : '添加分类', $rule, $url, $id ? 'PUT' : 'POST'); + } +} diff --git a/crmeb/app/services/system/SystemRouteServices.php b/crmeb/app/services/system/SystemRouteServices.php new file mode 100644 index 000000000..76be595ea --- /dev/null +++ b/crmeb/app/services/system/SystemRouteServices.php @@ -0,0 +1,131 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\services\system; + + +use app\dao\system\SystemRouteDao; +use app\services\BaseServices; +use Darabonba\GatewaySpi\Models\InterceptorContext\response; +use think\facade\Route; +use think\helper\Str; + +/** + * Class SystemRouteServices + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + * @package app\services\system + */ +class SystemRouteServices extends BaseServices +{ + + /** + * SystemRouteServices constructor. + * @param SystemRouteDao $dao + */ + public function __construct(SystemRouteDao $dao) + { + $this->dao = $dao; + } + + /** + * 获取某个应用下的所有路由权限 + * @param string $app + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function getRouteListAll(string $app = 'adminapi') + { + //获取所有的路由 + $this->app = app(); + $this->app->route->setTestMode(true); + $this->app->route->clear(); + $path = $this->app->getRootPath() . 'app' . DS . $app . DS . 'route' . DS; + $files = is_dir($path) ? scandir($path) : []; + foreach ($files as $file) { + if (strpos($file, '.php')) { + include $path . $file; + } + } + return $this->app->route->getRuleList(); + } + + /** + * 同步路由 + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + public function syncRoute(string $app = 'adminapi') + { + $listAll = $this->getRouteListAll($app); + //保持新增的权限路由 + $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray(); + $save = []; + foreach ($listAll as $item) { + if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '') === false) { + $save[] = [ + 'name' => $item['option']['real_name'] ?? $item['name'], + 'path' => $item['rule'], + 'app_name' => $app, + 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0, + 'method' => $item['method'], + 'add_time' => date('Y-m-d H:i:s'), + ]; + } + } + + if ($save) { + $this->dao->saveAll($save); + } + //删除不存在的权限路由 + $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray(); + $delete = []; + foreach ($data as $item) { + if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '') { + $delete[] = $item['id']; + } + } + if ($delete) { + $this->dao->delete([['id', 'in', $delete]]); + } + } + + /** + * 对比路由 + * @param array $data + * @param string $path + * @param string $method + * @return bool + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/6 + */ + protected function diffRoute(array $data, string $path, string $method, string $key = 'path') + { + $res = false; + foreach ($data as $item) { + if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) { + $res = true; + break; + } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) { + $res = true; + break; + } + } + return $res; + } +} diff --git a/crmeb/crmeb/services/crud/Make.php b/crmeb/crmeb/services/crud/Make.php index 3a35719ca..e1857d8ca 100644 --- a/crmeb/crmeb/services/crud/Make.php +++ b/crmeb/crmeb/services/crud/Make.php @@ -119,10 +119,19 @@ abstract class Make { //替换成本地路径格式 $path = str_replace('/', DS, $path); + $pathAttr = explode(DS, $path); + $basePathAttr = explode(DS, $this->baseDir); //替换掉和基础目录相同的 - $path = str_replace($this->baseDir, '', $path); + if (count($pathAttr) > 1) { + $newsPath = array_merge(array_diff($basePathAttr, $pathAttr))[0] ?? ''; + if ($newsPath !== 'crud') { + $path = $newsPath; + } else { + $this->baseDir = ''; + } + } //多个斜杠的替换成一个 - return str_replace(DS . DS, DS, $this->basePath . $this->baseDir . DS . ($path ? $path . DS : '')); + return str_replace(DS . DS, DS, $this->basePath . ($this->baseDir ? $this->baseDir . DS : '') . ($path ? $path . DS : '')); } /** @@ -269,8 +278,6 @@ abstract class Make */ protected function getFilePathName(string $path, string $name): string { - $path = str_replace(['app\\', 'app/'], '', $path); - $path = ltrim(str_replace('\\', '/', $path), '/'); return $this->getBasePath($path) . $name . ucwords($this->name) . '.' . $this->fileMime; diff --git a/crmeb/crmeb/services/crud/ViewApi.php b/crmeb/crmeb/services/crud/ViewApi.php index d1a94b889..7ee1c914d 100644 --- a/crmeb/crmeb/services/crud/ViewApi.php +++ b/crmeb/crmeb/services/crud/ViewApi.php @@ -14,6 +14,7 @@ namespace crmeb\services\crud; use crmeb\exceptions\CrudException; +use think\App; use think\helper\Str; /** @@ -31,6 +32,21 @@ class ViewApi extends Make */ protected $name = 'api'; + /** + * @var string + */ + protected $fileMime = 'js'; + + /** + * ViewApi constructor. + * @param App $app + */ + public function __construct(App $app) + { + parent::__construct($app); + $this->basePath = $this->adminTemplatePath; + } + /** * @return string * @author 等风来 @@ -83,15 +99,11 @@ class ViewApi extends Make [$className, $content] = $this->getStubContent($name, $this->name); $contentStr = str_replace($this->var, $this->value, $content); - - $this->basePath = $this->adminTemplatePath; - $this->fileMime = 'js'; $filePath = $this->getFilePathName($path, Str::camel($name)); $content = $this->makeFile($filePath, $contentStr); return [$content, $filePath]; - } /** diff --git a/crmeb/crmeb/services/crud/ViewPages.php b/crmeb/crmeb/services/crud/ViewPages.php index 42925af08..8869e7148 100644 --- a/crmeb/crmeb/services/crud/ViewPages.php +++ b/crmeb/crmeb/services/crud/ViewPages.php @@ -13,6 +13,7 @@ namespace crmeb\services\crud; +use think\App; use think\helper\Str; /** @@ -30,6 +31,21 @@ class ViewPages extends Make */ protected $name = 'pages'; + /** + * @var string + */ + protected $fileMime = 'vue'; + + /** + * ViewPages constructor. + * @param App $app + */ + public function __construct(App $app) + { + parent::__construct($app); + $this->basePath = $this->adminTemplatePath; + } + /** * @return string * @author 等风来 @@ -62,9 +78,8 @@ class ViewPages extends Make $this->value['content-vue'] = "\n" . implode(',', $columnStr); $this->value['pathApiJs'] = $options['pathApiJs'] ?? ''; $this->value['nameCamel'] = Str::snake($name, '-'); - $this->basePath = $this->adminTemplatePath . 'pages'; - $this->fileMime = 'vue'; - return parent::handle($name, $path, $options); // TODO: Change the autogenerated stub + + return parent::handle($name, $path, $options); } /** diff --git a/crmeb/crmeb/services/crud/ViewRouter.php b/crmeb/crmeb/services/crud/ViewRouter.php index fb125a3ad..48c9e5d09 100644 --- a/crmeb/crmeb/services/crud/ViewRouter.php +++ b/crmeb/crmeb/services/crud/ViewRouter.php @@ -14,6 +14,7 @@ namespace crmeb\services\crud; use crmeb\exceptions\CrudException; +use think\App; use think\helper\Str; /** @@ -30,6 +31,21 @@ class ViewRouter extends Make */ protected $name = 'router'; + /** + * @var string + */ + protected $fileMime = 'js'; + + /** + * ViewRouter constructor. + * @param App $app + */ + public function __construct(App $app) + { + parent::__construct($app); + $this->basePath = $this->adminTemplatePath; + } + /** * @return string * @author 等风来 @@ -52,9 +68,6 @@ class ViewRouter extends Make */ public function handle(string $name, string $path, array $options = []) { - $this->basePath = $this->adminTemplatePath . 'router' . DS . 'modules' . DS . 'crud'; - $this->fileMime = 'js'; - [$nameData, $content] = $this->getStubContent($name); $menus = $options['menus'] ?? $name; @@ -63,6 +76,7 @@ class ViewRouter extends Make if (!$route) { throw new CrudException('缺少路由名称'); } + $this->value['menus'] = $menus; $this->value['name'] = $nameData; $this->value['route'] = $route;