SwitchController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Http\Controllers\Admin\Index;
  3. use App\Models\Blog\Article;
  4. use App\Models\Blog\ArticleTag;
  5. use App\Models\Blog\Category;
  6. use App\Models\Blog\Tag;
  7. use App\Models\Index\Information;
  8. use App\Models\Index\Link;
  9. use App\Models\Index\Switches;
  10. use Illuminate\Http\Request;
  11. use App\Http\Controllers\Controller;
  12. use Illuminate\Support\Facades\Auth;
  13. class SwitchController extends Controller
  14. {
  15. //
  16. public function edit(Request $request)
  17. {
  18. $switches = self::switches();
  19. return view('admin.switch.edit', ['switches' => $switches]);
  20. }
  21. public function update(Request $request)
  22. {
  23. $input = $request->input();
  24. // $this->validate($request, [
  25. // "carousel-switch" => "required",
  26. // "carousel-image-1" => "required",
  27. // "carousel-url-1" => "required",
  28. // "carousel-description-1" => "required",
  29. // "carousel-image-2" => "required",
  30. // "carousel-url-2" => "required",
  31. // "carousel-description-2" => "required",
  32. // "carousel-image-3" => "required",
  33. // "carousel-url-3" => "required",
  34. // "carousel-description-3" => "required",
  35. // "motto-switch" => "required",
  36. // "motto-title" => "required",
  37. // "motto-content" => "required",
  38. // "tag-switch" => "required",
  39. // "tag-title" => "required",
  40. // "tag-count" => "required",
  41. // "category-switch" => "required",
  42. // "category-title" => "required",
  43. // "category-count" => "required",
  44. // "hot-switch" => "required",
  45. // "hot-title" => "required",
  46. // "hot-count" => "required",
  47. // "latest-switch" => "required",
  48. // "latest-title" => "required",
  49. // "latest-count" => "required",
  50. // "popular-switch" => "required",
  51. // "popular-title" => "required",
  52. // "popular-count" => "required",
  53. // "adsense-switch" => "required",
  54. // "adsense-script" => "required",
  55. // "counter-switch" => "required",
  56. // "counter-script" => "required"
  57. // ]);
  58. $baidu_autopushswitch = isset($input["baidu_autopush-switch"]) ? 1 : 0;
  59. $carouselswitch = isset($input["carousel-switch"]) ? 1 : 0;
  60. $carouselimage1 = $input["carousel-image-1"];
  61. $carouselurl1 = $input["carousel-url-1"];
  62. $carouseldescription1 = $input["carousel-description-1"];
  63. $carouselimage2 = $input["carousel-image-2"];
  64. $carouselurl2 = $input["carousel-url-2"];
  65. $carouseldescription2 = $input["carousel-description-2"];
  66. $carouselimage3 = $input["carousel-image-3"];
  67. $carouselurl3 = $input["carousel-url-3"];
  68. $carouseldescription3 = $input["carousel-description-3"];
  69. $mottoswitch = isset($input["motto-switch"]) ? 1 : 0;
  70. $mottotitle = $input["motto-title"];
  71. $mottocontent = $input["motto-content"];
  72. $tagswitch = isset($input["tag-switch"]) ? 1 : 0;
  73. $tagtitle = $input["tag-title"];
  74. $tagcount = $input["tag-count"];
  75. $categoryswitch = isset($input["category-switch"]) ? 1 : 0;
  76. $categorytitle = $input["category-title"];
  77. $categorycount = $input["category-count"];
  78. $hotswitch = isset($input["hot-switch"]) ? 1 : 0;
  79. $hottitle = $input["hot-title"];
  80. $hotcount = $input["hot-count"];
  81. $latestswitch = isset($input["latest-switch"]) ? 1 : 0;
  82. $latesttitle = $input["latest-title"];
  83. $latestcount = $input["latest-count"];
  84. $popularswitch = isset($input["popular-switch"]) ? 1 : 0;
  85. $populartitle = $input["popular-title"];
  86. $popularcount = $input["popular-count"];
  87. $adsenseswitch = isset($input["adsense-switch"]) ? 1 : 0;
  88. $adsensescript = isset($input["adsense-script"]) ? $input["adsense-script"] : "";
  89. $counterswitch = isset($input["counter-switch"]) ? 1 : 0;
  90. $counterscript = isset($input["counter-script"]) ? $input["counter-script"] : "";
  91. $adsense = Switches::query()->where("name", "=", "adsense")->first();
  92. $adsense->status = $adsenseswitch;
  93. $adsense->extra = json_encode(["script" => $adsensescript], JSON_UNESCAPED_UNICODE);
  94. $adsense->save();
  95. $counter = Switches::query()->where("name", "=", "counter")->first();
  96. $counter->status = $counterswitch;
  97. $counter->extra = json_encode(["script" => $counterscript], JSON_UNESCAPED_UNICODE);
  98. $counter->save();
  99. $motto = Switches::query()->where("name", "=", "motto")->first();
  100. $motto->status = $mottoswitch;
  101. $motto->extra = json_encode(["title" => $mottotitle, "content" => $mottocontent], JSON_UNESCAPED_UNICODE);
  102. $motto->save();
  103. $tag = Switches::query()->where("name", "=", "tag")->first();
  104. $tag->status = $tagswitch;
  105. $tag->extra = json_encode(["title" => $tagtitle, "count" => $tagcount], JSON_UNESCAPED_UNICODE);
  106. $tag->save();
  107. $category = Switches::query()->where("name", "=", "category")->first();
  108. $category->status = $categoryswitch;
  109. $category->extra = json_encode(["title" => $categorytitle, "count" => $categorycount], JSON_UNESCAPED_UNICODE);
  110. $category->save();
  111. $hot = Switches::query()->where("name", "=", "hot")->first();
  112. $hot->status = $hotswitch;
  113. $hot->extra = json_encode(["title" => $hottitle, "count" => $hotcount], JSON_UNESCAPED_UNICODE);
  114. $hot->save();
  115. $latest = Switches::query()->where("name", "=", "latest")->first();
  116. $latest->status = $latestswitch;
  117. $latest->extra = json_encode(["title" => $latesttitle, "count" => $latestcount], JSON_UNESCAPED_UNICODE);
  118. $latest->save();
  119. $popular = Switches::query()->where("name", "=", "popular")->first();
  120. $popular->status = $popularswitch;
  121. $popular->extra = json_encode(["title" => $populartitle, "count" => $popularcount], JSON_UNESCAPED_UNICODE);
  122. $popular->save();
  123. $carousel = Switches::query()->where("name", "=", "carousel")->first();
  124. $carousel->status = $carouselswitch;
  125. $carousel->extra = json_encode([
  126. ["image" => $carouselimage1, "url" => $carouselurl1, "description" => $carouseldescription1],
  127. ["image" => $carouselimage2, "url" => $carouselurl2, "description" => $carouseldescription2],
  128. ["image" => $carouselimage3, "url" => $carouselurl3, "description" => $carouseldescription3]
  129. ], JSON_UNESCAPED_UNICODE);
  130. $carousel->save();
  131. $baidu_autopush = Switches::query()->where("name", "=", "baidu_autopush")->first();
  132. $baidu_autopush->status = $baidu_autopushswitch;
  133. $baidu_autopush->save();
  134. $switches = self::switches();
  135. return view('admin.switch.edit', ['switches' => $switches])->with(['message' => 'success']);
  136. }
  137. private function switches() {
  138. $switches = Switches::query()
  139. ->select( 'id', 'name', 'status', 'extra')
  140. ->get()->toArray();
  141. $dict = array();
  142. foreach ($switches as $switch) {
  143. $extra = json_decode($switch['extra'], true);
  144. $dict[$switch['name']] = array('status' => intval($switch['status']) == 1,
  145. 'extra' => is_null($extra) ? [] : $extra);
  146. }
  147. $sidebar = [
  148. "carousel" => [
  149. "open" => $dict['carousel']['status'], // 是(true)否(false)开启 默认true 开启务必设置banners
  150. "banners" => $dict['carousel']['extra']
  151. ],
  152. "motto" => [ // 座右铭
  153. "open" => $dict['motto']['status'], // 是(true)否(false)开启 默认true,
  154. "title" => $dict['motto']['extra']['title'], // 标题
  155. "content" => $dict['motto']['extra']['content'] // 内容
  156. ],
  157. "tag" => [ // 标签
  158. "open" => $dict['tag']['status'], // 是(true)否(false)开启 默认true,
  159. "title" => $dict['tag']['extra']['title'], // 标题
  160. "count" => intval($dict['tag']['extra']['count']), // 展示数量
  161. ],
  162. "category" => [ // 分类
  163. "open" => $dict['category']['status'], // 是(true)否(false)开启 默认true,
  164. "title" => $dict['category']['extra']['title'], // 标题
  165. "count" => intval($dict['category']['extra']['count']), // 展示数量
  166. ],
  167. "hot" => [ // 最热文章
  168. "open" => $dict['hot']['status'], // 是(true)否(false)开启 默认true,
  169. "title" => $dict['hot']['extra']['title'], // 标题
  170. "count" => intval($dict['hot']['extra']['count']), // 展示数量
  171. ],
  172. "latest" => [ // 最新文章
  173. "open" => $dict['latest']['status'], // 是(true)否(false)开启 默认true,
  174. "title" => $dict['latest']['extra']['title'], // 标题
  175. "count" => intval($dict['latest']['extra']['count']), // 展示数量
  176. ],
  177. "popular" => [ // 刚有人看的文章
  178. "open" => $dict['popular']['status'], // 是(true)否(false)开启 默认true,
  179. "title" => $dict['popular']['extra']['title'], // 标题
  180. "count" => intval($dict['popular']['extra']['count']), // 展示数量
  181. ],
  182. ];
  183. $adsense_script = $dict['adsense']['extra'];
  184. $adsense = [
  185. "open" => $dict['adsense']['status'], // Google AdSense Auto AD 开关 默认关闭 开启需要在后台中添加 AdSense代码
  186. "script" => array_key_exists('script', $adsense_script) ? $adsense_script['script'] : ""
  187. ];
  188. $counter_script = $dict['counter']['extra'];
  189. $counter = [
  190. "open" => $dict['counter']['status'], // 统计工具(例如百度统计、StatCounter等) 开关 默认关闭 开启需要在后台中添加相关代码
  191. "script" => array_key_exists('script', $counter_script) ? $counter_script['script'] : ""
  192. ];
  193. $baidu_autopush = $dict['baidu_autopush']['status'];
  194. $switches = ['sidebar' => $sidebar, 'adsense' => $adsense, 'counter' => $counter, 'baidu_autopush' => $baidu_autopush];
  195. return $switches;
  196. }
  197. }