<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Date: 2021/2/9 下午3:23
// +----------------------------------------------------------------------

namespace library\utils;


use app\model\system\Admin;
use app\model\system\AdminLog;
use app\Request;

class AdminLogUtils
{

    private $request;

    /**
     * 路由配置文件 | 读取@路径访问标题内容
     * @var
     */
    private $_path;
    /**
     * 日志model
     * @var AdminLog
     */
    private $adminLogModel;

    private $logConfig = null;

    public function __construct(Request $r)
    {
        $this->logConfig     = \config('log');
        $this->request       = $r;
        $this->adminLogModel = new AdminLog;
    }

    /**
     * 路由配置文件
     * // @基本设置
     *   Route::group('sys', function () {
     *
     *
     * }
     * 自动识别@参数
     * @param $path
     * @return AdminLogUtils
     */
    public function path($path): AdminLogUtils
    {
        $this->_path = $path;
        return $this;
    }

    /**
     * 记录日志
     */
    public function log(): void
    {
        $pathinfo = $this->request->pathinfo();
        //禁用访问
        if ($this->logConfig['LOG_TYPE'] == 1 && in_array($pathinfo, $this->logConfig['LOG_DISABLE'])) {
            return;
        }
        //部分记录
        if ($this->logConfig['LOG_TYPE'] == 2 && !in_array($pathinfo, $this->logConfig['LOG_ALLOW'])) {
            return;
        }
        $ip = get_client_ip();
        if(empty($ip)){
            $ip = $this->request->ip();
        }
        $title                  = $this->getRoule($pathinfo);
        $save                   = [];
        $save['admin_username'] = $this->request->adminInfo['username'];
        $save['time']           = time();
        $save['ip']             = $ip;
        $save['action']         = $pathinfo;
        if ($this->logConfig['LOG_PARAM']) {
            $post         = array_filter($this->request->post(), function ($item) {
                if (empty($item)) return false;
                return true;
            });
            $get          = array_filter($this->request->get(), function ($item) {
                if (empty($item)) return false;
                return true;
            });
            $save['post'] = json_encode($post, \JSON_UNESCAPED_UNICODE);
            $save['get']  = json_encode($get, \JSON_UNESCAPED_UNICODE);
        }
        $save['title'] = empty($title) ? '-无-' : $title;
        $this->adminLogModel->insert($save);
    }

    /**
     * 获取路由参数
     * @param $pathinfo
     */
    private function getRoule($pathinfo)
    {
        $expAr = explode('/', $pathinfo);
        if (empty($expAr)) return '';
        $strAr = [];
        //路由一级
        $content = @file_get_contents($this->_path . "/{$expAr[0]}.php");
        preg_match("/\/\/\@(.*?)Route::group/is", $content, $match);
        if (!empty($match)) {
            $strAr[] = trim($match[1]);
        }
        //路由二级
        if (count($expAr) > 1) {
            $ruleName = $expAr[1];
            preg_match("/\/\/\@(.[^Route::group]*?)Route::(post|get|rule)(\(\'{$ruleName}\'|\(\"{$ruleName}\")/s", $content, $match);
            if (!empty($match)) {
                $strAr[] = trim($match[1]);
            }
        }
        return empty($strAr) ? '' : join('-', $strAr);
    }
}