美文网首页
控制器数据返回格式化字段

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

作者: Jason_b3e0 | 来源:发表于2020-09-25 12:23 被阅读0次

    控制器调用

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

    单条数据

    <?php
    
    namespace App\Modules\Crm\Resources\AssistancePromise;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class AssistancePromiseResource extends JsonResource
    {
        public function toArray($request)
        {
            return [
                'id' => $this->id,
            ];
        }
    }
    

    多条分页数据

    <?php
    
    namespace App\Modules\Crm\Resources\AssistancePromise;
    
    use Illuminate\Http\Resources\Json\ResourceCollection;
    
    class AssistancePromiseCollection extends ResourceCollection
    {
        public function toArray($request)
        {
            return $this->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);
                }
            );
        }
    

    相关文章

      网友评论

          本文标题:控制器数据返回格式化字段

          本文链接:https://www.haomeiwen.com/subject/cbqfuktx.html