Server : nginx/1.20.1
System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64
User : www ( 1000)
PHP Version : 7.3.28
Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Directory :  /www/wwwroot/0531yanglao.com/app/admin/middleware/
Upload File :
Current Directory [ Writeable ] Root Directory [ Writeable ]


Current File : /www/wwwroot/0531yanglao.com/app/admin/middleware/AdminActionRecord.php
<?php

namespace app\admin\middleware;

use app\common\model\ActionRecord;
use app\common\model\Equipment;
use app\common\model\Materials;
use Closure;
use think\Request;
use think\Response;

class AdminActionRecord
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param Closure $next
     * @return Response
     */
    public function handle($request, Closure $next)
    {
        $action = [
            'admin/materials/add',
            'admin/materials/edit',
            'admin/materials/del',
            'admin/equipment/add',
            'admin/equipment/edit',
            'admin/equipment/del',
        ];
        $request_pathinfo = request_pathinfo();
        if(in_array($request_pathinfo, $action)) {
            //操作类型
            $action_type =  '';
            if(stripos($request_pathinfo, 'add')) {
                $action_type = 1;
            }elseif(stripos($request_pathinfo, 'edit')) {
                $action_type = 2;
            }elseif(stripos($request_pathinfo, 'del')) {
                $action_type = 3;
            }
            $type = '';
            if(stripos($request_pathinfo, 'materials')) {
                $type = 1;
            }else {
                $type = 2;
            }
            $pk = $type == 1 ? 'materials_id' : 'eid';
            //更新操作
            if($action_type == 2 && \request()->isPost()) {
                //修改操作
                $this->update($pk, $action_type, $type);
            }elseif($action_type == 1) {
                $this->add($action_type, $type);
            }elseif($action_type == 3) {
                $this->del($pk, $action_type, $type);
            }
        }

        return $next($request);
    }

    /**
     * @Description 更新操作
     * @param $pk   主键名
     * @param $action_type  操作类型
     * @param $type 类型1材料,2设备
     * @return void
     * @author  Aaron
     * @date  2022/9/13 09:55
     */
    private function update($pk, $action_type, $type)
    {
        $param = \request()->param();
        $model = $type == 1 ? new Materials() : new Equipment();
        $pkv = $param[$pk];
        $origin_data = $model->find($pkv)->toArray();
        $update_data = $param;
        unset($update_data['type']);
        $data = [
            'user_id'    =>  admin_user_id(),
            'origion_data'    =>  var_export($origin_data, true),
            'update_data'    =>  var_export($update_data, true),
            'action_type'   =>  $action_type,
            'type'   =>  $type,
            'diff_data'   =>  var_export(array_diff_assoc($origin_data, $update_data), true),
        ];
        ActionRecord::add($data);
    }

    private function add($action_type, $type)
    {
        $param = \request()->param();
        $origin_data = [];
        $update_data = $param;
        unset($update_data['type']);
        $data = [
            'user_id'    =>  admin_user_id(),
            'origion_data'    =>  '',
            'update_data'    =>  var_export($update_data, true),
            'action_type'   =>  $action_type,
            'type'   =>  $type,
        ];
        ActionRecord::add($data);
    }

    private function del($pk, $action_type, $type)
    {
        $param = \request()->param();
        $model = $type == 1 ? new Materials() : new Equipment();
        $pkv = $param[$pk];
        $origin_data = $model->find($pkv)->toArray();
        //$update_data = $param;
        //unset($update_data['type']);
        $data = [
            'user_id'    =>  admin_user_id(),
            'origion_data'    =>  var_export($origin_data, true),
            'update_data'    =>  '',
            'action_type'   =>  $action_type,
            'type'   =>  $type,
        ];
        ActionRecord::add($data);
    }
}