接口返回结构


 

返回结果处理,归根结底 主要是有两点 数据结构和返回的数据类型

1、数据类型 :一般情况下,API 需要返回数据类型是JSON

2、数据结构:需要一个code来表明 返回结果状态,一个msg用户状态的描述 一个data用于包含所需要的返回的数据

明白上面两点,后续的无非就是就行封装,具体怎么封装,看个人习惯。

 

<?php

/*
 *  +----------------------------------------------------------------------
 *  | wangqy
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2022 http://upwqy.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Author: wangqy <529857614@qq.com>
 *  +----------------------------------------------------------------------
 */

namespace common\utils;

use think\Response;

class Result
{
    protected $header;

    /**
     * 结果状态码
     *
     * @var int
     */
    protected $code = 200;

    /**
     * http请求状态码
     *
     * @var int
     */
    protected $httpCode = 200;

    /**
     * 响应结果类型.
     *
     * @var string
     */
    protected $responseType = 'json';

    public function __construct()
    {
        $this->header = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age' => 1800,
            'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With,XX-Device-Type,XX-Api-Version,XX-App-Version,XX-Token',
            'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS',
        ];
    }

    /**
     * 失败返回信息.
     *
     * @return Response
     */
    public function fail($msg, $code = 204, $data = null)
    {
        return $this->make($code, $msg, $data);
    }

    /**
     * 仅返回说明.
     *
     * @return Response
     */
    public function onlySucMsg($msg = 'success')
    {
        return $this->success(null, $msg);
    }

    /**
     * @return $this
     */
    public function setCode(int $code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * @return $this
     */
    public function setHttpCode(int $httpCode)
    {
        $this->httpCode = $httpCode;

        return $this;
    }

    /**
     * @return $this
     */
    public function setResponseType(string $responseType)
    {
        $this->responseType = $responseType;

        return $this;
    }

    /**
     * 成功返回信息.
     *
     * @return Response
     */
    public function success($data = null, $msg = 'success')
    {
        return $this->make($this->code, $msg, $data);
    }

    public function aliSuc($data)
    {
        $res = array_merge($data, ['StatusCode' => $this->code]);

        return Response::create($res, $this->responseType, $this->httpCode)->header($this->header);
    }

    /**
     * @param int        $code
     * @param string     $msg
     * @param null|array $data
     *
     * @return Response
     */
    private function make($code, $msg, $data = null)
    {
        $extra = [
            'rid' => $GLOBALS['log_id'] ?? '',
            'now' => date('Y-m-d H:i:s', time()),
        ];

        $msg = \lang($msg); // 多语言转换

        $res = compact('code', 'msg', 'extra');

        if (null != $data) {
            $res['data'] = $data;
        }

        return Response::create($res, $this->responseType, $this->httpCode)->header($this->header);
    }
}

 

 

 /**
     * 获取当前json对象实例.
     *
     * @return App|object|Result
     */
    function result()
    {
        return app(Result::class);
    }

 

调用实例

 public function getStsToken()
    {
        $aliStsLogic = app()->make(AliStsLogic::class);
        $result = $aliStsLogic->getToken();

        return result()->success($result);
    }

 

发布时间 : 2023-03-01,阅读量:1745 , 分类: PHP
本文链接:https://upwqy.com/details/5.html
git教程 (一)安装配置 系统上禁止运行脚本