validate([ 'page' => ['required', 'integer', 'min:1'], 'limit' => ['required', 'integer', 'min:1'], 'title' => ['nullable', 'string'], ]); $page = request()->input('page', 1); $limit = request()->input('limit', 10); $where = ActivityRewardService::getWhere($params); $query = ActivityRewardModel::where($where); $count = $query->count(); $list = $query->orderByDesc('id') ->forPage($page, $limit)->get()->toArray(); $result = ['total' => $count, 'data' => $list]; } catch (ValidationException $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); } catch (Exception $e) { return $this->error($e->getCode(), $e->getMessage()); } return $this->success($result); } public function store(): JsonResponse { DB::beginTransaction(); try { $params = request()->validate([ 'id' => ['nullable', 'integer', 'min:1'], 'title' => ['required', 'string', 'min:1', 'max:140'], 'sub_title' => ['required', 'string', 'min:1', 'max:140'], 'start_time' => ['required', 'date', 'date_format:Y-m-d'], 'end_time' => ['required', 'date', 'date_format:Y-m-d', 'after_or_equal:start_time'], 'detail_image' => ['required', 'url', 'regex:/\.(jpeg|jpg|png|webp)$/i'], 'status' => ['required', 'integer', 'min:0', 'max:1'], ]); ActivityRewardService::submit($params); DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error($e->getCode(), $e->getMessage()); } return $this->success(); } public function destroy(): JsonResponse { try { request()->validate([ 'id' => ['required', 'integer', 'min:1'], ]); $id = request()->input('id'); ActivityRewardService::deleteAll(['id' => $id]); DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error($e->getCode(), $e->getMessage()); } return $this->success([], '删除成功'); } }