<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2019/12/5
 * Time: 17:44
 */


namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use app\admin\model\AdminLog;
use app\admin\model\Urlconfig;
use think\Cache;
use think\Db;

class Main extends Permissions
{
    public function index()
    {
        //tp版本号
        $info['tp'] = THINK_VERSION;
        //php版本
        $info['php'] = PHP_VERSION;
        //操作系统
        $info['win'] = PHP_OS;
        //最大上传限制
        $info['upload_max_filesize'] = ini_get('upload_max_filesize');//不能使用ini_set修改
        $info['post_max_size'] = ini_get('post_max_size');//不能使用ini_set修改
        $info['memory_limit'] = ini_get('memory_limit');
        $info['tplay_filesize'] = \app\common\model\Webconfig::getValue('file_size') * 1024;
        //脚本执行时间限制
        $info['max_execution_time'] = ini_get('max_execution_time') . 'S';
        //运行环境
//        $sapi = php_sapi_name();
//        if ($sapi == 'apache2handler') {
//            $info['environment'] = 'apache';
//        } elseif ($sapi == 'cgi-fcgi') {
//            $info['environment'] = 'cgi';
//        } elseif ($sapi == 'cli') {
//            $info['environment'] = 'cli';
//        } else {
//            $info['environment'] = $sapi;
//        }
        $info['environment'] = $_SERVER['SERVER_SOFTWARE'];
        try {
            //剩余空间大小，服务器没有权限会报错
            $info['disk'] = format_bytes(disk_free_space("/"));
        } catch (\Exception $e) {
        }

        $this->assign('info', $info);

        //==============网站数据=============================

        //用户
        $web['user_num'] = Db::name('user')->where('status', \app\common\model\User::STATUS_PASS)->count();
        //预约
        $web['application_num'] = Db::name('appointment_application')->count();
        //反馈
        $web['feedback_num'] = Db::name('feedback')->count();
        //公告
        $web['announcement_num'] = Db::name('announcement')->count();

        //
        $web['user_num_today'] = Db::name('user')->where('status', \app\common\model\User::STATUS_PASS)->whereTime('create_time', 'today')->count();
        $web['application_num_today'] = Db::name('appointment_application')->whereTime('create_time', 'today')->count();
        $web['feedback_num_today'] = Db::name('feedback')->whereTime('create_time', 'today')->count();
        $web['announcement_num_today'] = Db::name('announcement')->whereTime('create_time', 'today')->count();

        //==============管理员操作记录===========================

        $today = date('Y-m-d');
        //取当前时间的前十天
        $date = [];
        $date_string = '';
        for ($i = 9; $i > 0; $i--) {
            $date[] = date("Y-m-d", strtotime("-{$i} day"));
            $date_string .= date("Y-m-d", strtotime("-{$i} day")) . ',';
        }
        $date[] = $today;
        $date_string .= $today;
        $web['date_string'] = $date_string;

        $login_sum = '';
        foreach ($date as $k => $val) {
            $min_time = strtotime($val);
            $max_time = $min_time + 60 * 60 * 24;
            $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
            $login_sum .= Db::name('appointment_application')->where($where)->count() . ',';
        }
        $web['login_sum'] = $login_sum;
        $this->assign('web', $web);

        //==============风险提示=============================
        $safe = true;

        if (file_exists(APP_PATH . 'install')) {
            $safe = false;
            //提示删除install目录
            $this->assign('delete_install', true);
        }

        //提示改密码
        $weekpass = Db::name('admin')->where('id', session(self::ADMIN_ID))->where('password', password('123456'))->count();
        if ($weekpass > 0) {
            $safe = false;
            $this->assign('weekpass', true);
            if (!in_array($this->request->ip(), ['0.0.0.0', '127.0.0.1'])) {
                $this->assign('waring', ['msg' => "当前密码过于简单，是否立即修改？", "url" => url('admin/admin/editpassword')]);
            }
        }

        //提示设置安全入口
        if ((new Urlconfig())->isWeekBackend()) {
            $safe = false;
            $this->assign('week_backend', true);
        }

        if (session(self::ADMIN_CATE_ID) == \app\admin\model\Admin::SUPER_ADMIN_ID) {
            //登录请求过多，防止被爆破，提示管理员修改后台地址
            if ((new AdminLog())->where(['admin_menu_id' => '50', 'params' => ['<>', '']])->whereTime('create_time', 'today')->count() > 100) {
                $this->assign('waring', ['msg' => "今日发现多次登录请求，是否立即修改后台地址？", "url" => url('admin/urlsconfig/index')]);
            }
        }

        $this->assign('safe', $safe);
        return $this->fetch();
    }

    /**
     * 清除全部缓存
     */
    public function clear()
    {
        if (false == Cache::clear()) {
            $this->error('清除缓存失败');
        } else {
            $this->success('清除缓存成功');
        }
    }
}
