AdminLogs.php 3.93 KB
<?php

namespace common\components\adminlog;

use Yii;
use yii\db\ActiveRecord;
use yii\helpers\Url;
use common\helpers\Utils;
use common\models\AdminLog  as AdminLogModel;
use function sprintf;

/**
 * 后台管理员用户操作日志
 * Class AdminLogs
 * @package common\components\adminlog
 */
class AdminLogs
{
    public static function write($event)
    {
        // 排除日志表自身, 没有主键的表不记录
        if($event->sender instanceof AdminLogModel || !$event->sender->primaryKey()) {
            return;
        }

        // 显示详情有待优化,不过基本功能完整齐全
        if ($event->name == ActiveRecord::EVENT_AFTER_INSERT) {
            $description = "%s新增了表{%s}%s:%s的%s";
        } elseif($event->name == ActiveRecord::EVENT_AFTER_UPDATE) {
            $description = "%s修改了表{%s}%s:%s的%s";
        } else {
            $description = "%s删除了表{%s}%s:%s%s";
        }

        if (!empty($event->changedAttributes)) {
            $desc = '';
            foreach($event->changedAttributes as $name => $value) {
                $desc .= $name . ' : ' . $value . ' => ' . $event->sender->getAttribute($name) . ' | ';
            }
            $desc = substr($desc, 0, -1);
        } else {
            $desc = '';
        }

        $userName = Yii::$app->user->identity->username;
        $tableName = $event->sender->tableSchema->name;
        $description = sprintf($description, $userName, $tableName, $event->sender->primaryKey()[0], json_encode($event->sender->getPrimaryKey()), $desc);

        $route = Url::to();
        $userId = Yii::$app->user->id;
        $ip = Utils::clientIp();
        $model = new AdminLogModel();
        $model->route = $route;
        $model->description = $description;
        $model->user_id = $userId;
        $model->ip = $ip;
        $model->save();
    }

    /**
     * 后台用户-导入数据操作日志
     * @param $dataName 数据名称说明
     * @param $dataNum 插入数据条数
     * @param $updateCount 更新数据条数
     */
    public static function doImport($dataName, $dataNum, $updateCount = 0)
    {
        $userName = Yii::$app->user->identity->username;
        $description = $userName . '导入[' . $dataName . ']数据[' . $dataNum . ']条';
        if ($updateCount > 0) {
            $description = $userName . '导入[' . $dataName . ']新增数据[' . $dataNum . ']条,更新['.$updateCount.']条';
        }

        $route = Url::to();
        $userId = Yii::$app->user->id;
        $ip = Utils::clientIp();
        $model = new AdminLogModel();
        $model->route = $route;
        $model->description = $description;
        $model->user_id = $userId;
        $model->ip = $ip;
        $model->save();
    }

    /**
     * 后台用户-导出数据操作日志
     * @param $dataName 数据名称说明
     * @param $dataNum 数据条数
     */
    public static function doExport($dataName, $dataNum)
    {
        $userName = Yii::$app->user->identity->username;
        $description = $userName . '导出[' . $dataName . ']数据[' . $dataNum . ']条';

        $route = Url::to();
        $userId = Yii::$app->user->id;
        $ip = Utils::clientIp();
        $model = new AdminLogModel();
        $model->route = $route;
        $model->description = $description;
        $model->user_id = $userId;
        $model->ip = $ip;
        $model->save();
    }

    /**
     * 后台操作日志
     * @param $dataName 数据名称说明
     * @param $dataNum 数据条数
     */
    public static function doWrite($description)
    {
        $userName = Yii::$app->user->identity->username;
        $description = $userName . $description;

        $route = Url::to();
        $userId = Yii::$app->user->id;
        $ip = Utils::clientIp();
        $model = new AdminLogModel();
        $model->route = $route;
        $model->description = $description;
        $model->user_id = $userId;
        $model->ip = $ip;
        $model->save();
    }
}