引入格式化工具配置;节点管理新增挂载到控制器注解中的节点;

This commit is contained in:
2023-08-29 17:51:40 +08:00
parent 55cd948e46
commit d7f2476cbe
6 changed files with 257 additions and 94 deletions

View File

@@ -1,32 +1,30 @@
<?php
namespace app\admin\controller\system;
use app\admin\model\SystemAdmin;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use app\admin\service\TriggerService;
use app\common\constants\AdminConstant;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;
use think\facade\Validate;
use think\validate\ValidateRule;
/**
* Class Admin
* @package app\admin\controller\system
* Class Admin.
* @ControllerAnnotation(title="管理员管理")
*
* @NodeAnotation(title="自定义权限标识符",name="customFlag")
*/
class Admin extends AdminController
{
use \app\admin\traits\Curd;
protected $sort = [
'sort' => 'desc',
'id' => 'desc',
'id' => 'desc',
];
public function __construct(App $app)
@@ -37,6 +35,8 @@ class Admin extends AdminController
$this->setDataBrage('count', 10);
$this->setDataBrage('tips', '请谨慎操作');
$this->setDataBrage('adminCustomFlag', $this->checkAuth('system.admin/customFlag', false));
}
/**
@@ -59,13 +59,15 @@ class Admin extends AdminController
->order($this->sort)
->select();
$data = [
'code' => 0,
'msg' => '',
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list,
'data' => $list,
];
return json($data);
}
return $this->fetch();
}
@@ -82,10 +84,7 @@ class Admin extends AdminController
$post['password'] = password(sysconfig('site', 'site_default_password', '123456'));
$this->validate($post, $rule);
try {
$model_admin = SystemAdmin::where('username', $post['username'])->find();
if (!empty($model_admin)) {
@@ -98,6 +97,7 @@ class Admin extends AdminController
}
$save ? $this->success('保存成功') : $this->error('保存失败');
}
return $this->fetch();
}
@@ -127,6 +127,7 @@ class Admin extends AdminController
}
$row->auth_ids = explode(',', $row->auth_ids);
$this->assign('row', $row);
return $this->fetch();
}
@@ -141,7 +142,7 @@ class Admin extends AdminController
$this->checkPostRequest();
$post = $this->request->post();
$rule = [
'password|登录密码' => 'require',
'password|登录密码' => 'require',
'password_again|确认密码' => 'require',
];
$this->validate($post, $rule);
@@ -159,6 +160,7 @@ class Admin extends AdminController
}
$row->auth_ids = explode(',', $row->auth_ids);
$this->assign('row', $row);
return $this->fetch();
}
@@ -192,9 +194,9 @@ class Admin extends AdminController
$this->checkPostRequest();
$post = $this->request->post();
$rule = [
'id|ID' => 'require',
'id|ID' => 'require',
'field|字段' => 'require',
'value|值' => 'require',
'value|值' => 'require',
];
$this->validate($post, $rule);
if (!in_array($post['field'], $this->allowModifyFields)) {

View File

@@ -5,18 +5,17 @@
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\admin\service\annotation;
use Doctrine\Common\Annotations\Annotation\Attributes;
/**
* 创建节点注解类
* 创建节点注解类.
*
* @Annotation
* @Target({"METHOD","CLASS"})
@@ -26,19 +25,23 @@ use Doctrine\Common\Annotations\Annotation\Attributes;
*/
final class NodeAnotation
{
/**
* 节点名称
* 节点名称.
* @Required()
* @var string
*/
public $title;
/**
* 是否开启权限控制
* 是否开启权限控制.
* @Enum({true,false})
* @var bool
*/
public $auth = true;
}
/**
* 节点 一般无需设置.
* @var string
*/
public $name;
}

View File

