<?php
/**
 * Copyright: Shanghai NuLang Network Technology Co., Ltd.
 * Faith: Some things do not need others to understand.
 * Project: CoverCMS <Your world, your ambition>
 * License: http://www.svipplus.cn/license/
 * Author: The Rain° <rain@svipplus.cn>
 * File: SecurityListener.php
 * Time: 13:23
 */

namespace app\common\listener;


use app\common\toolKit\CommonKit;
use think\Db;

class SecurityListener
{
    public function run()
    {
        $module = request()->module();
        $controller = request()->controller();
        $action = request()->action();
        if ($module=='dash'){
            $hasLogin = true;
            if (!session('?manager')){
                if (!cache('?manager')){
                    $hasLogin = false;
                }else{
                    session('manager',cache('manager'));
                }
            }
            if (!$hasLogin){
                if ($controller!='Manager'){
                    echo '<script type="text/javascript">window.location.href=\''.request()->domain().request()->root().'/dash/manager/login'.'\';</script>';
                }
                if($action!='login' && $action!='logout'){
                    echo '<script type="text/javascript">window.location.href=\''.request()->domain().request()->root().'/dash/manager/login'.'\';</script>';
                }
            }else{
                if ($controller=='Manager' && $action=='login'){
                    echo '<script type="text/javascript">window.location.href=\''.request()->domain().request()->root().'/dash'.'\';</script>';
                }
            }
        }elseif ($module=='app'){
            if (cache('?sites')){
                $sites = cache('sites');
                $site = $sites[1];
            }else{
                $site = Db::name('site')->where('id',1)->find();
            }
            $status = json_decode($site['status'], true);
            if (!$status['o']){
                echo $status['m'];
                exit();
            }
        }

    }
}