|
|
@@ -0,0 +1,187 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\admin;
|
|
|
+
|
|
|
+use App\Constants\HttpStatus;
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use App\Models\EgameItem;
|
|
|
+use Exception;
|
|
|
+use Illuminate\Http\JsonResponse;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Validation\Rule;
|
|
|
+use Illuminate\Validation\ValidationException;
|
|
|
+
|
|
|
+class Egame extends Controller
|
|
|
+{
|
|
|
+ public function items(): JsonResponse
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ request()->validate([
|
|
|
+ 'item_type' => ['nullable', Rule::in(['platform', 'game'])],
|
|
|
+ 'plat_type' => ['nullable', 'string', 'max:32'],
|
|
|
+ 'game_type' => ['nullable', 'integer', 'min:0', 'max:255'],
|
|
|
+ 'game_code' => ['nullable', 'string', 'max:128'],
|
|
|
+ 'keyword' => ['nullable', 'string', 'max:128'],
|
|
|
+ 'status' => ['nullable', 'integer', Rule::in([0, 1])],
|
|
|
+ 'page' => ['nullable', 'integer', 'min:1'],
|
|
|
+ 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $query = EgameItem::query();
|
|
|
+ foreach (['item_type', 'plat_type', 'game_type', 'game_code', 'status'] as $field) {
|
|
|
+ $value = request()->input($field);
|
|
|
+ if ($value !== null && $value !== '') {
|
|
|
+ $query->where($field, $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $keyword = trim((string)request()->input('keyword', ''));
|
|
|
+ if ($keyword !== '') {
|
|
|
+ $query->where(function ($query) use ($keyword) {
|
|
|
+ $query->where('name', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('plat_type', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('game_code', 'like', "%{$keyword}%");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ $limit = (int)request()->input('limit', 15);
|
|
|
+ $page = (int)request()->input('page', 1);
|
|
|
+ $total = (clone $query)->count();
|
|
|
+ $list = $query->orderByDesc('sort')
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->forPage($page, $limit)
|
|
|
+ ->get();
|
|
|
+ } catch (ValidationException $e) {
|
|
|
+ return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success(['total' => $total, 'data' => $list]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function update(): JsonResponse
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ request()->validate([
|
|
|
+ 'id' => ['nullable', 'integer', 'min:0'],
|
|
|
+ 'item_type' => ['required', Rule::in(['platform', 'game'])],
|
|
|
+ 'plat_type' => ['required', 'string', 'max:32'],
|
|
|
+ 'game_type' => ['required', 'integer', 'min:0', 'max:255'],
|
|
|
+ 'game_code' => ['nullable', 'string', 'max:128'],
|
|
|
+ 'name' => ['nullable', 'string', 'max:128'],
|
|
|
+ 'logo' => ['nullable', 'string', 'max:500'],
|
|
|
+ 'image_circle' => ['nullable', 'string', 'max:500'],
|
|
|
+ 'image_rectangle' => ['nullable', 'string', 'max:500'],
|
|
|
+ 'image_square' => ['nullable', 'string', 'max:500'],
|
|
|
+ 'status' => ['nullable', 'integer', Rule::in([0, 1])],
|
|
|
+ 'sort' => ['nullable', 'integer', 'min:-999999', 'max:999999'],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'item_type' => request()->input('item_type'),
|
|
|
+ 'plat_type' => strtolower(trim((string)request()->input('plat_type'))),
|
|
|
+ 'game_type' => (int)request()->input('game_type'),
|
|
|
+ 'game_code' => trim((string)request()->input('game_code', '')),
|
|
|
+ 'name' => trim((string)request()->input('name', '')),
|
|
|
+ 'logo' => trim((string)request()->input('logo', '')),
|
|
|
+ 'image_circle' => trim((string)request()->input('image_circle', '')),
|
|
|
+ 'image_rectangle' => trim((string)request()->input('image_rectangle', '')),
|
|
|
+ 'image_square' => trim((string)request()->input('image_square', '')),
|
|
|
+ 'status' => (int)request()->input('status', 1),
|
|
|
+ 'sort' => (int)request()->input('sort', 0),
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($data['item_type'] === 'platform') {
|
|
|
+ $data['game_code'] = '';
|
|
|
+ }
|
|
|
+ if ($data['item_type'] === 'game' && $data['game_code'] === '') {
|
|
|
+ throw new Exception('游戏配置必须填写 game_code', HttpStatus::CUSTOM_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ $id = (int)request()->input('id', 0);
|
|
|
+ if ($id > 0) {
|
|
|
+ $item = EgameItem::query()->find($id);
|
|
|
+ if (!$item) {
|
|
|
+ throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
|
|
|
+ }
|
|
|
+ $duplicate = EgameItem::query()
|
|
|
+ ->where('item_type', $data['item_type'])
|
|
|
+ ->where('plat_type', $data['plat_type'])
|
|
|
+ ->where('game_type', $data['game_type'])
|
|
|
+ ->where('game_code', $data['game_code'])
|
|
|
+ ->where('id', '<>', $id)
|
|
|
+ ->exists();
|
|
|
+ if ($duplicate) {
|
|
|
+ throw new Exception('相同平台/类型/游戏代码配置已存在', HttpStatus::CUSTOM_ERROR);
|
|
|
+ }
|
|
|
+ $item->fill($data)->save();
|
|
|
+ } else {
|
|
|
+ EgameItem::query()->updateOrCreate(
|
|
|
+ [
|
|
|
+ 'item_type' => $data['item_type'],
|
|
|
+ 'plat_type' => $data['plat_type'],
|
|
|
+ 'game_type' => $data['game_type'],
|
|
|
+ 'game_code' => $data['game_code'],
|
|
|
+ ],
|
|
|
+ $data
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (ValidationException $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
|
|
|
+ } catch (Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ return $this->error((int)$e->getCode(), $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setStatus(): JsonResponse
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ request()->validate([
|
|
|
+ 'id' => ['required', 'integer', 'min:1'],
|
|
|
+ 'status' => ['required', 'integer', Rule::in([0, 1])],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $item = EgameItem::query()->find((int)request()->input('id'));
|
|
|
+ if (!$item) {
|
|
|
+ throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
|
|
|
+ }
|
|
|
+ $item->status = (int)request()->input('status');
|
|
|
+ $item->save();
|
|
|
+ } catch (ValidationException $e) {
|
|
|
+ return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return $this->error((int)$e->getCode(), $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function delete(): JsonResponse
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ request()->validate([
|
|
|
+ 'id' => ['required', 'integer', 'min:1'],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $item = EgameItem::query()->find((int)request()->input('id'));
|
|
|
+ if (!$item) {
|
|
|
+ throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
|
|
|
+ }
|
|
|
+ $item->delete();
|
|
|
+ } catch (ValidationException $e) {
|
|
|
+ return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return $this->error((int)$e->getCode(), $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success([], '删除成功');
|
|
|
+ }
|
|
|
+}
|