132 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			132 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace app\controller;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use app\model\Category;
							 | 
						||
| 
								 | 
							
								use app\model\ConfigSetting;
							 | 
						||
| 
								 | 
							
								use app\model\Link;
							 | 
						||
| 
								 | 
							
								use app\model\System;
							 | 
						||
| 
								 | 
							
								use app\model\VisitLogoModel;
							 | 
						||
| 
								 | 
							
								use think\facade\Config;
							 | 
						||
| 
								 | 
							
								use think\response\Redirect;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * 控制器基础类
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class Base extends BaseController
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    //需要向模板传递的值
							 | 
						||
| 
								 | 
							
								    protected $data = [];
							 | 
						||
| 
								 | 
							
								    //系统配置信息
							 | 
						||
| 
								 | 
							
								    protected $system = [];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 初始化
							 | 
						||
| 
								 | 
							
								    protected function initialize()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								//        $this->system         = System::getSystem();
							 | 
						||
| 
								 | 
							
								//        $this->data['system'] = $this->system;
							 | 
						||
| 
								 | 
							
								//        $this->data['links']  = Link::getList();
							 | 
						||
| 
								 | 
							
								        if (session('?__token__')) {
							 | 
						||
| 
								 | 
							
								            $this->data['_token'] = session('__token__');
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            $this->data['_token'] = $this->request->buildToken();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        Config::load('extra/base', 'base');
							 | 
						||
| 
								 | 
							
								//        var_dump(config('base'));exit;
							 | 
						||
| 
								 | 
							
								        $this->system = config('base');
							 | 
						||
| 
								 | 
							
								        $this->data['extraBase'] = config('base');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $keyword = input('keyword/s', '');
							 | 
						||
| 
								 | 
							
								        if (!empty($keyword)) {
							 | 
						||
| 
								 | 
							
								            $this->data['keyword'] = $keyword;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//        $this->nav();
							 | 
						||
| 
								 | 
							
								//        $this->logVisit();
							 | 
						||
| 
								 | 
							
								//        $this->setNofollowATagNames();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //设置SEO信息
							 | 
						||
| 
								 | 
							
								    protected function setSeo($title, $keywords = '', $description = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->data['seoTitle']       = $title;
							 | 
						||
| 
								 | 
							
								        $this->data['seoKeywords']    = $keywords;
							 | 
						||
| 
								 | 
							
								        $this->data['seoDescription'] = $description;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //模板
							 | 
						||
| 
								 | 
							
								    protected function view($template = '')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return view($template)->assign($this->data);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function setExtraBaseConfig()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->data['extraBase'] = ConfigSetting::getConfigContentsByName('extraBase');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /*
							 | 
						||
| 
								 | 
							
								     * 重构异常提示页面
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    protected function error($msg = '', string $url = null, $data = [], int $code = 404): Redirect
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        switch ($code) {
							 | 
						||
| 
								 | 
							
								            case 500:
							 | 
						||
| 
								 | 
							
								                return $this->redirect('/500.html');
							 | 
						||
| 
								 | 
							
								            default:
							 | 
						||
| 
								 | 
							
								                return $this->redirect('/404.html');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 记录访问日志
							 | 
						||
| 
								 | 
							
								    protected function logVisit()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $referer     = $_SERVER["HTTP_REFERER"] ?? '';
							 | 
						||
| 
								 | 
							
								        $domain      = $this->request->domain();
							 | 
						||
| 
								 | 
							
								        $checkDomain = substr($referer, 0, strlen($domain));
							 | 
						||
| 
								 | 
							
								        if (!empty($referer) && $domain !== $checkDomain) {
							 | 
						||
| 
								 | 
							
								            //            session('first_landing_page', $this->request->url(true));
							 | 
						||
| 
								 | 
							
								            VisitLogoModel::create([
							 | 
						||
| 
								 | 
							
								                'ip'          => request()->ip(),
							 | 
						||
| 
								 | 
							
								                'referer'     => $referer,
							 | 
						||
| 
								 | 
							
								                'visit'       => $this->request->url(true),
							 | 
						||
| 
								 | 
							
								                'create_time' => time(),
							 | 
						||
| 
								 | 
							
								            ]);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // HTML a标签设置 rel="nofollow" 属性,禁止搜索引擎追踪
							 | 
						||
| 
								 | 
							
								    protected function setNofollowATagNames()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->data['nofollowList']          = [
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								        $this->data['footerAllowFollowList'] = [
							 | 
						||
| 
								 | 
							
								            '客户案例', '公司新闻',
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    protected function nav()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->data['menus'] = Category::navList()['menus'];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 所有下级栏目ID
							 | 
						||
| 
								 | 
							
								    private function getAllSubordinateIds($items, $curId = 0)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $list = [];
							 | 
						||
| 
								 | 
							
								        foreach ($items as $k => $item) {
							 | 
						||
| 
								 | 
							
								            if ($item['parent_id'] == $curId) {
							 | 
						||
| 
								 | 
							
								                $list[] = $item['id'];
							 | 
						||
| 
								 | 
							
								                unset($items[$k]);
							 | 
						||
| 
								 | 
							
								                $childrenIds = $this->getAllSubordinateIds($items, $item['id']);
							 | 
						||
| 
								 | 
							
								                if ($childrenIds) {
							 | 
						||
| 
								 | 
							
								                    $list = array_merge($list, $childrenIds);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $list;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |