mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-08 02:52:49 +08:00
调整admin下的类库代码,将主要逻辑调整到extend下
This commit is contained in:
@@ -2,147 +2,8 @@
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\admin\model\MallCate;
|
||||
use app\admin\model\MallGoods;
|
||||
use app\admin\model\MallTag;
|
||||
use app\admin\model\SystemAdmin;
|
||||
use app\admin\model\SystemAuth;
|
||||
use app\admin\model\SystemAuthNode;
|
||||
use app\admin\model\SystemConfig;
|
||||
use app\admin\model\SystemMenu;
|
||||
use app\admin\model\SystemNode;
|
||||
use app\admin\model\SystemQuick;
|
||||
use app\common\constants\AdminConstant;
|
||||
use base\admin\service\InitAdminServiceClass;
|
||||
|
||||
class InitAdminService
|
||||
class InitAdminService extends InitAdminServiceClass
|
||||
{
|
||||
|
||||
protected $output = null;
|
||||
|
||||
public function __construct($output)
|
||||
{
|
||||
$this->output = $output;
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->initAdmin();
|
||||
$this->initAuth();
|
||||
$this->initConfig();
|
||||
$this->initMenu();
|
||||
$this->initQuick();
|
||||
$this->initMall();
|
||||
}
|
||||
|
||||
public function initMall()
|
||||
{
|
||||
$output = $this->output;
|
||||
|
||||
$output->writeln('开始初始化商城案例');
|
||||
|
||||
$list_cate = $this->requireData('MallCate');
|
||||
|
||||
$list_goods = $this->requireData('MallGoods');
|
||||
|
||||
foreach ($list_cate as $data_cate) {
|
||||
$model_cate = MallCate::create($data_cate);
|
||||
|
||||
foreach ($list_goods as $data_goods) {
|
||||
$data_goods['cate_id'] = $model_cate->id;
|
||||
MallGoods::create($data_goods);
|
||||
}
|
||||
}
|
||||
|
||||
$list_tag = $this->requireData('MallTag');
|
||||
|
||||
$this->installData(MallTag::class, $list_tag);
|
||||
}
|
||||
|
||||
public function initQuick()
|
||||
{
|
||||
$output = $this->output;
|
||||
|
||||
$output->writeln('开始初始化快捷入口');
|
||||
|
||||
$list_quick = $this->requireData('SystemQuick');
|
||||
|
||||
$this->installData(SystemQuick::class, $list_quick);
|
||||
}
|
||||
|
||||
public function initMenu()
|
||||
{
|
||||
$output = $this->output;
|
||||
|
||||
$output->writeln('开始初始化系统菜单');
|
||||
|
||||
$list_menu = $this->requireData('SystemMenu');
|
||||
|
||||
$this->installData(SystemMenu::class, $list_menu);
|
||||
}
|
||||
|
||||
public function initConfig()
|
||||
{
|
||||
$output = $this->output;
|
||||
$output->writeln('开始初始化系统设置');
|
||||
|
||||
$list_config = $this->requireData('SystemConfig');
|
||||
|
||||
$this->installData(SystemConfig::class, $list_config);
|
||||
}
|
||||
|
||||
public function initAdmin()
|
||||
{
|
||||
$output = $this->output;
|
||||
|
||||
$output->writeln('创建超级管理员');
|
||||
|
||||
$model_admin = SystemAdmin::find(AdminConstant::SUPER_ADMIN_ID);
|
||||
|
||||
if (empty($model_admin)) {
|
||||
$model_admin = new SystemAdmin();
|
||||
$model_admin->id = AdminConstant::SUPER_ADMIN_ID;
|
||||
$model_admin->head_img = '/static/admin/images/head.jpg';
|
||||
$model_admin->username = 'admin';
|
||||
$model_admin->password = password(123456);
|
||||
$model_admin->status = 1;
|
||||
$model_admin->save();
|
||||
$output->writeln('创建超级管理员成功');
|
||||
} else {
|
||||
$output->writeln('超级管理员已存在,无需初始化');
|
||||
}
|
||||
}
|
||||
|
||||
public function initAuth()
|
||||
{
|
||||
$output = $this->output;
|
||||
|
||||
$output->writeln('开始初始化权限');
|
||||
|
||||
$list_auth = $this->requireData('SystemAuth');
|
||||
|
||||
$this->installData(SystemAuth::class, $list_auth);
|
||||
|
||||
$output->writeln('开始初始化权限节点');
|
||||
|
||||
$list_auth_node = $this->requireData('SystemAuthNode');
|
||||
|
||||
$this->installData(SystemAuthNode::class, $list_auth_node);
|
||||
|
||||
$output->writeln('开始初始化系统节点');
|
||||
$list_node = $this->requireData('SystemNode');
|
||||
|
||||
$this->installData(SystemNode::class, $list_node);
|
||||
}
|
||||
|
||||
protected function installData($model_name, $list)
|
||||
{
|
||||
foreach ($list as $key => $value) {
|
||||
$model_name::create($value);
|
||||
}
|
||||
}
|
||||
|
||||
protected function requireData($table_name)
|
||||
{
|
||||
return include __DIR__ . '/initAdminData/' . $table_name . '.php';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use base\admin\service\NodeServiceClass;
|
||||
|
||||
use app\admin\service\node\Node;
|
||||
|
||||
class NodeService
|
||||
class NodeService extends NodeServiceClass
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取节点服务
|
||||
* @return array
|
||||
* @throws \Doctrine\Common\Annotations\AnnotationException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function getNodelist()
|
||||
{
|
||||
$basePath = base_path() . 'admin' . DIRECTORY_SEPARATOR . 'controller';
|
||||
$baseNamespace = "app\admin\controller";
|
||||
|
||||
$nodeList = (new Node($basePath, $baseNamespace))
|
||||
->getNodelist();
|
||||
|
||||
return $nodeList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use base\admin\service\TriggerServiceClass;
|
||||
|
||||
use think\facade\Cache;
|
||||
|
||||
class TriggerService
|
||||
class TriggerService extends TriggerServiceClass
|
||||
{
|
||||
|
||||
/**
|
||||
* 更新菜单缓存
|
||||
* @param null $adminId
|
||||
* @return bool
|
||||
*/
|
||||
public static function updateMenu($adminId = null)
|
||||
{
|
||||
if(empty($adminId)){
|
||||
Cache::tag('initAdmin')->clear();
|
||||
}else{
|
||||
Cache::delete('initAdmin_' . $adminId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新节点缓存
|
||||
* @param null $adminId
|
||||
* @return bool
|
||||
*/
|
||||
public static function updateNode($adminId = null)
|
||||
{
|
||||
if(empty($adminId)){
|
||||
Cache::tag('authNode')->clear();
|
||||
}else{
|
||||
Cache::delete('allAuthNode_' . $adminId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新系统设置缓存
|
||||
* @return bool
|
||||
*/
|
||||
public static function updateSysconfig()
|
||||
{
|
||||
Cache::tag('sysconfig')->clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,19 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | PHP交流群: 763822524
|
||||
// +----------------------------------------------------------------------
|
||||
// | 开源协议 https://mit-license.org
|
||||
// | 开源协议 https://mit-license.org
|
||||
// +----------------------------------------------------------------------
|
||||
// | github开源项目:https://github.com/zhongshaofa/EasyAdmin
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\admin\service\annotation;
|
||||
|
||||
use base\admin\service\annotation\ControllerAnnotationClass;
|
||||
use Doctrine\Common\Annotations\Annotation\Attributes;
|
||||
use Doctrine\Common\Annotations\Annotation\Required;
|
||||
use Doctrine\Common\Annotations\Annotation\Target;
|
||||
|
||||
/**
|
||||
* Class ControllerAnnotation
|
||||
* Class ControllerAnnotation.
|
||||
*
|
||||
* @Annotation
|
||||
* @Target("CLASS")
|
||||
@@ -27,23 +27,6 @@ use Doctrine\Common\Annotations\Annotation\Target;
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
final class ControllerAnnotation
|
||||
final class ControllerAnnotation extends ControllerAnnotationClass
|
||||
{
|
||||
|
||||
/**
|
||||
* Route group prefix for the controller
|
||||
*
|
||||
* @Required()
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $title = '';
|
||||
|
||||
/**
|
||||
* 是否开启权限控制
|
||||
* @Enum({true,false})
|
||||
* @var bool
|
||||
*/
|
||||
public $auth = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
namespace app\admin\service\annotation;
|
||||
|
||||
use base\admin\service\annotation\NodeAnotationClass;
|
||||
use Doctrine\Common\Annotations\Annotation\Attributes;
|
||||
|
||||
/**
|
||||
@@ -23,25 +24,6 @@ use Doctrine\Common\Annotations\Annotation\Attributes;
|
||||
* @Attribute("time", type = "int")
|
||||
* })
|
||||
*/
|
||||
final class NodeAnotation
|
||||
final class NodeAnotation extends NodeAnotationClass
|
||||
{
|
||||
/**
|
||||
* 节点名称.
|
||||
* @Required()
|
||||
* @var string
|
||||
*/
|
||||
public $title;
|
||||
|
||||
/**
|
||||
* 是否开启权限控制.
|
||||
* @Enum({true,false})
|
||||
* @var bool
|
||||
*/
|
||||
public $auth = true;
|
||||
|
||||
/**
|
||||
* 节点 一般无需设置.
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\admin\service\curd\exceptions;
|
||||
|
||||
use base\admin\service\curd\exceptions\CurdExceptionClass;
|
||||
|
||||
class CurdException extends \Exception
|
||||
class CurdException extends CurdExceptionClass
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\admin\service\curd\exceptions;
|
||||
|
||||
use base\admin\service\curd\exceptions\FileExceptionClass;
|
||||
|
||||
class FileException extends \Exception
|
||||
class FileException extends FileExceptionClass
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\admin\service\curd\exceptions;
|
||||
|
||||
use base\admin\service\curd\exceptions\TableExceptionClass;
|
||||
|
||||
class TableException extends \Exception
|
||||
class TableException extends TableExceptionClass
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,163 +12,12 @@
|
||||
|
||||
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 think\helper\Str;
|
||||
use base\admin\service\node\NodeClass;
|
||||
|
||||
/**
|
||||
* 节点处理类
|
||||
* Class Node.
|
||||
*/
|
||||
class Node
|
||||
class Node extends NodeClass
|
||||
{
|
||||
/**
|
||||
* @var string 当前文件夹
|
||||
*/
|
||||
protected $basePath;
|
||||
|
||||
/**
|
||||
* @var string 命名空间前缀
|
||||
*/
|
||||
protected $baseNamespace;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Node constructor.
|
||||
* @param string $basePath 读取的文件夹
|
||||
* @param string $baseNamespace 读取的命名空间前缀
|
||||
*/
|
||||
public function __construct($basePath, $baseNamespace)
|
||||
{
|
||||
$this->basePath = $basePath;
|
||||
$this->baseNamespace = $baseNamespace;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有节点.
|
||||
* @return array
|
||||
* @throws \Doctrine\Common\Annotations\AnnotationException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function getNodelist()
|
||||
{
|
||||
list($nodeList, $controllerList) = [[], $this->getControllerList()];
|
||||
|
||||
if (!empty($controllerList)) {
|
||||
AnnotationRegistry::registerLoader('class_exists');
|
||||
$parser = new DocParser();
|
||||
$parser->setIgnoreNotImportedAnnotations(true);
|
||||
$reader = new AnnotationReader($parser);
|
||||
|
||||
foreach ($controllerList as $controllerFormat => $controller) {
|
||||
// 获取类和方法的注释信息
|
||||
$reflectionClass = new \ReflectionClass($controller);
|
||||
$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;
|
||||
|
||||
$method_name = $nodeAnnotation->name;
|
||||
|
||||
if (empty($method_name)) {
|
||||
$method_name = $method->name;
|
||||
}
|
||||
|
||||
$actionList[] = [
|
||||
'node' => $controllerFormat . '/' . $method_name,
|
||||
'title' => $actionTitle,
|
||||
'is_auth' => $actionAuth,
|
||||
'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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 方法非空才读取控制器注解
|
||||
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,
|
||||
'is_auth' => $controllerAuth,
|
||||
'type' => 1,
|
||||
];
|
||||
$nodeList = array_merge($nodeList, $actionList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $nodeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有控制器.
|
||||
* @return array
|
||||
*/
|
||||
public function getControllerList()
|
||||
{
|
||||
return $this->readControllerFiles($this->basePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历读取控制器文件.
|
||||
* @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)) . '\\' : '';
|
||||
|
||||
foreach ($temp_list as $file) {
|
||||
// 排除根目录和没有开启注解的模块
|
||||
if ($file == '..' || $file == '.') {
|
||||
continue;
|
||||
}
|
||||
if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
|
||||
// 子文件夹,进行递归
|
||||
$childFiles = $this->readControllerFiles($path . DIRECTORY_SEPARATOR . $file);
|
||||
$list = array_merge($childFiles, $list);
|
||||
} else {
|
||||
// 判断是不是控制器
|
||||
$fileExplodeArray = explode('.', $file);
|
||||
if (count($fileExplodeArray) != 2 || end($fileExplodeArray) != 'php') {
|
||||
continue;
|
||||
}
|
||||
// 根目录下的文件
|
||||
$className = str_replace('.php', '', $file);
|
||||
$controllerFormat = str_replace('\\', '.', $middleDir) . Str::snake(lcfirst($className));
|
||||
|
||||
$list[$controllerFormat] = "{$this->baseNamespace}\\{$middleDir}" . $className;
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user