zzwy2/app/controller/Base.php

163 lines
5.0 KiB
PHP
Executable File

<?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\response\Redirect;
/**
* 控制器基础类
*/
class Base extends BaseController
{
//需要向模板传递的值
protected $data = [];
//系统配置信息
protected $system = [];
protected $isMobile = false;
// 初始化
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();
}
$this->isMobile = request()->isMobile();
$this->data['config'] = ConfigSetting::getConfigContentsByName('extraBase');
$this->data['isMobile'] = $this->isMobile;
$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;
}
// 获取所在一级栏目banner图
protected function firstBanner($category): array
{
// 一级栏目的banner
$firstCategoryId = Category::firstGradeId($category['path'], $category['id']);
$bgImg = [];
if ($firstCategoryId == $category['id']) {
$bgImg['pc'] = ['src' => $category['src'] ?? '', 'title' => $category['title'] ?? ''];
$bgImg['mobile'] = ['src' => $category['src_mobile'] ?? '', 'title' => $category['title'] ?? ''];
} else {
$firstCategory = Category::where('id', $firstCategoryId)->field('id,title,src,src_mobile')->find();
$bgImg['pc'] = ['src' => $firstCategory['src'] ?? '', 'title' => $firstCategory['title'] ?? ''];
$bgImg['mobile'] = ['src' => $firstCategory['src_mobile'] ?? '', 'title' => $firstCategory['title'] ?? ''];
}
return $bgImg;
}
// 获取所在一级栏目背景图
protected function firstBg($category)
{
// 一级栏目的banner
$firstCategoryId = Category::firstGradeId($category['path'], $category['id']);
if ($firstCategoryId == $category['id']) {
$this->data['bg_pc'] = $category['bg'];
$this->data['bg_mobile'] = $category['bg_mobile'];
} else {
$firstCategory = Category::where('id', $firstCategoryId)->field('id,title,bg,bg_mobile')->find();
$this->data['bg_pc'] = $firstCategory['bg'];
$this->data['bg_mobile'] = $firstCategory['bg_mobile'];
}
}
}