InitServiceProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. //
  25. $information = Information::query()
  26. ->select('site_title', 'site_keywords', 'site_description', 'author_name', 'author_intro', 'author_avatar', 'navigation')
  27. ->first();
  28. if ($information) {
  29. $blog = [
  30. "name" => $information->site_title,
  31. "keywords" => $information->site_keywords,
  32. "description" => $information->site_description,
  33. ];
  34. $this->app->get('config')->set('vienblog.blog', $blog);
  35. $author = [ // 博主
  36. "name" => $information->author_name,
  37. "description" => $information->author_intro,
  38. "avatar" => $information->author_avatar,
  39. ];
  40. $this->app->get('config')->set('vienblog.author', $author);
  41. }
  42. $switches = Switches::query()
  43. ->select('name', 'status', 'extra')
  44. ->get()->all();
  45. if ($switches) {
  46. $dict = array();
  47. foreach ($switches as $switch) {
  48. $extra = json_decode($switch['extra'], true);
  49. $dict[$switch['name']] = array('status' => intval($switch['status']) == 1,
  50. 'extra' => is_null($extra) ? [] : $extra);
  51. }
  52. $sidebar = [
  53. "carousel" => [
  54. "open" => $dict['carousel']['status'], // 是(true)否(false)开启 默认true 开启务必设置banners
  55. "banners" => $dict['carousel']['extra']
  56. ],
  57. "motto" => [ // 座右铭
  58. "open" => $dict['motto']['status'], // 是(true)否(false)开启 默认true,
  59. "title" => $dict['motto']['extra']['title'], // 标题
  60. "content" => $dict['motto']['extra']['content'] // 内容
  61. ],
  62. "tag" => [ // 标签
  63. "open" => $dict['tag']['status'], // 是(true)否(false)开启 默认true,
  64. "title" => $dict['tag']['extra']['title'], // 标题
  65. "count" => intval($dict['tag']['extra']['count']), // 展示数量
  66. ],
  67. "category" => [ // 分类
  68. "open" => $dict['category']['status'], // 是(true)否(false)开启 默认true,
  69. "title" => $dict['category']['extra']['title'], // 标题
  70. "count" => intval($dict['category']['extra']['count']), // 展示数量
  71. ],
  72. "hot" => [ // 最热文章
  73. "open" => $dict['hot']['status'], // 是(true)否(false)开启 默认true,
  74. "title" => $dict['hot']['extra']['title'], // 标题
  75. "count" => intval($dict['hot']['extra']['count']), // 展示数量
  76. ],
  77. "latest" => [ // 最新文章
  78. "open" => $dict['latest']['status'], // 是(true)否(false)开启 默认true,
  79. "title" => $dict['latest']['extra']['title'], // 标题
  80. "count" => intval($dict['latest']['extra']['count']), // 展示数量
  81. ],
  82. "popular" => [ // 刚有人看的文章
  83. "open" => $dict['popular']['status'], // 是(true)否(false)开启 默认true,
  84. "title" => $dict['popular']['extra']['title'], // 标题
  85. "count" => intval($dict['popular']['extra']['count']), // 展示数量
  86. ],
  87. ];
  88. $adsense_script = $dict['adsense']['extra'];
  89. $adsense = [
  90. "open" => $dict['adsense']['status'], // Google AdSense Auto AD 开关 默认关闭 开启需要在后台中添加 AdSense代码
  91. "script" => array_key_exists('script', $adsense_script) ? $adsense_script['script'] : ""
  92. ];
  93. $counter_script = $dict['counter']['extra'];
  94. $counter = [
  95. "open" => $dict['counter']['status'], // 统计工具(例如百度统计、StatCounter等) 开关 默认关闭 开启需要在后台中添加相关代码
  96. "script" => array_key_exists('script', $counter_script) ? $counter_script['script'] : ""
  97. ];
  98. $this->app->get('config')->set('vienblog.sidebar', $sidebar);
  99. $this->app->get('config')->set('vienblog.ad', $adsense);
  100. $this->app->get('config')->set('vienblog.counter', $counter);
  101. $this->app->get('config')->set('vienblog.baidu.auto_push', $dict['baidu_autopush']['status']);
  102. // dump($this->app->get('config')->get('vienblog'));
  103. }
  104. }
  105. }