ActivityReward.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. ]);
  22. $page = request()->input('page', 1);
  23. $limit = request()->input('limit', 10);
  24. $where = ActivityRewardService::getWhere($params);
  25. $query = ActivityRewardModel::where($where);
  26. $count = $query->count();
  27. $list = $query->orderByDesc('id')
  28. ->forPage($page, $limit)->get()->toArray();
  29. foreach ($list as &$item) {
  30. if (!empty($item['params'])) {
  31. $item['params'] = json_decode($item['params'], true);
  32. }
  33. }
  34. $result = ['total' => $count, 'data' => $list];
  35. } catch (ValidationException $e) {
  36. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  37. } catch (Exception $e) {
  38. return $this->error($e->getCode(), $e->getMessage());
  39. }
  40. return $this->success($result);
  41. }
  42. public function store(): JsonResponse
  43. {
  44. DB::beginTransaction();
  45. try {
  46. $params = request()->validate([
  47. 'id' => ['nullable', 'integer', 'min:1'],
  48. 'title' => ['required', 'string', 'min:1', 'max:140'],
  49. 'sub_title' => ['required', 'string', 'min:1', 'max:140'],
  50. 'start_time' => ['required', 'date', 'date_format:Y-m-d'],
  51. 'end_time' => ['required', 'date', 'date_format:Y-m-d', 'after_or_equal:start_time'],
  52. 'detail_image' => ['required', 'url', 'regex:/\.(jpeg|jpg|png|webp)$/i'],
  53. 'pc_image' => ['required', 'url', 'regex:/\.(jpeg|jpg|png|webp)$/i'],
  54. 'status' => ['required', 'integer', 'min:0', 'max:1'],
  55. 'type' => ['required'],
  56. 'lang' => ['required', 'string'],
  57. 'content' => ['nullable', 'string'],
  58. 'params' => ['nullable', 'array'],
  59. ]);
  60. $params['params'] = empty($params['params']) ? null : json_encode($params['params']);
  61. ActivityRewardService::submit($params);
  62. DB::commit();
  63. } catch (ValidationException $e) {
  64. DB::rollBack();
  65. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  66. } catch (Exception $e) {
  67. DB::rollBack();
  68. return $this->error($e->getCode(), $e->getMessage());
  69. }
  70. return $this->success();
  71. }
  72. public function destroy(): JsonResponse
  73. {
  74. try {
  75. request()->validate([
  76. 'id' => ['required', 'integer', 'min:1'],
  77. ]);
  78. $id = request()->input('id');
  79. ActivityRewardService::deleteAll(['id' => $id]);
  80. DB::commit();
  81. } catch (ValidationException $e) {
  82. DB::rollBack();
  83. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  84. } catch (Exception $e) {
  85. DB::rollBack();
  86. return $this->error($e->getCode(), $e->getMessage());
  87. }
  88. return $this->success([], '删除成功');
  89. }
  90. }