使用writeJson不返回数据是为什么呢

来源:2-4 EasySwoole结合Mysql使用

慕少5137873

2019-11-15

图片描述
这个是Api下的index

<?php


namespace App\HttpController\Api;

use App\HttpController\Api\Base;
use EasySwoole\Component\Di;


class Index extends Base
{

    public function video()
    {
        //$config =  new \EasySwoole\Mysqli\Config(config::getInstance()->getConf('MYSQL'));
        $db = Di::getInstance()->get('MYSQL');
        $db->queryBuilder()->getOne('video');
        $result = $db->execBuilder();
        $this->writeJson('201', 'ok', $result);
    }

    public function nihao()
    {
        //$this->response()->write('hhhhhh');
        return $this->writeJson('101', 'ok');
    }
}

这个是Base

<?php


namespace App\HttpController\Api;

use EasySwoole\Http\AbstractInterface\Controller;

/**
 * Api下基础类库
 * Class Base
 * @package App\HttpController\Api
 */
class Base extends Controller
{
    public function index()
    {
        // TODO: Implement index() method.
    }

    /**
     * 程序有异常时报错
     * @param \Throwable $throwable
     */
    public function onException(\Throwable $throwable): void
    {
         // TODO: Change the autogenerated stub
        //记录日志,待完善
         $this->writeJson(400, '','内部有异常');
    }

    /**
     * Api返回Json数据格式
     * @param int $statusCode
     * @param null $message
     * @param null $result
     * @return bool
     */
    protected function writeJson($statusCode = 200, $message = null, $result = null){
        if(!$this->response()->isEndResponse()){
            $data = Array(
                "code"=>$statusCode,
                "message"=>$message,
                "result"=>$result
            );
            $this->response()->write(json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
            $this->response()->withHeader('Content-type','application/json;charset=utf-8');
            $this->response()->withStatus($statusCode);
            return true;
        }else{
            return false;
        }
    }
}
写回答

1回答

singwa

2019-11-15

//img.mukewang.com/szimg/5dce92080919ebfc15040614.jpg

图1中可以,图2不可以吗?

0
3
singwa
回复
慕少5137873
OK 。解决了就好。攒 祝学习愉快
2019-11-18
共3条回复

EasySwoole+ElasticSearch打造高性能小视频服务系统

系统掌握EasySwoole框架,从容应对高并发、高性能业务

556 学习 · 235 问题

查看课程