BannerController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Game\Banner;
  4. use App\Game\WebThemeConfig;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Validator;
  7. class BannerController
  8. {
  9. public function index(Request $request)
  10. {
  11. $query = Banner::query();
  12. // 可以根据主题筛选
  13. if ($request->has('theme_key') && !empty($request->theme_key)) {
  14. $query->where('theme_key', $request->theme_key);
  15. }
  16. $list = $query->orderByRaw('ISNULL(b_order), b_order ASC')
  17. ->orderBy('bid', 'desc')
  18. ->paginate(20);
  19. // 获取所有主题用于筛选
  20. $themes = WebThemeConfig::all();
  21. return view('admin.banner.index', [
  22. 'list' => $list,
  23. 'theme_key' => $request->theme_key ?? '',
  24. 'themes' => $themes
  25. ]);
  26. }
  27. public function add(Request $request)
  28. {
  29. if ($request->isMethod('post')) {
  30. $validator = Validator::make($request->all(), [
  31. 'img' => 'required|string',
  32. 'alt' => 'nullable|string',
  33. 'link_game' => 'nullable|integer',
  34. 'link_module' => 'nullable|integer',
  35. 'theme_key' => 'nullable|string|max:30',
  36. 'link' => 'nullable|string|max:200',
  37. 'b_order' => 'nullable|integer',
  38. 'state' => 'nullable|integer',
  39. ]);
  40. if ($validator->fails()) {
  41. return apiReturnFail($validator->errors()->first());
  42. }
  43. $data = $request->only([
  44. 'img', 'img_pt', 'img_es', 'alt', 'link_game', 'link_module',
  45. 'theme_key', 'link', 'b_order', 'state'
  46. ]);
  47. // 默认值处理
  48. $data['link'] = $data['link'] ?? '';
  49. $data['state'] = isset($data['state']) && $data['state'] !== '' ? (int)$data['state'] : 127;
  50. $data['b_order'] = $data['b_order'] !== '' ? $data['b_order'] : null;
  51. Banner::create($data);
  52. return apiReturnSuc();
  53. }
  54. $themes = WebThemeConfig::all();
  55. return view('admin.banner.add', ['themes' => $themes]);
  56. }
  57. public function edit(Request $request, $id)
  58. {
  59. $info = Banner::findOrFail($id);
  60. if ($request->isMethod('post')) {
  61. $validator = Validator::make($request->all(), [
  62. 'img' => 'required|string',
  63. 'alt' => 'nullable|string',
  64. 'link_game' => 'nullable|integer',
  65. 'link_module' => 'nullable|integer',
  66. 'theme_key' => 'nullable|string|max:30',
  67. 'link' => 'nullable|string|max:200',
  68. 'b_order' => 'nullable|integer',
  69. 'state' => 'nullable|integer',
  70. ]);
  71. if ($validator->fails()) {
  72. return apiReturnFail($validator->errors()->first());
  73. }
  74. $data = $request->only([
  75. 'img', 'img_pt', 'img_es', 'alt', 'link_game', 'link_module',
  76. 'theme_key', 'link', 'b_order', 'state'
  77. ]);
  78. $data['link'] = $data['link'] ?? '';
  79. $data['state'] = isset($data['state']) && $data['state'] !== '' ? (int)$data['state'] : 127;
  80. $data['b_order'] = $data['b_order'] !== '' ? $data['b_order'] : null;
  81. $info->update($data);
  82. return apiReturnSuc();
  83. }
  84. $themes = WebThemeConfig::all();
  85. return view('admin.banner.edit', [
  86. 'info' => $info,
  87. 'themes' => $themes
  88. ]);
  89. }
  90. public function delete($id)
  91. {
  92. $info = Banner::findOrFail($id);
  93. $info->delete();
  94. return apiReturnSuc();
  95. }
  96. /**
  97. * 同步当前 banner(事件)到其他所有主题
  98. * 规则:通过 link 字段识别"事件"
  99. * 其他主题如果没有相同 link 的 banner,则复制过去
  100. */
  101. public function sync($id)
  102. {
  103. $info = Banner::findOrFail($id);
  104. if (empty($info->link)) {
  105. return apiReturnFail('当前 Banner 没有设置 link(事件),无法识别为一个事件进行同步');
  106. }
  107. $themes = WebThemeConfig::all();
  108. $copied = 0;
  109. $skipped = 0;
  110. foreach ($themes as $theme) {
  111. $themeKey = $theme->ThemeKey;
  112. // 跳过当前 banner 所在的主题
  113. if ($themeKey === $info->theme_key) {
  114. continue;
  115. }
  116. // 检查该主题下是否已存在相同 link 的 banner
  117. $exists = Banner::where('theme_key', $themeKey)
  118. ->where('link', $info->link)
  119. ->exists();
  120. if ($exists) {
  121. $skipped++;
  122. continue;
  123. }
  124. // 复制一份到该主题(使用原始属性,避免 accessor 处理过的 img)
  125. $new = $info->replicate();
  126. $new->theme_key = $themeKey;
  127. $new->save();
  128. $copied++;
  129. }
  130. return apiReturnSuc([
  131. 'copied' => $copied,
  132. 'skipped' => $skipped,
  133. ], '', "同步完成:新增 {$copied} 个,跳过 {$skipped} 个已存在");
  134. }
  135. }