123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- declare(strict_types=1);
- /**
- * @file Index.php
- * @date 2019-02-27 14:49:36
- * @author huwhis<huuwhois>
- * @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);
- if (Env::get('app.app_env', false)) {
- View::assign('bdtongji', $system->tongji);
- } else {
- View::assign('bdtongji', "");
- }
- // 菜单
- $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);
- }
- }
- }
|