BannerController.php 3.1 KB

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