ActivityReward.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. class ActivityReward extends Controller
  12. {
  13. public function index(): JsonResponse
  14. {
  15. try {
  16. $params = request()->validate([
  17. 'page' => ['required', 'integer', 'min:1'],
  18. 'limit' => ['required', 'integer', 'min:1'],
  19. 'title' => ['nullable', 'string'],
  20. 'type' => ['nullable', 'string'],
  21. 'platform' => ['nullable', 'string', 'in:pc,h5'],
  22. 'status' => ['nullable', 'integer', 'in:0,1'],
  23. 'lang' => ['nullable', 'string', 'max:10'],
  24. ]);
  25. $page = request()->input('page', 1);
  26. $limit = request()->input('limit', 10);
  27. $where = ActivityRewardService::getWhere(array_intersect_key($params, ['title' => true, 'type' => true]));
  28. $query = ActivityRewardModel::where($where);
  29. if (isset($params['status']) && $params['status'] !== '') {
  30. $query->where('status', (int)$params['status']);
  31. }
  32. if (isset($params['lang']) && $params['lang'] !== '') {
  33. $query->where('lang', $params['lang']);
  34. }
  35. if (isset($params['platform']) && $params['platform'] !== '') {
  36. $platform = strtoupper($params['platform']);
  37. $query->where(function ($platformQuery) use ($platform) {
  38. $platformQuery->where('type', 2)
  39. ->orWhereNull('publish_platforms')
  40. ->orWhereJsonLength('publish_platforms', 0);
  41. if ($platformQuery->getModel()->getConnection()->getDriverName() === 'sqlite') {
  42. $platformQuery->orWhereRaw('EXISTS (SELECT 1 FROM json_each(publish_platforms) WHERE json_each.value = ?)', [$platform]);
  43. } else {
  44. $platformQuery->orWhereJsonContains('publish_platforms', $platform);
  45. }
  46. });
  47. }
  48. $count = $query->count();
  49. $list = $query->orderByDesc('sort')->orderByDesc('id')
  50. ->forPage($page, $limit)->get()->toArray();
  51. foreach ($list as &$item) {
  52. if (!empty($item['params'])) {
  53. $item['params'] = json_decode($item['params'], true);
  54. }
  55. }
  56. $result = ['total' => $count, 'data' => $list];
  57. } catch (ValidationException $e) {
  58. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  59. } catch (Exception $e) {
  60. return $this->error($e->getCode(), $e->getMessage());
  61. }
  62. return $this->success($result);
  63. }
  64. public function store(): JsonResponse
  65. {
  66. DB::beginTransaction();
  67. try {
  68. $params = request()->validate([
  69. 'id' => ['nullable', 'integer', 'min:1'],
  70. 'title' => ['required', 'string', 'min:1', 'max:140'],
  71. 'sub_title' => ['required', 'string', 'min:1', 'max:140'],
  72. 'start_time' => ['required', 'date'],
  73. 'end_time' => ['required', 'date', 'after_or_equal:start_time'],
  74. 'detail_image' => ['required'],
  75. 'pc_image' => ['required'],
  76. 'detail_banner' => ['required'],
  77. 'pc_banner' => ['required'],
  78. 'status' => ['required', 'integer', 'min:0', 'max:1'],
  79. 'type' => ['required', 'integer', 'min:1', 'max:6'],
  80. 'lang' => ['required', 'string'],
  81. 'content' => ['nullable', 'string'],
  82. 'params' => ['nullable', 'array'],
  83. ]);
  84. $params = array_merge($params, array_intersect_key(request()->all(), ActivityRewardService::configurationDefaults()));
  85. // Wrong-mode inputs are rejected rather than silently disabling a limit.
  86. $params = array_merge($params, array_intersect_key(request()->all(), ['budget' => true, 'budget_used' => true]));
  87. $params['params'] = empty($params['params']) ? null : json_encode($params['params']);
  88. ActivityRewardService::submit($params);
  89. DB::commit();
  90. } catch (ValidationException $e) {
  91. DB::rollBack();
  92. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  93. } catch (Exception $e) {
  94. DB::rollBack();
  95. return $this->error($e->getCode(), $e->getMessage());
  96. }
  97. return $this->success();
  98. }
  99. public function destroy(): JsonResponse
  100. {
  101. try {
  102. request()->validate([
  103. 'id' => ['required', 'integer', 'min:1'],
  104. ]);
  105. $id = request()->input('id');
  106. ActivityRewardService::deleteAll(['id' => $id]);
  107. DB::commit();
  108. } catch (ValidationException $e) {
  109. DB::rollBack();
  110. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  111. } catch (Exception $e) {
  112. DB::rollBack();
  113. return $this->error($e->getCode(), $e->getMessage());
  114. }
  115. return $this->success([], '删除成功');
  116. }
  117. }