2
0

BannerController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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->orderBy('bid', 'desc')->paginate(20);
  17. // 获取所有主题用于筛选
  18. $themes = WebThemeConfig::all();
  19. return view('admin.banner.index', [
  20. 'list' => $list,
  21. 'theme_key' => $request->theme_key ?? '',
  22. 'themes' => $themes
  23. ]);
  24. }
  25. public function add(Request $request)
  26. {
  27. if ($request->isMethod('post')) {
  28. $validator = Validator::make($request->all(), [
  29. 'img' => 'required|string',
  30. 'alt' => 'nullable|string',
  31. 'link_game' => 'nullable|integer',
  32. 'link_module' => 'nullable|integer',
  33. 'theme_key' => 'nullable|string|max:30',
  34. ]);
  35. if ($validator->fails()) {
  36. return apiReturnFail($validator->errors()->first());
  37. }
  38. $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link_game', 'link_module', 'theme_key']);
  39. Banner::create($data);
  40. return apiReturnSuc();
  41. }
  42. $themes = WebThemeConfig::all();
  43. return view('admin.banner.add', ['themes' => $themes]);
  44. }
  45. public function edit(Request $request, $id)
  46. {
  47. $info = Banner::findOrFail($id);
  48. if ($request->isMethod('post')) {
  49. $validator = Validator::make($request->all(), [
  50. 'img' => 'required|string',
  51. 'alt' => 'nullable|string',
  52. 'link_game' => 'nullable|integer',
  53. 'link_module' => 'nullable|integer',
  54. 'theme_key' => 'nullable|string|max:30',
  55. ]);
  56. if ($validator->fails()) {
  57. return apiReturnFail($validator->errors()->first());
  58. }
  59. $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link_game', 'link_module', 'theme_key']);
  60. $info->update($data);
  61. return apiReturnSuc();
  62. }
  63. $themes = WebThemeConfig::all();
  64. return view('admin.banner.edit', [
  65. 'info' => $info,
  66. 'themes' => $themes
  67. ]);
  68. }
  69. public function delete($id)
  70. {
  71. $info = Banner::findOrFail($id);
  72. $info->delete();
  73. return apiReturnSuc();
  74. }
  75. }