• API接口返回数据结构构建类


    <?php
    
    declare(strict_types=1);
    
    namespace wangqy\tcc\utils;
    
    use think\Response;
    
    class Result
    {
        /**
         * 结果状态码
         *
         * @var int
         */
        protected $code = 0;
        /**
         * http请求状态码
         *
         * @var int
         */
        protected $httpCode = 200;
    
        /**
         * 响应结果类型.
         *
         * @var string
         */
        protected $responseType = 'json';
    
        /**
         * 失败返回信息.
         *
         * @param $code
         * @param $msg
         * @param $data
         *
         * @return Response
         */
        public function fail($msg, $code, $data = null)
        {
            return $this->make($code, $msg, $data);
        }
    
        /**
         * 仅返回说明.
         *
         * @param $msg
         *
         * @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;
        }
    
        /**
         * 成功返回信息.
         *
         * @param $data
         * @param $msg
         *
         * @return Response
         */
        public function success($data = null, $msg = 'success')
        {
            return $this->make($this->code, lang($msg), $data);
        }
    
        /**
         * @param int        $code
         * @param string     $msg
         * @param null|array $data
         *
         * @return Response
         */
        private function make($code, $msg, $data = null)
        {
            $extra = [
                'log_id' => $GLOBALS['log_id'] ?? '',
                'now' => time(),
            ];
    
            $res = compact('code', 'msg', 'extra');
    
            if (null !== $data) {
                $res['data'] = $data;
            }
    
            return Response::create($res, $this->responseType, $this->httpCode);
        }
    }
    

      

    在common文件中

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

      

    实例:

      public function getList()
        {
            $params = input();
    
            $result = $this->logic->getList($params);
    
            return result()->success($result);
        }
    

      

    原文:http://upwqy.com/?p=5

  • 相关阅读:
    【模式分解】无损连接&保持函数依赖
    【范式与函数依赖】3NF与BCNF的区别
    C#设置按钮三态背景图片
    C#代码设置窗体和Panel的位置大小
    C#窗体嵌套
    C#中弹出新窗口
    C#定义委托函数实现在别的窗体中操作主窗体中的SerialPort控件
    C#界面设计疑问2:panel摆放问题
    C#拖动自己的定义标题栏(panel)以及实现窗体拖动关闭和最小化
    C#界面设计疑问
  • 原文地址:https://www.cnblogs.com/wqy415/p/16038997.html
Copyright © 2020-2023  润新知