Declaration, most of the code in this article refers to the esayAdmin framework, address:Github.com/zhongshaofa…

SystemLogService.php


      
/** * Created by. * User: Jim * Date: 2020/10/16 * Time: 10:23 */

namespace app\index\services;


use think\Db;
use think\facade\Config;

class SystemLogService
{
    /** * instance *@var null
     */
    protected static $instance = null;

    /** * table prefix *@var string
     */
    protected $tablePrefix;

    /** * table suffix *@var string
     */
    protected $tableSuffix;

    /** * table name *@var string
     */
    protected $tableName;

    protected function __construct()
    {
        $this->tablePrefix = Config::get('database.prefix');
        $this->tableSuffix = date('Ym', time());
        $this->tableName = "{$this->tablePrefix}system_log_{$this->tableSuffix}";
    }

    protected function __clone()
    {
        // TODO: Implement __clone() method.
    }


    public static function instance()
    {
        if (is_null(self: :$instance)) {
            self: :$instance = new static(a); }return self: :$instance;
    }


    public function save($data = [])
    {
        if (empty($data)) return false;
        $this->detectTable();

        Db::startTrans();
        try {
            Db::table($this->tableName)->insert($data);
            Db::commit();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }


    protected function detectTable()
    {
        $check = Db::query("show tables like '{$this->tableName}'");

        if (empty($check)) {
            $sql = $this->getCreateSql();
            Db::execute($sql);
        }
        return true;
    }


    /** * get the SQL * to create the table according to the suffix@return string
     */
    protected function getCreateSql()
    {
        return <<<EOT
CREATE TABLE `{$this->tableName}` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', 'admin_id' int(10) unsigned DEFAULT '0' COMMENT 'admin ',' url 'varchar(1500) NOT NULL DEFAULT' COMMENT ', 'method' varchar(50) NOT NULL COMMENT 'id ',' title 'varchar(100) DEFAULT ', 'content' text NOT NULL COMMENT 'content ', 'IP' varchar(50) NOT NULL DEFAULT ', 'userAgent' varchar(255) DEFAULT 'COMMENT' user-agent ', 'create_time' int(10) DEFAULT NULL COMMENT 'id ', PRIMARY KEY (' id ')) ENGINE=InnoDB AUTO_INCREMENT=630 DEFAULT CHARSET= UTf8 ROW_FORMAT=COMPACT COMMENT={$this->tableSuffix}';
EOT; }}Copy the code

Registered middleware

Create a file

php think make:middleware AdminAuth
Copy the code

Code:


      

namespace app\http\middleware;

use app\Admin\services\SystemLogService;

class AdminAuth
{
    public function handle($request, \Closure $next)
    {
// if (condition..) {
//
/ /}
        $data['admin_id'] = 'admin_id';
        $data['url'] = 'url';
        $data['method'] = 'method';
        $data['title'] = 'title';
        $data['content'] = 'content';
        $data['ip'] = 'ip';
        $data['useragent'] = 'useragent';
        $data['create_time'] = 'create_time';
        SystemLogService::instance()->save($data);
        return $next($request); }}Copy the code

Configuring middleware

Go to the middleware.php configuration file and create it if it doesn’t exist

Configure the content


      
/** * Created by. * User: Jim * Date: 2020/10/16 * Time: 9:16 */
return [
    \app\http\middleware\AdminAuth::class,
];
Copy the code