Json.php 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace daswork\response;
  3. use daswork\Response;
  4. class Json extends Response
  5. {
  6. // 输出参数
  7. protected $options = [
  8. 'json_encode_param' => JSON_UNESCAPED_UNICODE,
  9. ];
  10. protected $contentType = 'application/json';
  11. /**
  12. * 处理数据
  13. * @access protected
  14. * @param mixed $data 要处理的数据
  15. * @return mixed
  16. * @throws \Exception
  17. */
  18. protected function output($data)
  19. {
  20. try {
  21. // 返回JSON数据格式到客户端 包含状态信息
  22. $data = json_encode($data, $this->options['json_encode_param']);
  23. if ($data === false) {
  24. throw new \InvalidArgumentException(json_last_error_msg());
  25. }
  26. return $data;
  27. } catch (\Exception $e) {
  28. if ($e->getPrevious()) {
  29. throw $e->getPrevious();
  30. }
  31. throw $e;
  32. }
  33. }
  34. }