| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Game\Banner;
- use App\Game\WebThemeConfig;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- class BannerController
- {
- public function index(Request $request)
- {
- $query = Banner::query();
- // 可以根据主题筛选
- if ($request->has('theme_key') && !empty($request->theme_key)) {
- $query->where('theme_key', $request->theme_key);
- }
- $list = $query->orderBy('bid', 'desc')->paginate(20);
- // 获取所有主题用于筛选
- $themes = WebThemeConfig::all();
- return view('admin.banner.index', [
- 'list' => $list,
- 'theme_key' => $request->theme_key ?? '',
- 'themes' => $themes
- ]);
- }
- public function add(Request $request)
- {
- if ($request->isMethod('post')) {
- $validator = Validator::make($request->all(), [
- 'img' => 'required|string',
- 'alt' => 'nullable|string',
- 'link' => 'nullable|string|max:255',
- 'state' => 'nullable|integer',
- 'link_game' => 'nullable|integer',
- 'link_module' => 'nullable|integer',
- 'theme_key' => 'nullable|string|max:30',
- ]);
- if ($validator->fails()) {
- return apiReturnFail($validator->errors()->first());
- }
- $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link', 'state', 'link_game', 'link_module', 'theme_key']);
- // state: 127 启用, 0 禁用
- $state = isset($data['state']) && $data['state'] !== '' ? intval($data['state']) : 127;
- $data['state'] = in_array($state, [0, 127]) ? $state : 127;
- Banner::create($data);
- return apiReturnSuc();
- }
- $themes = WebThemeConfig::all();
- return view('admin.banner.add', ['themes' => $themes]);
- }
- public function edit(Request $request, $id)
- {
- $info = Banner::findOrFail($id);
- if ($request->isMethod('post')) {
- $validator = Validator::make($request->all(), [
- 'img' => 'required|string',
- 'alt' => 'nullable|string',
- 'link' => 'nullable|string|max:255',
- 'state' => 'nullable|integer',
- 'link_game' => 'nullable|integer',
- 'link_module' => 'nullable|integer',
- 'theme_key' => 'nullable|string|max:30',
- ]);
- if ($validator->fails()) {
- return apiReturnFail($validator->errors()->first());
- }
- $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link', 'state', 'link_game', 'link_module', 'theme_key']);
- // state: 127 启用, 0 禁用
- $state = isset($data['state']) && $data['state'] !== '' ? intval($data['state']) : 127;
- $data['state'] = in_array($state, [0, 127]) ? $state : 127;
- $info->update($data);
- return apiReturnSuc();
- }
- $themes = WebThemeConfig::all();
- return view('admin.banner.edit', [
- 'info' => $info,
- 'themes' => $themes
- ]);
- }
- public function delete($id)
- {
- $info = Banner::findOrFail($id);
- $info->delete();
- return apiReturnSuc();
- }
- }
|