InitServiceProvider.php 6.2 KB

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