Index.php 7.28 KB
<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Env;
use think\facade\Request;
class Index extends Common
{
    public function initialize(){
        parent::initialize();
    }
    public function index(){
        //导航
        // 获取缓存数据
        $authRule = cache('authRule');
        if(!$authRule){
            $authRule = db('auth_rule')->where('menustatus=1')->order('sort')->select();
            cache('authRule', $authRule, 3600);
       }
        //声明数组
        $menus = array();
        foreach ($authRule as $key=>$val){
            $authRule[$key]['href'] = url($val['href']);
            if($val['pid']==0){
                if(session('aid')!=1){
                    if(in_array($val['id'],$this->adminRules)){
                        $menus[] = $val;
                    }
                }else{
                    $menus[] = $val;
                }
            }
        }
        foreach ($menus as $k=>$v){
            foreach ($authRule as $kk=>$vv){
                if($v['id']==$vv['pid']){
                    if(session('aid')!=1) {
                        if (in_array($vv['id'], $this->adminRules)) {
                            $menus[$k]['children'][] = $vv;
                        }
                    }else{
                        $menus[$k]['children'][] = $vv;
                    }
                }
            }
        }
        $this->assign('menus',json_encode($menus,true));
        return $this->fetch();
    }

    //获取最近7天的所有日期
    public function GetDate($format='Y-m-d'){
      //组合数据
      $date = [];
      for ($i=1; $i<=7; $i++){
        $date[date($format ,strtotime( '+' . $i-7 .' days'))]['sta'] = strtotime(date($format,strtotime( '+' . $i-7 .' days'))." 00:00:00");
        $date[date($format ,strtotime( '+' . $i-7 .' days'))]['end'] = strtotime(date($format,strtotime( '+' . $i-7 .' days'))." 23:59:59");
      }
      return $date;
    }
    public function main(){

        if(Request::isAjax()) {

            $arr = cache('time_total');

            if(!$arr){
                $arr  = array();
                $time = $this->GetDate();
                foreach ($time as $key => $value) {
                    $time[$key]['list'] = array_column( Db::query("SELECT
                                                type
                                            FROM
                                                `xt-member-two-LINE`.xt_dc_reserve AS r
                                                LEFT JOIN `xt-member-two-LINE`.xt_dc_dz_seller_type_rel AS t ON r.sid = t.sid 
                                            WHERE
                                                r.cdate BETWEEN ".$value['sta']." 
                                                AND ".$value['end']." 
                                                AND t.type_id = 1 AND r.status not in (4,6)"), 'type');
                    $time[$key]['cancel'] = Db::query("SELECT
                                                count(r.id) as cancel
                                            FROM
                                                `xt-member-two-LINE`.xt_dc_reserve AS r
                                                LEFT JOIN `xt-member-two-LINE`.xt_dc_dz_seller_type_rel AS t ON r.sid = t.sid 
                                            WHERE
                                                r.cdate BETWEEN ".$value['sta']." 
                                                AND ".$value['end']." 
                                                AND t.type_id = 1 AND r.status = 4");
                    $time[$key]['refuse'] = Db::query("SELECT
                                                count(r.id) as refuse
                                            FROM
                                                `xt-member-two-LINE`.xt_dc_reserve AS r
                                                LEFT JOIN `xt-member-two-LINE`.xt_dc_dz_seller_type_rel AS t ON r.sid = t.sid 
                                            WHERE
                                                r.cdate BETWEEN ".$value['sta']." 
                                                AND ".$value['end']." 
                                                AND t.type_id = 1 AND r.status = 6");
                }
                foreach ($time as $key => $value) {
                    $arr['time'][]   = $key;    //日期
                    $arr['online'][] = array_sum($value['list']);   //最近7天每天在线订单
                    $arr['unline'][] = count($value['list']) - array_sum($value['list']);  //最近7天每天内部订单
                    $arr['cancel'][] = intval(join(",",$value['cancel'][0]));   //最近7天每天取消的
                    $arr['refuse'][] = intval(join(",",$value['refuse'][0]));   //最近7天每天拒单的
                    unset($time[$key]['list']);
                }
                $arr['online_sum'] = array_sum($arr['online']);  //最近7天在线订单
                $arr['unline_sum'] = array_sum($arr['unline']);  //最近7天内部订单
                $arr['cancel_sum'] = array_sum($arr['cancel']);  //最近7天取消的
                $arr['refuse_sum'] = array_sum($arr['refuse']);  //最近7天拒单的
                cache('time_total', $arr, 3600);  //将数据集加入缓存
            }

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$arr,'rel'=>1];
        }

        $version = Db::query('SELECT VERSION() AS ver');
        $config  = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_ip'       => $_SERVER['SERVER_ADDR'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'max_upload_size' => ini_get('upload_max_filesize')
        ];
        //查询最近7天预订情况
        $this->assign('config', $config);
        return $this->fetch();
    }
    public function welcome(){
        $config = "Welcome";
        $this->assign('config', $config);
        return $this->fetch();
    }
    public function navbar(){
        return $this->fetch();
    }
    public function nav(){
        return $this->fetch();
    }
    public function clear(){
        $R = Env::get('runtime_path');
        if ($this->_deleteDir($R)) {
            $result['info'] = '清除缓存成功!';
            $result['status'] = 1;
        } else {
            $result['info'] = '清除缓存失败!';
            $result['status'] = 0;
        }
        $result['url'] = url('admin/index/index');
        return $result;
    }
    private function _deleteDir($R)
    {
        $handle = opendir($R);
        while (($item = readdir($handle)) !== false) {
            if ($item != '.' and $item != '..') {
                if (is_dir($R . '/' . $item)) {
                    $this->_deleteDir($R . '/' . $item);
                } else {
                    if (!unlink($R . '/' . $item))
                        die('error!');
                }
            }
        }
        closedir($handle);
        return rmdir($R);
    }

    //退出登陆
    public function logout(){
        session(null);
        $this->redirect('login/index');
    }
    
}