Base.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @file Index.php
  5. * @date 2019-02-27 14:49:36
  6. * @author huwhis<huuwhois>
  7. * @version 0.0.6
  8. */
  9. namespace app\index\controller;
  10. use think\facade\View;
  11. use think\facade\Config;
  12. use think\App;
  13. use app\common\model\Article;
  14. use app\common\model\Category;
  15. use think\facade\Env;
  16. abstract class Base
  17. {
  18. /**
  19. * Request实例
  20. * @var \think\Request
  21. */
  22. protected $request;
  23. /**
  24. * 应用实例
  25. * @var \think\App
  26. */
  27. protected $app;
  28. /**
  29. * seo
  30. */
  31. protected $seo = [];
  32. /**
  33. * makeHtmlFile
  34. */
  35. protected $html = false;
  36. /**
  37. * 构造方法
  38. * @access public
  39. * @param App $app 应用对象
  40. */
  41. public function __construct(App $app)
  42. {
  43. $this->app = $app;
  44. $this->request = $this->app->request;
  45. $this->html = Config::get('app.html', false);
  46. // 控制器初始化
  47. $this->initialize();
  48. }
  49. // 初始化
  50. protected function initialize()
  51. {
  52. // SEO标题
  53. $system = \app\common\model\System::find(1);
  54. $this->seo = [
  55. 'title' => $system->title,
  56. 'key' => $system->key,
  57. 'des' => $system->des,
  58. ];
  59. View::assign('seo', $this->seo);
  60. // 菜单
  61. $categories = Category::where('is_nav', 1)->order(['sort desc'])->select();
  62. View::assign('categories', $categories);
  63. // 侧边栏
  64. if ($this->request->has('_aside')==false) {
  65. $this->aside();
  66. }
  67. }
  68. /**
  69. * 侧边栏
  70. */
  71. protected function aside()
  72. {
  73. $cate_lists = Category::where('template', 'article')->select();
  74. $hits_lists = Article::getListByCid(0, 9, ['hits' => 'desc']);
  75. $top_lists = Article::getTop(7);
  76. $time_lists = Article::createTimeArchive();
  77. $last_lists = Article::getListByCid(0, 9);
  78. View::assign([
  79. 'cate_lists' => $cate_lists,
  80. 'hits_lists' => $hits_lists,
  81. 'top_lists' => $top_lists,
  82. 'time_lists' => $time_lists,
  83. 'last_lists' => $last_lists,
  84. ]);
  85. }
  86. protected function makeHtmlFile($html)
  87. {
  88. $basepath = $this->app->getRootPath() . 'public';
  89. $url = $this->request->baseUrl();
  90. if (substr($url, -1) === '/') {
  91. $url .= 'index.html';
  92. }
  93. if (empty(pathinfo($url, PATHINFO_EXTENSION))) {
  94. $url .= '.html';
  95. }
  96. $filename = $basepath . $url;
  97. $force = $this->request->has('force') ? (boolean) $this->request->param('force') : false;
  98. if (!file_exists($filename) || filectime($filename) <= time()-60*60*24*5 || $force == true) {
  99. $dir = dirname($filename);
  100. if (!file_exists($dir) ) {
  101. mkdir($dir, 0755, true);
  102. @file_put_contents($dir.'/index.html', ""); // 添加空html文件
  103. }
  104. @file_put_contents($filename, $html);
  105. }
  106. }
  107. }