Laravel框架实现后台操作日志功能(provider服务、event事件、listen监听、observe观察者)(二)

在App\Observer下创建ModelObserver.php
加入以下代码
<?php
/**
 * Created by PhpStorm.
 * User: Elkan
 * Date: 2021/03/07
 * Time: 下午10:04
 * Describe: 数据变更Observers
 */

namespace App\Observers;

use Request;
use App\Model\Manage\LogModel;
use Illuminate\Support\Arr;
use App\Events\DefaultLog;


class OperationObservers
{

    /**
     * 模型新建后
     */
    public function created($model)
    {
        $attributes = $model->getAttributes();
        $attributes = Arr::except($attributes, ['created_at', 'updated_at']);
        # 修改后的数据
        $dirty = $model->getDirty();

        # 模型
        $baseModel = class_basename($model);
        # 模型名称
        $baseModelName = LogModel::$Model[$baseModel];
        # 接口
        $path = Request::url();
        # 影响的ID
        $model_id = $model->getKey();
        $title = "添加{$baseModelName}信息";
        $content = json_encode($dirty);

        # 创建
        $type = 1;
        # 处理
        $this->handle($title, $content, '', $path, $type, $model_id);
    }

    /**
     * 监听数据保存后的事件
     *
     * @param $model
     */
    public function saved($model)
    {
        # 这里是监听数据变更后的事件,可以在这里对缓存进行处理
    }

    /**
     * 只有确定更新后才记录日志
     */
    public function updated($model)
    {
        # 修改前的数据
        $original = $model->getOriginal();
        # 修改后的数据
        $dirty = $model->getDirty();

        if (count($dirty)) {
            # 模型
            $baseModel = class_basename($model);
            # 模型名称
            $baseModelName = LogModel::$Model[$baseModel];
            $title = "修改{$baseModelName}信息";
            # 用户登录信息
            $user = request()->get('member');
            $user_id = $user['admin_user_id'] ?? 0;
            # 接口
            $path = Request::url();
            # 影响的ID
            $model_id = $model->getKey();
            # 修改内容
            $content = json_encode($dirty);
            # 修改
            $type = 2;
            # 处理
            $this->handle($title, $content, json_encode($original), $path, $type, $model_id);
        }
    }

    /**
     * 模型删除后
     */
    public function deleted($model)
    {
        $original = [];
        # 删除信息
        $attributes = $model->getAttributes();
        # 模型
        $baseModel = class_basename($model);
        # 模型名称
        $baseModelName = LogModel::$Model[$baseModel];
        # 影响的ID
        $model_id = $model->getKey();
        $title = "删除{$baseModelName}信息";
        $content = json_encode($attributes);
        $path = Request::url();
        # 删除
        $type = 3;
        # 处理
        $this->handle($title, $content, json_encode($original), $path, $type, $model_id);
    }

    /**
     * 处理
     *
     * @param $title
     * @param $content
     * @param $input
     * @param $path
     * @param $type
     * @param $model_id
     */
    public function handle($title, $content, $input, $path, $type, $model_id)
    {
        # 用户登录信息
        $user = request()->get('member');
        $user_id = $user['admin_user_id'] ?? 0;
        event(new DefaultLog($title, $content, $input, $user_id, $path, $type, $model_id));
    }
}
创建Config\Power
加入以下代码
<?php
/**
 * Created by PhpStorm.
 * User: Elkan
 * Date: 2021/3/07
 * Time: 下午10:05
 * Describe: 服务中间件配置
 */

return [
    /**
     * 事件相关
     */
    'event' => [
        /**
         * 监听者
         */
        'listeners' => [
            /**
             * 默认的记录事件
             */
            'App\Events\DefaultLog' => [
                'App\Listeners\DefaultLogListener',
            ],
        ],

        /**
         * 模型观察者.
         * 以下添加的模型都被OperationObservers监听和观察
         * 自行扩展
         */
        'observers' => [
            \App\Models\AdminLog::class,
        ],
    ],
];
php artisan make:provider PowerServiceProvider
<?php
/**
 * Created by PhpStorm.
 * User: Elkan
 * Date: 2021/3/07
 * Time: 下午10:10
 * Describe: 数据变更服务
 */

namespace App\Providers;


use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use App\Observers\OperationObservers;

class PowerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerEvents();
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * 执行配置文件
     */
    public function registerEvents()
    {
        /**
         * 监听者 默认的记录事件
         */
        $allListeners = config('power.event.listeners');
        foreach ($allListeners as $event => $listeners) {
            foreach ($listeners as $listener) {
                Event::listen($event, $listener);
            }
        }


        /**
         *  监听广播 BUT 现在没相关应用

        $subscribers = config('power.event.subscribers');
        foreach ($subscribers as $subscriber) {
            Event::subscribe($subscriber);
        }
         **/


        /**
         * 模型观察 处理 可扩展
         */
        $observers = config('power.event.observers');
        foreach ($observers as $observer) {
            $observer::observe(OperationObservers::class);
        }
    }
}

在打开config/app.php,找到providers数组追加
App\Providers\PowerServiceProvider::class

Elkan的小破站
请先登录后发表评论
  • latest comments
  • 总共0条评论