<?php
/**
 * Created by PhpStorm.
 * User: 庞涛
 * Date: 2018/8/28
 * Time: 10:30
 */
namespace app\mobile\controller;
class Base extends \think\Controller
{
    protected $site;
    private $controller;
    private $seo;
    protected $menuSeo = true;
    public function _initialize(){
        parent::_initialize();
        if(request()->isMobile()){
        }
        $site = cache('site');
        if($site['status'] == 0){
            die($site['reson']);
        }
        $this->site = $site;
        $this->seo = [
            'title'     => $site['title'],
            'seo_title' => $site['seo_title'],
            'seo_keys'  => $site['seo_keys'],
            'seo_desc'  => $site['seo_desc']
        ];
        $qq = str_replace(',',',',$site['qq']);
        $qq = explode(',',$qq);
        $this->controller = strtolower(request()->controller());
        $this->getMenu();
        $this->setSeo();
        $this->getCate();//单页分类
        $this->assign('site',$site);
        $this->assign('qq',$qq);
        $this->assign('controller',$this->controller);
    }

    //设置站点优化
    protected function setSeo($info='',$field='title',$q=''){
        if(!empty($info)){
            $info['title'] = isset($info['title']) ? $info['title'] : $info[$field];
            $seo['title'] = empty($info['seo_title']) ? $info[$field].'_'.$this->seo['title'] : $info['seo_title'].'_'.$this->seo['title'];
            $seo['keys']  = empty($info['seo_keys']) ? (empty($info['seo_title']) ? $info['title']:$info['seo_title']) : $info['seo_keys'];//$seoarr[$key]['keys'];
            $seo['desc']  = empty($info['seo_desc']) ? '' : $info['seo_desc'];//$seoarr[$key]['desc'];
        }else{
            $site['title'] = empty($q) ? $this->seo['seo_title'].'_'.$this->seo['title'] : $q .'_'.$this->seo['title'];
            $site['keys']  = empty($this->seo['seo_keys']) ? $this->seo['seo_title'] : $this->seo['seo_keys'];
            $site['desc']  = empty($this->seo['seo_desc']) ? '' : $this->seo['seo_desc'];
            $seo = $site;
        }
        $this->assign('seo',$seo);
    }
    /**
     * 读取站点导航
     */
    private function getMenu(){
        $menu = cache('nav');
        if(!$menu){
            $map['status'] = 1;
            $lists = model('nav')->where($map)->order('ordid asc')->select();
            if($lists){
                $cate = objToArray($lists);//普通列表
                $temp = [];
                foreach($cate as $v){
                    if(!empty($v['alias'])){
                        $temp[$v['alias']] = $v;
                    }else{
                        $temp[$v['model']] = $v;
                    }
                }
                $menu = $temp;
                cache('nav',$temp);
            }

        }
        if(isset($menu[$this->controller]) && $this->menuSeo){
            $this->seo = [
                'title'     =>$this->site['title'],
                'seo_title' => empty($menu[$this->controller]['seo_title']) ? $menu[$this->controller]['title'] : $menu[$this->controller]['seo_title'],
                'seo_keys'  => $menu[$this->controller]['seo_keys'],
                'seo_desc'  => $menu[$this->controller]['seo_desc']
            ];
        }
        $this->assign('menu',$menu);
    }
    /**
     * @return mixed
     * 空操作 找不到操作方法时执行
     */
    public function _empty(){
        return $this->fetch('public/404');
    }
    private function getCate(){
        $cate = cache('oneCate');
        $articleCate = cache('articleCate');
        $this->assign('articleCate',$articleCate);
        $this->assign('oneCate',$cate);
    }
}