本文簡單的介紹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 = [ 'route' => Url::to(), 'table' => $event->sender->tableName(), 'class' => $event->sender->className(), 'type' => 2, 'description' => $description, 'user_id' => Yii::$app->user->id, 'create_time' => time(), 'ip' => $ip];
$model = new \common\models\SystemLog();
$model->setAttributes($data);
$model->save(false);
}
}
/**
* 刪除操作.
* @param obj $event
* @return mixed
*/
public static function afterDelete($event)
{
// 內容
$arr = [];
foreach($event->sender as $key => $value) {
$arr[$key] = $value;
}
$description = json_encode($arr);
// IP轉換
$ip = \common\models\CommonModel::getUserHostAddressIp();
$ip = ip2long($ip);
// 保存
$data = [ 'route' => Url::to(), 'table' => $event->sender->tableName(), 'class' => $event->sender->className(), 'type' => 3, 'description' => $description, 'user_id' =>Yii::$app->user->id, 'create_time' => time(), 'ip' => $ip];
$model = new \common\models\SystemLog();
$model->setAttributes($data);
$model->save(false);
}
/**
* 插入操作.
* @param obj $event
* @return mixed
*/
public static function afterInsert($event)
{
if($event->sender->tableName() != self::DB_TABLE_LOG){
// 內容
$arr = [];
foreach($event->sender as $key => $value) {
$arr[$key] = $value;
}
$description = json_encode($arr);
// IP轉換
$ip = \common\models\CommonModel::getUserHostAddressIp();
$ip = ip2long($ip);
// 保存
$data = [ 'route' => Url::to(), 'table' => $event->sender->tableName(), 'class' => $event->sender->className(), 'type' => 1, 'description' => $description, 'user_id' => Yii::$app->user->id, 'create_time' => time(), 'ip' => $ip];
$model = new \common\models\SystemLog();
$model->setAttributes($data);
$model->save(false);
}
}
}
2、在配置文件中 backend/config/main.php
增加下面代碼:
// 操作日誌
'on beforeRequest' => function($event) {
\yii\base\Event::on(\yii\db\BaseActiveRecord::className(), \yii\db\BaseActiveRecord::EVENT_AFTER_UPDATE, ['backend\models\AdminLog', 'afterUpdate']);
\yii\base\Event::on(\yii\db\BaseActiveRecord::className(), \yii\db\BaseActiveRecord::EVENT_AFTER_DELETE, ['backend\models\AdminLog', 'afterDelete']);
\yii\base\Event::on(\yii\db\BaseActiveRecord::className(), \yii\db\BaseActiveRecord::EVENT_AFTER_INSERT, ['backend\models\AdminLog', 'afterInsert']);
},
3、在MySQL數據庫中創建 system_log
表
CREATE TABLE `system_log` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`type` tinyint(3) DEFAULT '1' COMMENT '操作類型:1添加,2修改,3刪除',
`class` varchar(255) DEFAULT '' COMMENT '操作class',
`route` varchar(255) NOT NULL DEFAULT '' COMMENT '路由',
`table` varchar(50) NOT NULL DEFAULT '' COMMENT '操作表',
`description` text COMMENT '操作信息',
`create_time` int(10) NOT NULL DEFAULT '0' COMMENT '操作時間',
`user_id` int(10) NOT NULL DEFAULT '0' COMMENT '用戶ID',
`ip` int(11) NOT NULL DEFAULT '0' COMMENT '操作人ip',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4、用gii生成AdminLog模型
php yii gii/model --ns=common\\models --modelClass=AdminLog --tableName=system_log