Skip to content

Commit

Permalink
add SysLog
Browse files Browse the repository at this point in the history
  • Loading branch information
penghcheng committed Nov 7, 2019
1 parent e285b8c commit b8376f7
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 1 deletion.
46 changes: 46 additions & 0 deletions app/Controller/Admin/SysLogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Created by PhpStorm.
* User: penghcheng
* Date: 2019/11/7 0007
* Time: 10:06
*/

namespace App\Controller\Admin;


use App\Controller\AbstractController;
use App\Service\Instance\JwtInstance;
use App\Service\SysUserService;
use Hyperf\Di\Annotation\Inject;

class SysLogController extends AbstractController
{
/**
* @Inject()
* @var SysUserService
*/
protected $sysUserService;


/**
* 操作日志管理list
* @return \Psr\Http\Message\ResponseInterface
*/
public function sysLogList()
{
$currentLoginUserId = JwtInstance::instance()->build()->getId();

$key = (string)$this->request->input('key');
$page = (int)$this->request->input('page');
$limit = (int)$this->request->input('limit');

$result = $this->sysUserService->getSysLogList($key, $limit, $page);

return $this->response->success([
'page' => $result
]);
}


}
44 changes: 44 additions & 0 deletions app/Model/Dao/SysLogDao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace App\Model\Dao;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\SysLog;
use Hyperf\DbConnection\Db;

class SysLogDao
{
public function first($id, $throw = true)
{
$model = SysLog::query()->where('id', $id)->first();
if (empty($model) && $throw) {
throw new BusinessException(ErrorCode::USER_NOT_EXIST);
}
return $model;
}

/**
* 根据条件获取totalCount
* @param string $key
* @return int
*/
public function getTotalCount(string $key): int
{
$where = ['like', "'%" . $key . "%'"];

$count = SysLog::query()->where('username', $where)->orWhere("operation", $where)->count();

return $count;
}
}
37 changes: 37 additions & 0 deletions app/Model/SysLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $username
* @property string $operation
* @property string $method
* @property string $params
* @property int $time
* @property string $ip
* @property string $create_date
*/
class SysLog extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'sys_log';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = ['id' => 'int', 'time' => 'integer'];
}
61 changes: 61 additions & 0 deletions app/Service/Formatter/SysLogFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/7 0007
* Time: 10:32
*/

namespace App\Service\Formatter;


use App\Model\SysLog;

class SysLogFormatter extends Formatter
{
public function base(SysLog $model)
{
return [
'id' => $model->id,
'ip' => $model->ip,
'method' => $model->method,
'username' => $model->username ?? null,
'operation' => $model->operation,
'time' => $model->time,
'createDate' => $model->create_date
];
}

public function forArray($model)
{
return [
'id' => $model['id'],
'ip' => $model['ip'],
'method' => $model['method'],
'username' => $model['username'] ?? null,
'operation' => $model['operation'],
'time' => $model['time'],
'createDate' => $model['create_date']
];
}

public function arrayFormat($models)
{
$result = [];
foreach ($models as $model) {
$item = self::forArray($model);
$result[] = $item;
}
return $result;
}

public function collectionFormat($models)
{
$result = [];
foreach ($models as $model) {
$item = self::base($model);
$result[] = $item;
}
return $result;
}
}
57 changes: 56 additions & 1 deletion app/Service/SysUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\Dao\SysLogDao;
use App\Model\Dao\SysRoleDao;
use App\Model\Dao\SysUserDao;
use App\Model\SysMenu;
use App\Model\SysUser;
use App\Service\Formatter\SysLogFormatter;
use App\Service\Formatter\SysMenuFormatter;
use App\Service\Formatter\SysRoleFormatter;
use App\Service\Formatter\SysUserFormatter;
Expand All @@ -40,6 +42,13 @@ class SysUserService extends Service
protected $sysRoleDao;


/**
* @Inject()
* @var SysLogDao
*/
protected $sysLogDao;


/**
* 获取系统用户信息
* @param $userId
Expand Down Expand Up @@ -78,7 +87,7 @@ public function getNemuNav(int $user_id): array
$role_ids = Db::table('sys_user_role')->where("user_id", $user_id)->pluck('role_id');
$role_ids = $role_ids->toArray();
$datas = Db::select("SELECT * FROM sys_role_menu where role_id in (" . implode(',', $role_ids) . ");");

} else {
$datas = Db::select('SELECT * FROM sys_menu;');
}
Expand Down Expand Up @@ -646,4 +655,50 @@ public function getSysMenuDelete($id)
}


/**
* 返回日志管理列表
* @param string $key
* @param int $pageSize
* @param int $currPage
* @return array
*/
public function getSysLogList(string $key, int $pageSize = 10, int $currPage = 1): array
{
$totalCount = $this->sysLogDao->getTotalCount($key);

if ($totalCount > 0) {
$totalPage = ceil($totalCount / $pageSize);
} else {
$totalPage = 0;
}

if ($currPage <= 0 || $currPage > $totalPage) {
$currPage = 1;
}

$startCount = ($currPage - 1) * $pageSize;

$where = " 1=1 ";

if (!empty($key)) {
$where .= " and a.username like '%" . $key . "%' or a.operation like '%" . $key . "%'";
}

$sysRoles = Db::select("SELECT * FROM sys_log a JOIN (select id from sys_log order by id desc limit " . $startCount . ", " . $pageSize . ") b ON a.id = b.id where " . $where . " order by b.id desc;");

if (!empty($sysRoles)) {
$sysRoles = SysLogFormatter::instance()->arrayFormat($sysRoles);
}

$result = [
'totalCount' => $totalCount,
'pageSize' => $pageSize,
'totalPage' => $totalPage,
'currPage' => $currPage,
'list' => $sysRoles
];
return $result;
}


}
2 changes: 2 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
Router::post('sys/role/update', 'App\Controller\Admin\SysRoleController@sysRoleUpdate'); // 更新角色
Router::post('sys/role/delete', 'App\Controller\Admin\SysRoleController@sysRoleDelete'); // 删除角色

Router::get('sys/log/list', 'App\Controller\Admin\SysLogController@sysLogList'); // 日志列表

Router::post('sys/logout', 'App\Controller\Admin\SysUserController@sysLogout'); // 退出登录

},
Expand Down

0 comments on commit b8376f7

Please sign in to comment.