Yii2 后台添加《操作日志》的功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35296546/article/details/70229125

本文简单的介绍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
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页