InitServiceProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Providers;
  3. use App\Models\Index\Information;
  4. use App\Models\Index\Switches;
  5. use Illuminate\Support\ServiceProvider;
  6. class InitServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Register services.
  10. *
  11. * @return void
  12. */
  13. public function register()
  14. {
  15. //
  16. }
  17. /**
  18. * Bootstrap services.
  19. *
  20. * @return void
  21. */
  22. public function boot()
  23. {
  24. try {
  25. //
  26. $information = Information::query()
  27. ->select('site_title', 'site_keywords', 'site_description', 'author_name', 'author_intro', 'author_avatar', 'navigation')
  28. ->first();
  29. if ($information) {
  30. $blog = [
  31. "name" => $information->site_title,
  32. "keywords" => $information->site_keywords,
  33. "description" => $information->site_description,
  34. ];
  35. $this->app->get('config')->set('vienblog.blog', $blog);
  36. $author = [ // 博主
  37. "name" => $information->author_name,
  38. "description" => $information->author_intro,
  39. "avatar" => $information->author_avatar,
  40. ];
  41. $this->app->get('config')->set('vienblog.author', $author);
  42. }
  43. $switches = Switches::query()
  44. ->select('name', 'status', 'extra')
  45. ->get()->all();
  46. if ($switches) {
  47. $dict = array();
  48. foreach ($switches as $switch) {
  49. $extra = json_decode($switch['extra'], true);
  50. $dict[$switch['name']] = array('status' => intval($switch['status']) == 1,
  51. 'extra' => is_null($extra) ? [] : $extra);
  52. }
  53. $sidebar = [
  54. "carousel" => [
  55. "open" => $dict['carousel']['status'], // 是(true)否(false)开启 默认true 开启务必设置banners
  56. "banners" => $dict['carousel']['extra']
  57. ],
  58. "motto" => [ // 座右铭
  59. "open" => $dict['motto']['status'], // 是(true)否(false)开启 默认true,
  60. "title" => $dict['motto']['extra']['title'], // 标题
  61. "content" => $dict['motto']['extra']['content'] // 内容
  62. ],
  63. "tag" => [ // 标签
  64. "open" => $dict['tag']['status'], // 是(true)否(false)开启 默认true,
  65. "title" => $dict['tag']['extra']['title'], // 标题
  66. "count" => intval($dict['tag']['extra']['count']), // 展示数量
  67. ],
  68. "category" => [ // 分类
  69. "open" => $dict['category']['status'], // 是(true)否(false)开启 默认true,
  70. "title" => $dict['category']['extra']['title'], // 标题
  71. "count" => intval($dict['category']['extra']['count']), // 展示数量
  72. ],
  73. "hot" => [ // 最热文章
  74. "open" => $dict['hot']['status'], // 是(true)否(false)开启 默认true,
  75. "title" => $dict['hot']['extra']['title'], // 标题
  76. "count" => intval($dict['hot']['extra']['count']), // 展示数量
  77. ],
  78. "latest" => [ // 最新文章
  79. "open" => $dict['latest']['status'], // 是(true)否(false)开启 默认true,
  80. "title" => $dict['latest']['extra']['title'], // 标题
  81. "count" => intval($dict['latest']['extra']['count']), // 展示数量
  82. ],
  83. "popular" => [ // 刚有人看的文章
  84. "open" => $dict['popular']['status'], // 是(true)否(false)开启 默认true,
  85. "title" => $dict['popular']['extra']['title'], // 标题
  86. "count" => intval($dict['popular']['extra']['count']), // 展示数量
  87. ],
  88. ];
  89. $adsense_script = $dict['adsense']['extra'];
  90. $adsense = [
  91. "open" => $dict['adsense']['status'], // Google AdSense Auto AD 开关 默认关闭 开启需要在后台中添加 AdSense代码
  92. "script" => array_key_exists('script', $adsense_script) ? $adsense_script['script'] : ""
  93. ];
  94. $counter_script = $dict['counter']['extra'];
  95. $counter = [
  96. "open" => $dict['counter']['status'], // 统计工具(例如百度统计、StatCounter等) 开关 默认关闭 开启需要在后台中添加相关代码
  97. "script" => array_key_exists('script', $counter_script) ? $counter_script['script'] : ""
  98. ];
  99. $this->app->get('config')->set('vienblog.sidebar', $sidebar);
  100. $this->app->get('config')->set('vienblog.ad', $adsense);
  101. $this->app->get('config')->set('vienblog.counter', $counter);
  102. $this->app->get('config')->set('vienblog.baidu.auto_push', $dict['baidu_autopush']['status']);
  103. // dump($this->app->get('config')->get('vienblog'));
  104. }
  105. } catch (\Exception $e) {
  106. }
  107. }
  108. }