ActivityReward.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\ActivityRewardService;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. use App\Models\ActivityReward as ActivityRewardModel;
  11. use App\Support\ActivityCatalog;
  12. use Illuminate\Validation\Rule;
  13. class ActivityReward extends Controller
  14. {
  15. public function options(): JsonResponse
  16. {
  17. return $this->success(ActivityCatalog::options());
  18. }
  19. public function index(): JsonResponse
  20. {
  21. try {
  22. $params = request()->validate([
  23. 'page' => ['required', 'integer', 'min:1'],
  24. 'limit' => ['required', 'integer', 'min:1'],
  25. 'title' => ['nullable', 'string'],
  26. 'type' => ['nullable', 'string'],
  27. 'platform' => ['nullable', 'string', 'in:pc,h5'],
  28. 'status' => ['nullable', 'integer', 'in:0,1'],
  29. 'lang' => ['nullable', 'string', 'max:10'],
  30. 'game_category' => ['nullable', 'string', Rule::in(array_keys(ActivityCatalog::gameTypes()))],
  31. ]);
  32. $page = request()->input('page', 1);
  33. $limit = request()->input('limit', 10);
  34. // 同步真实状态后再统计和分页,避免调度延迟时仍显示为上架。
  35. ActivityRewardService::expire();
  36. $where = ActivityRewardService::getWhere(array_intersect_key($params, ['title' => true, 'type' => true]));
  37. $query = ActivityRewardModel::where($where);
  38. if (isset($params['status']) && $params['status'] !== '') {
  39. $query->where('status', (int)$params['status']);
  40. }
  41. if (isset($params['lang']) && $params['lang'] !== '') {
  42. $query->where('lang', $params['lang']);
  43. }
  44. if (isset($params['game_category']) && $params['game_category'] !== '') {
  45. $query->where('game_category', $params['game_category']);
  46. }
  47. if (isset($params['platform']) && $params['platform'] !== '') {
  48. $platform = strtoupper($params['platform']);
  49. $query->where(function ($platformQuery) use ($platform) {
  50. $platformQuery->whereNull('publish_platforms')
  51. ->orWhereJsonLength('publish_platforms', 0);
  52. if ($platformQuery->getModel()->getConnection()->getDriverName() === 'sqlite') {
  53. $platformQuery->orWhereRaw('EXISTS (SELECT 1 FROM json_each(publish_platforms) WHERE json_each.value = ?)', [$platform]);
  54. } else {
  55. $platformQuery->orWhereJsonContains('publish_platforms', $platform);
  56. }
  57. });
  58. }
  59. $count = $query->count();
  60. $list = $query->orderByDesc('sort')->orderByDesc('id')
  61. ->forPage($page, $limit)->get()->toArray();
  62. foreach ($list as &$item) {
  63. if (!empty($item['params'])) {
  64. $item['params'] = json_decode($item['params'], true);
  65. }
  66. }
  67. $result = ['total' => $count, 'data' => $list];
  68. } catch (ValidationException $e) {
  69. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  70. } catch (Exception $e) {
  71. return $this->error($e->getCode(), $e->getMessage());
  72. }
  73. return $this->success($result);
  74. }
  75. public function store(): JsonResponse
  76. {
  77. DB::beginTransaction();
  78. try {
  79. $params = request()->validate([
  80. 'id' => ['nullable', 'integer', 'min:1'],
  81. 'title' => ['required', 'string', 'min:1', 'max:140'],
  82. 'sub_title' => ['required', 'string', 'min:1', 'max:140'],
  83. 'start_time' => ['nullable', 'required_with:end_time', 'date'],
  84. 'end_time' => ['nullable', 'required_with:start_time', 'date', 'after_or_equal:start_time'],
  85. 'detail_image' => ['required'],
  86. 'pc_image' => ['required'],
  87. 'status' => ['required', 'integer', 'min:0', 'max:1'],
  88. 'type' => ['required', 'integer', 'min:1', 'max:6'],
  89. 'lang' => ['required', 'string'],
  90. 'content' => ['nullable', 'string'],
  91. 'params' => ['nullable', 'array'],
  92. 'game_category' => ['nullable', 'string', Rule::in(array_keys(ActivityCatalog::gameTypes()))],
  93. ], [
  94. 'start_time.required_with' => '活动开始和结束时间必须同时填写或同时留空',
  95. 'end_time.required_with' => '活动开始和结束时间必须同时填写或同时留空',
  96. ]);
  97. $params = array_merge($params, array_intersect_key(request()->all(), ActivityRewardService::configurationDefaults()));
  98. $params = array_merge($params, array_intersect_key(request()->all(), ['click_type' => true, 'link' => true, 'link_pc' => true, 'link_h5' => true, 'click_position' => true]));
  99. // Wrong-mode inputs are rejected rather than silently disabling a limit.
  100. $params = array_merge($params, array_intersect_key(request()->all(), ['budget' => true, 'budget_used' => true]));
  101. $params['params'] = empty($params['params']) ? null : json_encode($params['params']);
  102. ActivityRewardService::submit($params);
  103. DB::commit();
  104. } catch (ValidationException $e) {
  105. DB::rollBack();
  106. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  107. } catch (Exception $e) {
  108. DB::rollBack();
  109. return $this->error($e->getCode(), $e->getMessage());
  110. }
  111. return $this->success();
  112. }
  113. public function destroy(): JsonResponse
  114. {
  115. try {
  116. request()->validate([
  117. 'id' => ['required', 'integer', 'min:1'],
  118. ]);
  119. $id = request()->input('id');
  120. ActivityRewardService::deleteAll(['id' => $id]);
  121. DB::commit();
  122. } catch (ValidationException $e) {
  123. DB::rollBack();
  124. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  125. } catch (Exception $e) {
  126. DB::rollBack();
  127. return $this->error($e->getCode(), $e->getMessage());
  128. }
  129. return $this->success([], '删除成功');
  130. }
  131. }