本文简单的介绍Yii2 后台添加《操作日志》的功能,该功能出于监控多用户操作后台的目的,往往需要把每个管理员的操作都记录下来。下面上代码~
1、增加文件 backend/models/AdminLog.php
,代码如下所示:
<?php
namespace backend\models;
use Yii;
use yii\helpers\Url;
/**
* 操作日志记录.
*
* @author Qimi
* @copyright Copyright (c) 2017
* @version V1.0
*/
class AdminLog
{
// 日志表名称
const DB_TABLE_LOG = 'system_log';
/**
* 修改操作.
* @param obj $event
* @return mixed
*/
public static function afterUpdate($event)
{
if(!empty($event->changedAttributes)) {
// 内容
$arr['changedAttributes'] = $event->changedAttributes;
$arr['oldAttributes'] = [];
foreach($event->sender as $key => $value) {
$arr['oldAttributes'][$key] = $value;
}
$description = json_encode($arr);
// IP转换
$ip = \common\models\CommonModel::getUserHostAddressIp();
$ip = ip2long($ip);
// 保存
$data = [