增加crud控制器代码和路由权限接口表分类逻辑

This commit is contained in:
liaofei 2023-04-06 18:49:30 +08:00
parent a45d46f729
commit de14acb007
23 changed files with 911 additions and 37 deletions

View File

@ -0,0 +1,46 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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()
{
}
}

View File

@ -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 = [];

View File

@ -0,0 +1,82 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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()
{
}
}

View File

@ -0,0 +1,163 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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('删除成功');
}
}

View File

@ -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([

View File

@ -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' => '申请版权']);
//保存版权

View File

@ -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,

View File

@ -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' => '获取商品规格']);
//商品列表头

View File

@ -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);

View File

@ -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

View File

@ -0,0 +1,38 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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;
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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;
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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;
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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';
}

View File

@ -0,0 +1,41 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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';
}

View File

@ -0,0 +1,31 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
namespace app\model\system;
use crmeb\basic\BaseModel;
class SystemRouteCate extends BaseModel
{
/**
* @var string
*/
protected $name = 'system_route_cate';
/**
* @var string
*/
protected $pk = 'id';
}

View File

@ -0,0 +1,38 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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;
}
}

View File

@ -0,0 +1,109 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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');
}
}

View File

@ -0,0 +1,131 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
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'], '<MISS>') === 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'] !== '<MISS>') {
$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;
}
}

View File

@ -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;

View File

@ -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];
}
/**

View File

@ -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);
}
/**

View File

@ -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;