<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: sai <1430792918@qq.com>
// +----------------------------------------------------------------------
namespace plugin\saiadmin\app\event;

use plugin\saiadmin\app\cache\ReflectionCache;
use plugin\saiadmin\app\model\system\SystemLoginLog;
use plugin\saiadmin\app\model\system\SystemOperLog;
use plugin\saiadmin\app\model\system\SystemUser as SystemUserModel;

class SystemUser
{
    /**
     * 登录日志
     * @param $item
     */
    public function login($item)
    {
        $request = request();
        $ip = $request ? $request->getRealIp() : '127.0.0.1';
        $http_user_agent = $request ? $request->header('user-agent') : '';
        $data['username'] = $item['username'];
        $data['ip'] = $ip;
        $data['ip_location'] = self::getIpLocation($ip);
        $data['os'] = self::getOs($http_user_agent);
        $data['browser'] = self::getBrowser($http_user_agent);
        $data['status'] = $item['status'];
        $data['message'] = $item['message'];
        $data['login_time'] = date('Y-m-d H:i:s');
        if (isset($item['admin_id'])) {
            $data['created_by'] = $item['admin_id'];
            $data['updated_by'] = $item['admin_id'];
            $deptId = SystemUserModel::where('id', $item['admin_id'])->value('dept_id');
            if ($deptId !== null && $deptId !== '' && $deptId > 0) {
                $data['dept_id'] = $deptId;
            }
        }
        SystemLoginLog::create($data);
    }

    /**
     * 记录操作日志
     */
    public function operateLog(): bool
    {
        $request = request();
        if (!$request) {
            return false;
        }
        if ($request->method() === 'GET') {
            return false;
        }
        $info = getCurrentInfo();
        if (!$info) {
            return false;
        }
        $ip = $request->getRealIp();
        $module = $request->plugin;
        $rule = trim($request->uri());
        $data['username'] = $info['username'];
        $data['method'] = $request->method();
        $data['router'] = $rule;
        $data['service_name'] = self::getServiceName();
        $data['app'] = $module;
        $data['ip'] = $ip;
        $data['ip_location'] = self::getIpLocation($ip);
        $data['request_data'] = $this->filterParams($request->all());
        if (isset($info['dept_id']) && $info['dept_id'] > 0) {
            $data['dept_id'] = $info['dept_id'];
        } elseif (isset($info['id'])) {
            $deptId = SystemUserModel::where('id', $info['id'])->value('dept_id');
            if ($deptId !== null && $deptId !== '' && $deptId > 0) {
                $data['dept_id'] = $deptId;
            }
        }
        SystemOperLog::create($data);
        return true;
    }

    /**
     * 获取业务名称
     */
    protected function getServiceName(): string
    {
        $request = request();
        if (!$request) {
            return '未命名业务';
        }
        $permissions = ReflectionCache::getPermissionAttributes($request->controller, $request->action);
        if (!empty($permissions)) {
            return $permissions['title'] ?? '未命名业务';
        } else {
            return '未命名业务';
        }
    }

    /**
     * 过滤字段
     */
    protected function filterParams($params): string
    {
        $blackList = ['password', 'oldPassword', 'newPassword'];
        foreach ($params as $key => $value) {
            if (in_array($key, $blackList)) {
                $params[$key] = '******';
            }
        }
        return json_encode($params, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 获取IP地理位置
     */
    protected function getIpLocation($ip): string
    {
        $ip2region = new \Ip2Region();
        try {
            $region = $ip2region->memorySearch($ip);
        } catch (\Exception $e) {
            return '未知';
        }
        list($country, $province, $city, $network) = explode('|', $region['region']);
        if ($network === '内网IP') {
            return $network;
        }
        if ($country == '中国') {
            return $province . '-' . $city . ':' . $network;
        } else if ($country == '0') {
            return '未知';
        } else {
            return $country;
        }
    }

    /**
     * 获取浏览器信息
     */
    protected function getBrowser($user_agent): string
    {
        $br = 'Unknown';
        if (preg_match('/MSIE/i', $user_agent)) {
            $br = 'MSIE';
        } elseif (preg_match('/Firefox/i', $user_agent)) {
            $br = 'Firefox';
        } elseif (preg_match('/Chrome/i', $user_agent)) {
            $br = 'Chrome';
        } elseif (preg_match('/Safari/i', $user_agent)) {
            $br = 'Safari';
        } elseif (preg_match('/Opera/i', $user_agent)) {
            $br = 'Opera';
        } else {
            $br = 'Other';
        }
        return $br;
    }

    /**
     * 获取操作系统信息
     */
    protected function getOs($user_agent): string
    {
        $os = 'Unknown';
        if (preg_match('/win/i', $user_agent)) {
            $os = 'Win';
        } elseif (preg_match('/mac/i', $user_agent)) {
            $os = 'Mac';
        } elseif (preg_match('/linux/i', $user_agent)) {
            $os = 'Linux';
        } else {
            $os = 'Other';
        }
        return $os;
    }

}