控制器数据返回格式化字段

控制器调用

     return $this->success(
            new AssistancePromiseResource($policyClaimBusiness->findById(90))
        );

        return $this->success(
            new AssistancePromiseCollection($policyClaimBusiness->getListByUser($params))
        );

单条数据

 $this->id,
        ];
    }
}

多条分页数据

collection->map(function ($log) {
            return [
                'id'                 => $log->id
            ];
        })->all();
    }
}

BaseController

    /**
     * 接口响应
     * @param $data
     * @param string $message
     * @param int $code
     * @return array|\Illuminate\Support\HigherOrderTapProxy|mixed
     */
    protected function success($data, string $message = '', $code = 0)
    {
        if (!$data instanceof JsonResource)
        {
            return $this->formatData($data, $message, $code);
        }

        if ($data instanceof ResourceCollection && ($data->resource instanceof AbstractPaginator))
        {
            return $this->formatPaginatedResourceResponse(...func_get_args());
        }

        return $this->formatResourceResponse(...func_get_args());
    }

    protected function formatPaginatedResourceResponse($resource, string $message = '', $code = 0)
    {
        $paginated = $resource->resource->toArray();

        $paginationInformation = [
            'current_page'   => $paginated['current_page'] ?? null,
            'first_page_url' => $paginated['first_page_url'] ?? null,
            'from'           => $paginated['from'] ?? null,
            'last_page'      => $paginated['last_page'] ?? null,
            'last_page_url'  => $paginated['last_page_url'] ?? null,
            'next_page_url'  => $paginated['next_page_url'] ?? null,
            'path'           => $paginated['path'] ?? null,
            'per_page'       => $paginated['per_page'] ?? null,
            'prev_page_url'  => $paginated['prev_page_url'] ?? null,
            'to'             => $paginated['to'] ?? null,
            'total'          => $paginated['total'] ?? null,
        ];

        $data = array_merge_recursive(['data' => $this->parseDataFrom($resource)], $paginationInformation);

        return tap(
            response()->json($this->formatData($data, $message, $code), 200),
            function ($response) use ($resource) {
                $response->original = $resource->resource->map(
                    function ($item) {
                        return is_array($item) ? Arr::get($item, 'resource') : $item->resource;
                    }
                );

                $resource->withResponse(request(), $response);
            }
        );
    }

    protected function formatData($data, $message, &$code)
    {
        return [
            'code'    => $code,
            'message' => $message ?: 'success!',
            'data'    => $data ?: (object)$data,
            'time'    => get_now(),
            'module'  => config('service.name'),
        ];
    }

    /**
     * Parse data from JsonResource.
     *
     * @param JsonResource $data
     *
     * @return array
     */
    protected function parseDataFrom(JsonResource $data)
    {
        return array_merge_recursive($data->resolve(request()), $data->with(request()), $data->additional);
    }

    protected function formatResourceResponse($resource, string $message = '', $code = 0)
    {
        return tap(
            response()->json($this->formatData($this->parseDataFrom($resource), $message, $code), 200),
            function ($response) use ($resource) {
                $response->original = $resource->resource;

                $resource->withResponse(request(), $response);
            }
        );
    }

你可能感兴趣的:(控制器数据返回格式化字段)