<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/8/27
 * +----------------------------------------------------------------------
 * | TIME: 14:08
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: userLog
 * +----------------------------------------------------------------------
 */

namespace model;


class userLog extends \model
{
    public $table = 'oper_log';

    private $notList = [
        'admin/log/list'
    ];

    public function add($data) 
    {
       $class = \mvc::$cfg['class'];
       $class = str_replace('\admin\action','admin',$class);
       $class = str_replace('Action','',$class);
       $class .= '\\'.\mvc::$cfg['method'];
       $class = str_replace('\\','/',$class);
       if(in_array($class,$this->notList)){
           return true;
       }
       //TODO 此处可以写不计入日志的规则
       $data['ol_url'] = $class;
       $data['ol_header'] = $this->isAjax().'|'.($this->getIsPostRequest() ? 'POST' : 'GET');
       $data['opuser'] = $this->getOpuser();
       $data['create_time'] = $this->getDateTime();
        $this->db->insert($this->table,$data);
    }

    /**
     * 是否ajax 请求
     * @return bool
     */
    private function isAjax()
    {
        return isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest" ? true : false;
    }

    /**
     * 是否post 请求
     * @return bool
     */
    private function getIsPostRequest()
    {
        return isset($_SERVER['REQUEST_METHOD']) && !strcasecmp($_SERVER['REQUEST_METHOD'],'POST');
    }

}
