* @version 0.0.6 */ namespace app\index\controller; use think\facade\View; use think\facade\Config; use think\App; use app\common\model\Article; use app\common\model\Category; use think\facade\Env; abstract class Base { /** * Request实例 * @var \think\Request */ protected $request; /** * 应用实例 * @var \think\App */ protected $app; /** * seo */ protected $seo = []; /** * makeHtmlFile */ protected $html = false; /** * 构造方法 * @access public * @param App $app 应用对象 */ public function __construct(App $app) { $this->app = $app; $this->request = $this->app->request; $this->html = Config::get('app.html', false); // 控制器初始化 $this->initialize(); } // 初始化 protected function initialize() { // SEO标题 $system = \app\common\model\System::find(1); $this->seo = [ 'title' => $system->title, 'key' => $system->key, 'des' => $system->des, ]; View::assign('seo', $this->seo); // 菜单 $categories = Category::where('is_nav', 1)->order(['sort desc'])->select(); View::assign('categories', $categories); // 侧边栏 if ($this->request->has('_aside')==false) { $this->aside(); } } /** * 侧边栏 */ protected function aside() { $cate_lists = Category::where('template', 'article')->select(); $hits_lists = Article::getListByCid(0, 9, ['hits' => 'desc']); $top_lists = Article::getTop(7); $time_lists = Article::createTimeArchive(); $last_lists = Article::getListByCid(0, 9); View::assign([ 'cate_lists' => $cate_lists, 'hits_lists' => $hits_lists, 'top_lists' => $top_lists, 'time_lists' => $time_lists, 'last_lists' => $last_lists, ]); } protected function makeHtmlFile($html) { $basepath = $this->app->getRootPath() . 'public'; $url = $this->request->baseUrl(); if (substr($url, -1) === '/') { $url .= 'index.html'; } if (empty(pathinfo($url, PATHINFO_EXTENSION))) { $url .= '.html'; } $filename = $basepath . $url; $force = $this->request->has('force') ? (boolean) $this->request->param('force') : false; if (!file_exists($filename) || filectime($filename) <= time()-60*60*24*5 || $force == true) { $dir = dirname($filename); if (!file_exists($dir) ) { mkdir($dir, 0755, true); @file_put_contents($dir.'/index.html', ""); // 添加空html文件 } @file_put_contents($filename, $html); } } }