Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新框架为最新thinkPHP版本,插件工具composer化,完善日志链路追踪 #84

Merged
merged 7 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ runtime/admin
runtime/log
runtime/session
runtime/temp
runtime/cache
public/conf
public/WowOss.exe
app/index/controller/Test.php
Expand Down
6 changes: 3 additions & 3 deletions app/admin/config/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
// 路由中间件
'middleware' => [

// 后台视图初始化
\app\admin\middleware\ViewInit::class,
// // 后台视图初始化
// \app\admin\middleware\ViewInit::class,

// 检测用户是否登录
\app\admin\middleware\CheckAdmin::class,
// \app\admin\middleware\CheckAdmin::class,


],
Expand Down
2 changes: 1 addition & 1 deletion app/admin/controller/system/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\auth\Node as NodeService;
use app\admin\service\NodeService;
use think\App;

/**
Expand Down
10 changes: 9 additions & 1 deletion app/admin/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
// 事件定义文件
return [
'bind' => [
'MenuUpdate' => 'app\admin\event\MenuUpdate',
],

'listen' => [
'AppInit' => [
\app\admin\listener\ViewInitListener::class,
],
'HttpRun' => [
\app\admin\listener\ViewInitListener::class,
],
'HttpEnd' => [],
'LogLevel' => [],
'LogWrite' => [],
],

'subscribe' => [
Expand Down
6 changes: 6 additions & 0 deletions app/admin/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
// 系统操作日志
\app\admin\middleware\SystemLog::class,

// 后台视图初始化
// \app\admin\middleware\ViewInit::class,

// 检测用户是否登录
// \app\admin\middleware\CheckAdmin::class,


];
1 change: 1 addition & 0 deletions app/admin/middleware/CheckAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use think\Request;

/**
* @deprecated 废弃,新版TP不支持在中间件获取控制器相关信息
* 检测用户登录和节点权限
* Class CheckAdmin
* @package app\admin\middleware
Expand Down
33 changes: 23 additions & 10 deletions app/admin/middleware/SystemLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace app\admin\middleware;

use app\admin\service\SystemLogService;
use app\Request;
use EasyAdmin\tool\CommonTool;
use think\facade\Log;

/**
* 系统操作日志中间件
Expand All @@ -30,22 +32,33 @@ class SystemLog
protected $sensitiveParams = [
'password',
'password_again',
'phone',
'mobile'
];

public function handle($request, \Closure $next)
public function handle(Request $request, \Closure $next)
{
$params = $request->param();
if (isset($params['s'])) {
unset($params['s']);
}
foreach ($params as $key => $val) {
in_array($key, $this->sensitiveParams) && $params[$key] = "***********";
}
$method = strtolower($request->method());
$url = $request->url();

trace([
'url' => $url,
'method' => $method,
'params' => $params,
],
'requestDebugInfo'
);

if ($request->isAjax()) {
$method = strtolower($request->method());
if (in_array($method, ['post', 'put', 'delete'])) {
$url = $request->url();
$ip = CommonTool::getRealIp();
$params = $request->param();
if (isset($params['s'])) {
unset($params['s']);
}
foreach ($params as $key => $val) {
in_array($key, $this->sensitiveParams) && $params[$key] = password($val);
}
$data = [
'admin_id' => session('admin.id'),
'url' => $url,
Expand Down
7 changes: 6 additions & 1 deletion app/admin/middleware/ViewInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
use think\facade\Request;
use think\facade\View;

/**
* @deprecated 废弃,新版TP不支持在中间件获取控制器相关信息
* Class ViewInit
* @package app\admin\middleware
*/
class ViewInit
{

public function handle($request, \Closure $next)
public function handle(\app\Request $request, \Closure $next)
{
list($thisModule, $thisController, $thisAction) = [app('http')->getName(), Request::controller(), $request->action()];
list($thisControllerArr, $jsPath) = [explode('.', $thisController), null];
Expand Down
37 changes: 37 additions & 0 deletions app/admin/service/NodeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目:https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\service;


use EasyAdmin\auth\Node;

class NodeService
{

/**
* 获取节点服务
* @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 change: 1 addition & 0 deletions app/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function password($value)

/**
* debug调试
* @deprecated 不建议使用,建议直接使用框架自带的log组件
* @param string|array $data 打印信息
* @param string $type 类型
* @param string $suffix 文件后缀名
Expand Down
2 changes: 1 addition & 1 deletion app/common/command/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use EasyAdmin\auth\Node as NodeService;
use app\admin\service\NodeService;

class Node extends Command
{
Expand Down
74 changes: 74 additions & 0 deletions app/common/controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
namespace app\common\controller;


use app\admin\service\ConfigService;
use app\BaseController;
use app\common\constants\AdminConstant;
use app\common\service\AuthService;
use EasyAdmin\tool\CommonTool;
use think\facade\Env;
use think\facade\View;
use think\Model;

/**
Expand Down Expand Up @@ -95,6 +99,8 @@ protected function initialize()
parent::initialize();
$this->layout && $this->app->view->engine()->layout($this->layout);
$this->isDemo = Env::get('easyadmin.is_demo', false);
$this->viewInit();
$this->checkAuth();
}

/**
Expand Down Expand Up @@ -206,4 +212,72 @@ public function selectList()
$this->success(null, $data);
}

/**
* 初始化视图参数
*/
private function viewInit(){
$request = app()->request;
list($thisModule, $thisController, $thisAction) = [app('http')->getName(), app()->request->controller(), $request->action()];
list($thisControllerArr, $jsPath) = [explode('.', $thisController), null];
foreach ($thisControllerArr as $vo) {
empty($jsPath) ? $jsPath = parse_name($vo) : $jsPath .= '/' . parse_name($vo);
}
$autoloadJs = file_exists(root_path('public') . "static/{$thisModule}/js/{$jsPath}.js") ? true : false;
$thisControllerJsPath = "{$thisModule}/js/{$jsPath}.js";
$adminModuleName = config('app.admin_alias_name');
$isSuperAdmin = session('admin.id') == AdminConstant::SUPER_ADMIN_ID ? true : false;
$data = [
'adminModuleName' => $adminModuleName,
'thisController' => parse_name($thisController),
'thisAction' => $thisAction,
'thisRequest' => parse_name("{$thisModule}/{$thisController}/{$thisAction}"),
'thisControllerJsPath' => "{$thisControllerJsPath}",
'autoloadJs' => $autoloadJs,
'isSuperAdmin' => $isSuperAdmin,
'version' => env('app_debug') ? time() : ConfigService::getVersion(),
];

View::assign($data);
}

/**
* 检测权限
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function checkAuth(){
$adminConfig = config('admin');
$adminId = session('admin.id');
$expireTime = session('admin.expire_time');
/** @var AuthService $authService */
$authService = app(AuthService::class, ['adminId' => $adminId]);
$currentNode = $authService->getCurrentNode();
$currentController = parse_name(app()->request->controller());

// 验证登录
if (!in_array($currentController, $adminConfig['no_login_controller']) &&
!in_array($currentNode, $adminConfig['no_login_node'])) {
empty($adminId) && $this->error('请先登录后台', [], __url('admin/login/index'));

// 判断是否登录过期
if ($expireTime !== true && time() > $expireTime) {
session('admin', null);
$this->error('登录已过期,请重新登录', [], __url('admin/login/index'));
}
}

// 验证权限
if (!in_array($currentController, $adminConfig['no_auth_controller']) &&
!in_array($currentNode, $adminConfig['no_auth_node'])) {
$check = $authService->checkNode($currentNode);
!$check && $this->error('无权限访问');

// 判断是否为演示环境
if(env('easyadmin.is_demo', false) && app()->request->isPost()){
$this->error('演示环境下不允许修改');
}

}
}
}
7 changes: 7 additions & 0 deletions app/service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
declare (strict_types=1);

return [
0 => 'think\\captcha\\CaptchaService',
1 => 'think\\app\\Service',
];
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@
"topthink/think-orm": "^2.0",
"topthink/think-multi-app": "^1.0",
"topthink/think-view": "^1.0",
"doctrine/annotations": "^1.8",
"topthink/think-captcha": "^3.0",
"aliyuncs/oss-sdk-php": "^2.3",
"qcloud/cos-sdk-v5": "^2.0",
"qiniu/php-sdk": "^7.2",
"alibabacloud/client": "^1.5",
"jianyan74/php-excel": "^1.0"
"jianyan74/php-excel": "^1.0",
"zhongshaofa/easy-admin": "^1.0",
"ext-json": "*",
"zhongshaofa/thinkphp-log-trace": "^1.0"
},
"require-dev": {
"symfony/var-dumper": "^4.2",
"eaglewu/swoole-ide-helper": "dev-master"
},
"autoload": {
"psr-4": {
"app\\": "app",
"addons\\": "addons",
"EasyAdmin\\":"vendor/zhongshaofa/easy-admin/src",
"ServiceSwoole\\":"vendor/zhongshaofa/service-swoole/src"
"app\\": "app"
},
"psr-0": {
"": "extend/"
Expand Down
Loading