luck-draw/app/controller/Base.php

86 lines
2.4 KiB
PHP

<?php
namespace app\controller;
use app\model\ArchivesCategory as ArchivesCategoryModel;
use app\model\Category;
use app\model\Link;
use app\model\System;
use app\repository\BlockRepository;
use app\repository\CmsRepository;
use think\facade\Cache;
use think\facade\Config;
/**
* 控制器基础类
*/
class Base extends BaseController
{
//需要向模板传递的值
protected $data = [
"activeCategoryId"=> 0,//当前选中的顶级栏目
"links"=> [],//友情连接
"article"=> [],//文章
"blocks"=> [],//碎片
];
//系统配置信息
protected $system = [];
protected $auth = [];
protected $authId = 0;
protected $aboutCategory = [];
// 初始化
protected function initialize()
{
$this->auth = session('frontend_auth') ?? [];
$this->data['auth'] = $this->auth;
$this->authId = $this->auth['id'] ?? 0;
//加载基础配置
Config::load('extra/base', 'extra_system');
$this->system = config("extra_system");
$this->data['system'] = $this->system;
$this->setSeo();
$this->setLinks();
$this->setActiveCategory(ArchivesCategoryModel::index_id);
}
//设置选中的栏目
protected function setActiveCategory($id)
{
$this->data["active_category_id"] = $id;
}
//设置友情链接 缓存1小时
protected function setLinks()
{
if(Cache::has("links")){
$this->data["links"] = Cache::get("links",[]);
}else{
$links = Link::findList([],[],1,20,null,["sort"=>"asc"]);
if(isset( $links['list'])){
$this->data["links"] = $links['list']->toArray();
}else{
$this->data["links"] = [];
}
Cache::set("links",$this->data["links"],3600);
}
}
//设置SEO信息
protected function setSeo($title = '', $keywords = '', $description = '')
{
$this->data['seoTitle'] = $title ?: $this->system['seo_title'] ?? '';
$this->data['seoKeywords'] = $keywords ?: $this->system['seo_keywords'] ?? '';
$this->data['seoDescription'] = $description ?: $this->system['seo_description'] ?? '';
}
//模板
protected function view($template = '')
{
return view($template)->assign($this->data);
}
}