Egame.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\EgameItem;
  6. use Exception;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\ValidationException;
  11. class Egame extends Controller
  12. {
  13. public function items(): JsonResponse
  14. {
  15. try {
  16. request()->validate([
  17. 'item_type' => ['nullable', Rule::in(['platform', 'game'])],
  18. 'plat_type' => ['nullable', 'string', 'max:32'],
  19. 'game_type' => ['nullable', 'integer', 'min:0', 'max:255'],
  20. 'game_code' => ['nullable', 'string', 'max:128'],
  21. 'keyword' => ['nullable', 'string', 'max:128'],
  22. 'status' => ['nullable', 'integer', Rule::in([0, 1])],
  23. 'page' => ['nullable', 'integer', 'min:1'],
  24. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  25. ]);
  26. $query = EgameItem::query();
  27. foreach (['item_type', 'plat_type', 'game_type', 'game_code', 'status'] as $field) {
  28. $value = request()->input($field);
  29. if ($value !== null && $value !== '') {
  30. $query->where($field, $value);
  31. }
  32. }
  33. $keyword = trim((string)request()->input('keyword', ''));
  34. if ($keyword !== '') {
  35. $query->where(function ($query) use ($keyword) {
  36. $query->where('name', 'like', "%{$keyword}%")
  37. ->orWhere('plat_type', 'like', "%{$keyword}%")
  38. ->orWhere('game_code', 'like', "%{$keyword}%");
  39. });
  40. }
  41. $limit = (int)request()->input('limit', 15);
  42. $page = (int)request()->input('page', 1);
  43. $total = (clone $query)->count();
  44. $list = $query->orderByDesc('sort')
  45. ->orderByDesc('id')
  46. ->forPage($page, $limit)
  47. ->get();
  48. } catch (ValidationException $e) {
  49. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  50. } catch (Exception $e) {
  51. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  52. }
  53. return $this->success(['total' => $total, 'data' => $list]);
  54. }
  55. public function update(): JsonResponse
  56. {
  57. DB::beginTransaction();
  58. try {
  59. request()->validate([
  60. 'id' => ['nullable', 'integer', 'min:0'],
  61. 'item_type' => ['required', Rule::in(['platform', 'game'])],
  62. 'plat_type' => ['required', 'string', 'max:32'],
  63. 'game_type' => ['required', 'integer', 'min:0', 'max:255'],
  64. 'game_code' => ['nullable', 'string', 'max:128'],
  65. 'name' => ['nullable', 'string', 'max:128'],
  66. 'logo' => ['nullable', 'string', 'max:500'],
  67. 'image_circle' => ['nullable', 'string', 'max:500'],
  68. 'image_rectangle' => ['nullable', 'string', 'max:500'],
  69. 'image_square' => ['nullable', 'string', 'max:500'],
  70. 'status' => ['nullable', 'integer', Rule::in([0, 1])],
  71. 'sort' => ['nullable', 'integer', 'min:-999999', 'max:999999'],
  72. ]);
  73. $data = [
  74. 'item_type' => request()->input('item_type'),
  75. 'plat_type' => strtolower(trim((string)request()->input('plat_type'))),
  76. 'game_type' => (int)request()->input('game_type'),
  77. 'game_code' => trim((string)request()->input('game_code', '')),
  78. 'name' => trim((string)request()->input('name', '')),
  79. 'logo' => trim((string)request()->input('logo', '')),
  80. 'image_circle' => trim((string)request()->input('image_circle', '')),
  81. 'image_rectangle' => trim((string)request()->input('image_rectangle', '')),
  82. 'image_square' => trim((string)request()->input('image_square', '')),
  83. 'status' => (int)request()->input('status', 1),
  84. 'sort' => (int)request()->input('sort', 0),
  85. ];
  86. if ($data['item_type'] === 'platform') {
  87. $data['game_code'] = '';
  88. }
  89. if ($data['item_type'] === 'game' && $data['game_code'] === '') {
  90. throw new Exception('游戏配置必须填写 game_code', HttpStatus::CUSTOM_ERROR);
  91. }
  92. $id = (int)request()->input('id', 0);
  93. if ($id > 0) {
  94. $item = EgameItem::query()->find($id);
  95. if (!$item) {
  96. throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
  97. }
  98. $duplicate = EgameItem::query()
  99. ->where('item_type', $data['item_type'])
  100. ->where('plat_type', $data['plat_type'])
  101. ->where('game_type', $data['game_type'])
  102. ->where('game_code', $data['game_code'])
  103. ->where('id', '<>', $id)
  104. ->exists();
  105. if ($duplicate) {
  106. throw new Exception('相同平台/类型/游戏代码配置已存在', HttpStatus::CUSTOM_ERROR);
  107. }
  108. $item->fill($data)->save();
  109. } else {
  110. EgameItem::query()->updateOrCreate(
  111. [
  112. 'item_type' => $data['item_type'],
  113. 'plat_type' => $data['plat_type'],
  114. 'game_type' => $data['game_type'],
  115. 'game_code' => $data['game_code'],
  116. ],
  117. $data
  118. );
  119. }
  120. DB::commit();
  121. } catch (ValidationException $e) {
  122. DB::rollBack();
  123. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  124. } catch (Exception $e) {
  125. DB::rollBack();
  126. return $this->error((int)$e->getCode(), $e->getMessage());
  127. }
  128. return $this->success();
  129. }
  130. public function setStatus(): JsonResponse
  131. {
  132. try {
  133. request()->validate([
  134. 'id' => ['required', 'integer', 'min:1'],
  135. 'status' => ['required', 'integer', Rule::in([0, 1])],
  136. ]);
  137. $item = EgameItem::query()->find((int)request()->input('id'));
  138. if (!$item) {
  139. throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
  140. }
  141. $item->status = (int)request()->input('status');
  142. $item->save();
  143. } catch (ValidationException $e) {
  144. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  145. } catch (Exception $e) {
  146. return $this->error((int)$e->getCode(), $e->getMessage());
  147. }
  148. return $this->success();
  149. }
  150. public function delete(): JsonResponse
  151. {
  152. try {
  153. request()->validate([
  154. 'id' => ['required', 'integer', 'min:1'],
  155. ]);
  156. $item = EgameItem::query()->find((int)request()->input('id'));
  157. if (!$item) {
  158. throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
  159. }
  160. $item->delete();
  161. } catch (ValidationException $e) {
  162. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  163. } catch (Exception $e) {
  164. return $this->error((int)$e->getCode(), $e->getMessage());
  165. }
  166. return $this->success([], '删除成功');
  167. }
  168. }