提交 fc58c343 编写于 作者: M moTzxx

优化 记录行为

优化 记录行为
上级 d2efc44e
......@@ -9,6 +9,7 @@
```b
框架版本: ThinkPHP5.1.40
后台入口: xxxxx.com/cmsx;
PHP版本: >=7.1
```
- 最新更新
......
<?php
namespace app\cms\behavior;
use app\common\lib\BaseException;
use app\common\lib\IAuth;
use app\common\model\XcmsOpLogs;
use think\facade\Request;
use think\facade\Response;
/**
* 行为日志
* Class CmsOp
* @package app\cms\behavior
*/
class CmsLogger
{
/**
* @param string $params
* @throws BaseException
*/
public function run($params = ''){
if (empty($params)){
throw new BaseException(['msg' => '日志信息不能为空']);
}
if (is_array($params)){
list('admin_id' => $admin_id, 'admin_name' => $admin_name, 'msg' => $message) = $params;
}else{
list($admin_id,$admin_name) = IAuth::getAdminIDCurrLogged();
$message = $params;
}
$LogData = [
'message' => $message,
'admin_id' => $admin_id,
'admin_name' => $admin_name,
'status_code' => Response::getCode(),
'method' => Request::method(),
'path' => '/' . Request::path(),
];
(new XcmsOpLogs())::create($LogData);
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ use app\common\controller\CmsBase;
use app\common\model\XadminRoles;
use app\common\model\Xadmins;
use app\common\model\XnavMenus;
use think\facade\Hook;
use think\Request;
use think\response\View;
......@@ -105,7 +106,7 @@ class Admin extends CmsBase
$adminRoles = $this->ar_model->getAllRoleList();
if ($request->isGet()){
//$adminRoles = [];
return view('role_react', ['roles' => $adminRoles]);
return view('role', ['roles' => $adminRoles]);
}else{
showMsg(1,'roleList',$adminRoles);
}
......@@ -144,6 +145,7 @@ class Admin extends CmsBase
if ($request->isPost()) {
$input = $request->param();
$opRes = $this->ar_model->editRole($id, $input);
Hook::listen('cms_op',"更新了id为 {$id} 的角色信息");
showMsg($opRes['tag'], $opRes['message']);
} else {
//TODO 获取所有可以分配的权限菜单
......
......@@ -4,6 +4,7 @@ namespace app\cms\controller;
use app\common\controller\CmsBase;
use app\common\model\Xarticles;
use think\facade\Hook;
use think\Request;
use think\response\View;
......@@ -57,6 +58,7 @@ class Article extends CmsBase
if ($request->isPost()) {
$input = $request->param();
$opRes = $this->model->addArticle($input);
Hook::listen('cms_op',"新增了一篇文章");
showMsg($opRes['tag'], $opRes['message']);
} else {
return view('add');
......@@ -73,6 +75,7 @@ class Article extends CmsBase
{
if ($request->isPost()) {
$opRes = $this->model->updateCmsArticleData($request->post(),$id);
Hook::listen('cms_op',"更新了id为 {$id} 的文章");
showMsg($opRes['tag'], $opRes['message']);
} else {
$article = $this->model->getCmsArticleByID($id);
......@@ -93,6 +96,7 @@ class Article extends CmsBase
public function ajaxForRecommend(Request $request)
{
$opRes = $this->model->updateForRecommend($request->post('article_id'), $request->post('okStatus'));
Hook::listen('cms_op',"更新了id为 {$request->post('article_id')} 的文章推荐状态");
showMsg($opRes['tag'], $opRes['message']);
}
......
......@@ -25,7 +25,7 @@ class Chat extends Controller
public function __construct()
{
parent::__construct();
$this->cmsAID = IAuth::getAdminIDCurrLogged();
list($this->cmsAID) = IAuth::getAdminIDCurrLogged();
if (!$this->cmsAID) {
echo "Sorry,您已离线!";die;
}else{
......
......@@ -34,7 +34,7 @@ class Index extends Controller
public function __construct()
{
parent::__construct();
$this->cmsAID = IAuth::getAdminIDCurrLogged();
list($this->cmsAID) = IAuth::getAdminIDCurrLogged();
if (!$this->cmsAID || !IAuth::ckPasswordNoChangedCurrLogged($this->cmsAID)) {
$action = trim(strtolower(request()->action()));
if ($action == 'index'){
......
......@@ -11,8 +11,8 @@ use think\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Hook;
use think\Request;
use think\response\Redirect;
use think\response\View;
/**
......@@ -38,7 +38,7 @@ class Login extends Controller
*/
public function index()
{
$cmsAID = IAuth::getAdminIDCurrLogged();
list($cmsAID) = IAuth::getAdminIDCurrLogged();
if ($cmsAID && IAuth::ckPasswordNoChangedCurrLogged($cmsAID)) {
$this->redirect('cms/index/index');
......@@ -52,6 +52,7 @@ class Login extends Controller
*/
public function logout()
{
Hook::listen('cms_op',"退出了系统");
IAuth::logoutAdminCurrLogged();
$this->redirect('cms/login/index');
}
......@@ -70,10 +71,10 @@ class Login extends Controller
$sysConf = new XsysConf();
if ($sysConf->checkCmsIpAuth()){
$tagRes = $this->adminModel->checkAdminLogin($input);
Hook::listen('cms_op',"登录了系统");
}else{
$tagRes = ['tag'=>0,'message'=>'Sorry,Your IP is abnormal, please contact the administrator!'];
}
showMsg($tagRes['tag'], $tagRes['message']);
} else {
showMsg(0, 'sorry,您的请求不合法!');
......@@ -87,7 +88,7 @@ class Login extends Controller
public function ajaxCheckLoginStatus(Request $request)
{
if ($request->isPost()) {
$cmsAID = IAuth::getAdminIDCurrLogged();
list($cmsAID) = IAuth::getAdminIDCurrLogged();
$nav_menu_id = $request->param('nav_menu_id');
//TODO 判断当前菜单是否属于他的权限内
if ($cmsAID && IAuth::ckPasswordNoChangedCurrLogged($cmsAID) && $this->navMenuModel->checkNavMenuMan($nav_menu_id, $cmsAID)) {
......
......@@ -10,7 +10,9 @@
// +----------------------------------------------------------------------
// 应用公共文件
use app\common\lib\IAuth;
use JetBrains\PhpStorm\NoReturn;
use think\Db;
/**
* 公用的方法 返回json数据,进行信息的提示
......@@ -177,16 +179,16 @@ function insertCmsOpLogs($opStatus = 0,$opTag = '',
if (!$opStatus){
return false;
}else{
list($cmsAID) = IAuth::getAdminIDCurrLogged();
$opData = [
'op_id' => $op_id,
'tag' => $opTag,
'admin_id' => \app\common\lib\IAuth::getAdminIDCurrLogged(),
'admin_id' => $cmsAID,
'add_time' => date('Y-m-d H:i:s',time()),
'op_msg' => $op_msg
];
if ($opTag){
$opStatus = \think\Db::name('xcmsLogs')
->insert($opData);
$opStatus = Db::name('xcmsLogs')->insert($opData);
}else{
return false;
}
......@@ -202,7 +204,7 @@ function insertCmsOpLogs($opStatus = 0,$opTag = '',
* @return array|PDOStatement|string|\think\Collection
*/
function getCmsOpViewLogs($video_id = 0,$opTag = ''){
$logs = \think\Db::name('xcmsLogs l')
$logs = Db::name('xcmsLogs l')
->field('l.*,a.user_name')
->join('xadmins a','a.id = l.admin_id')
->where([['tag','=',$opTag],['op_id','=',$video_id]])
......@@ -239,7 +241,8 @@ function arrSortByKey($result = [],$key = '',$sort = SORT_DESC){
if (empty($result) || !$key){
return [];
}else{
array_multisort(array_column($result,$key),$sort,$result);
$deal_result = array_column($result,$key);
array_multisort($deal_result,$sort,$result);
return $result;
}
}
......
......@@ -41,7 +41,7 @@ class CmsBase extends Base
public function initAuth()
{
$authFlag = false;
$cmsAID = IAuth::getAdminIDCurrLogged();
list($cmsAID) = IAuth::getAdminIDCurrLogged();
if (!$cmsAID) {
$message = "You are offline,please logon again!";
} else {
......
<?php
namespace app\common\lib;
use think\Exception;
class BaseException extends Exception
{
//HTTP状态码
public $code = 400;
//错误具体信息
public $msg = '参数错误';
//自定义的错误码
public $error_code = 10000;
public function __construct($params = [])
{
if (!is_array($params)) {
return;
}
if (array_key_exists('code', $params)) {
$this->code = $params['code'];
}
if (array_key_exists('msg', $params)) {
$this->msg = $params['msg'];
}
if (array_key_exists('error_code', $params)) {
$this->error_code = $params['error_code'];
}
}
}
\ No newline at end of file
......@@ -73,11 +73,12 @@ class IAuth
Session::set(self::AUTH_CONF('SESSION_CMS_TAG'), $cms_encrypt,self::AUTH_CONF('SESSION_CMS_SCOPE'));
}
}
/**
* 获取当前登录状态下的管理员 ID信息
* @return int
* @return array
*/
public static function getAdminIDCurrLogged(): int
public static function getAdminIDCurrLogged()
{
$cmsRes = self::getDecryCmsRes();
......@@ -86,7 +87,9 @@ class IAuth
if ($time_stamp + config('session.expire') > time()){
$cmsAID = $cmsRes['op_id'] ?? 0;
}
return isset($cmsAID)?intval($cmsAID):0;
$cmsAID = isset($cmsAID)?intval($cmsAID):0;
$cmsAName = (new Xadmins())->getAdminNameByID($cmsAID??0);
return [$cmsAID,$cmsAName];
}
/**
......
......@@ -366,4 +366,13 @@ class Xadmins extends BaseModel
public function getPasswordByID($adminID = 0){
return $this->where('id',$adminID)->value('password');
}
/**
* 获取管理员昵称
* @param int $adminID
* @return mixed|string
*/
public function getAdminNameByID($adminID = 0){
return $this->where('id',$adminID)->value('user_name')??'';
}
}
......@@ -3,10 +3,7 @@
namespace app\common\model;
use app\common\validate\Xarticle;
use think\cache\driver\Memcache;
use think\cache\driver\Redis;
use think\Db;
use \think\Model;
/**
* Created by PhpStorm.
......@@ -222,7 +219,6 @@ class Xarticles extends BaseModel
'seo_description' => isset($input['seo_description'])?$input['seo_description']:'',
'status' => isset($input['status'])?$input['status']:'0',
]);
insertCmsOpLogs($saveTag||$saveTag2,'ARTICLE',$id,'文章更新');
$validateRes['tag'] = intval($saveTag||$saveTag2);
$validateRes['message'] = $saveTag||$saveTag2 ? '数据更新成功' : 'Sorry,数据无变动';
......
<?php
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
class XcmsOpLogs extends Model
{
use SoftDelete;
protected $table;
public $autoWriteTimestamp = 'datetime';
/**
* 如果只是用静态方法,是不会调用构造函数的
* XcmsOpLogs constructor.
* @param array $data
*/
public function __construct($data = [])
{
parent::__construct($data);
$prefix = config('database.prefix');
$this->table = $prefix.'xcms_op_logs';
}
}
\ No newline at end of file
......@@ -25,4 +25,9 @@ return [
'log_write' => [],
// 应用结束
'app_end' => [],
// 记录后台管理员操作日志
'cms_op' => [
'app\\cms\\behavior\\CmsLogger'
],
];
......@@ -16,7 +16,7 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=7.1",
"topthink/framework": "5.1.*",
"topthink/think-migration": "^2.0",
"tp5er/tp5-databackup": "dev-master",
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册