@@ -5,27 +5,26 @@
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\admin\service\node;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\DocParser;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\helper\Str;
/**
* 节点处理类
* Class Node
* Class Node.
*/
class Node
{
/**
* @var string 当前文件夹
*/
@@ -44,13 +43,14 @@ class Node
*/
public function __construct($basePath, $baseNamespace)
{
$this->basePath = $basePath;
$this->baseNamespace = $baseNamespace;
$this->basePath = $basePath;
$this->baseNamespace = $baseNamespace;
return $this;
}
/**
* 获取所有节点
* 获取所有节点.
* @return array
* @throws \Doctrine\Common\Annotations\AnnotationException
* @throws \ReflectionException
@@ -66,24 +66,43 @@ class Node
$reader = new AnnotationReader($parser);
foreach ($controllerList as $controllerFormat => $controller) {
// 获取类和方法的注释信息
$reflectionClass = new \ReflectionClass($controller);
$methods = $reflectionClass->getMethods();
$actionList = [];
$methods = $reflectionClass->getMethods();
$actionList = [];
// 遍历读取所有方法的注释的参数信息
foreach ($methods as $method) {
// 读取NodeAnotation的注解
$nodeAnnotation = $reader->getMethodAnnotation($method, NodeAnotation::class);
if (!empty($nodeAnnotation) && !empty($nodeAnnotation->title)) {
$actionTitle = !empty($nodeAnnotation) && !empty($nodeAnnotation->title) ? $nodeAnnotation->title : null;
$actionAuth = !empty($nodeAnnotation) && !empty($nodeAnnotation->auth) ? $nodeAnnotation->auth : false;
$actionTitle = !empty($nodeAnnotation) && !empty($nodeAnnotation->title) ? $nodeAnnotation->title : null;
$actionAuth = !empty($nodeAnnotation) && !empty($nodeAnnotation->auth) ? $nodeAnnotation->auth : false;
$method_name = $nodeAnnotation->name;
if (empty($method_name)) {
$method_name = $method->name;
}
$actionList[] = [
'node' => $controllerFormat . '/' . $method->name,
'title' => $actionTitle,
'node' => $controllerFormat . '/' . $method_name,
'title' => $actionTitle,
'is_auth' => $actionAuth,
'type' => 2,
'type' => 2,
];
}
}
// 读取挂载到控制器注解中的节点
$nodeAnnotationInController = $reader->getClassAnnotations($reflectionClass);
foreach ($nodeAnnotationInController as $nodeAnnotation) {
if ($nodeAnnotation instanceof NodeAnotation) {
$actionList[] = [
'node' => $controllerFormat . '/' . $nodeAnnotation->name,
'title' => $nodeAnnotation->title,
'is_auth' => $nodeAnnotation->auth,
'type' => 2,
];
}
}
@@ -92,23 +111,24 @@ class Node
if (!empty($actionList)) {
// 读取Controller的注解
$controllerAnnotation = $reader->getClassAnnotation($reflectionClass, ControllerAnnotation::class);
$controllerTitle = !empty($controllerAnnotation) && !empty($controllerAnnotation->title) ? $controllerAnnotation->title : null;
$controllerAuth = !empty($controllerAnnotation) && !empty($controllerAnnotation->auth) ? $controllerAnnotation->auth : false;
$nodeList[] = [
'node' => $controllerFormat,
'title' => $controllerTitle,
$controllerTitle = !empty($controllerAnnotation) && !empty($controllerAnnotation->title) ? $controllerAnnotation->title : null;
$controllerAuth = !empty($controllerAnnotation) && !empty($controllerAnnotation->auth) ? $controllerAnnotation->auth : false;
$nodeList[] = [
'node' => $controllerFormat,
'title' => $controllerTitle,
'is_auth' => $controllerAuth,
'type' => 1,
'type' => 1,
];
$nodeList = array_merge($nodeList, $actionList);
$nodeList = array_merge($nodeList, $actionList);
}
}
}
return $nodeList;
}
/**
* 获取所有控制器
* 获取所有控制器.
* @return array
*/
public function getControllerList()
@@ -117,20 +137,18 @@ class Node
}
/**
* 遍历读取控制器文件
* 遍历读取控制器文件.
* @param $path
* @return array
*/
protected function readControllerFiles($path)
{
list($list, $temp_list, $dirExplode) = [[], scandir($path), explode($this->basePath, $path)];
$middleDir = isset($dirExplode[1]) && !empty($dirExplode[1]) ? str_replace('/', '\\', substr($dirExplode[1], 1)) . "\\" : '';
$middleDir = isset($dirExplode[1]) && !empty($dirExplode[1]) ? str_replace('/', '\\', substr($dirExplode[1], 1)) . '\\' : '';
foreach ($temp_list as $file) {
// 排除根目录和没有开启注解的模块
if ($file == ".." || $file == ".") {
if ($file == '..' || $file == '.') {
continue;
}
if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
@@ -147,11 +165,10 @@ class Node
$className = str_replace('.php', '', $file);
$controllerFormat = str_replace('\\', '.', $middleDir) . Str::snake(lcfirst($className));
$list[$controllerFormat] = "{$this->baseNamespace}\\{$middleDir}" . $className;
}
}
return $list;
}
}