BannerController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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' => 'nullable|string|max:255',
  32. 'state' => 'nullable|integer',
  33. 'link_game' => 'nullable|integer',
  34. 'link_module' => 'nullable|integer',
  35. 'theme_key' => 'nullable|string|max:30',
  36. ]);
  37. if ($validator->fails()) {
  38. return apiReturnFail($validator->errors()->first());
  39. }
  40. $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link', 'state', 'link_game', 'link_module', 'theme_key']);
  41. // state: 127 启用, 0 禁用
  42. $state = isset($data['state']) && $data['state'] !== '' ? intval($data['state']) : 127;
  43. $data['state'] = in_array($state, [0, 127]) ? $state : 127;
  44. Banner::create($data);
  45. return apiReturnSuc();
  46. }
  47. $themes = WebThemeConfig::all();
  48. return view('admin.banner.add', ['themes' => $themes]);
  49. }
  50. public function edit(Request $request, $id)
  51. {
  52. $info = Banner::findOrFail($id);
  53. if ($request->isMethod('post')) {
  54. $validator = Validator::make($request->all(), [
  55. 'img' => 'required|string',
  56. 'alt' => 'nullable|string',
  57. 'link' => 'nullable|string|max:255',
  58. 'state' => 'nullable|integer',
  59. 'link_game' => 'nullable|integer',
  60. 'link_module' => 'nullable|integer',
  61. 'theme_key' => 'nullable|string|max:30',
  62. ]);
  63. if ($validator->fails()) {
  64. return apiReturnFail($validator->errors()->first());
  65. }
  66. $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link', 'state', 'link_game', 'link_module', 'theme_key']);
  67. // state: 127 启用, 0 禁用
  68. $state = isset($data['state']) && $data['state'] !== '' ? intval($data['state']) : 127;
  69. $data['state'] = in_array($state, [0, 127]) ? $state : 127;
  70. $info->update($data);
  71. return apiReturnSuc();
  72. }
  73. $themes = WebThemeConfig::all();
  74. return view('admin.banner.edit', [
  75. 'info' => $info,
  76. 'themes' => $themes
  77. ]);
  78. }
  79. public function delete($id)
  80. {
  81. $info = Banner::findOrFail($id);
  82. $info->delete();
  83. return apiReturnSuc();
  84. }
  85. }