Json.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\response;
  12. use think\Response;
  13. class Json extends Response
  14. {
  15. // 输出参数
  16. protected $options = [
  17. 'json_encode_param' => JSON_UNESCAPED_UNICODE,
  18. ];
  19. protected $contentType = 'application/json';
  20. /**
  21. * 处理数据
  22. * @access protected
  23. * @param mixed $data 要处理的数据
  24. * @return mixed
  25. * @throws \Exception
  26. */
  27. protected function output($data)
  28. {
  29. try {
  30. // 返回JSON数据格式到客户端 包含状态信息
  31. $data = json_encode($data, $this->options['json_encode_param']);
  32. if (false === $data) {
  33. throw new \InvalidArgumentException(json_last_error_msg());
  34. }
  35. return $data;
  36. } catch (\Exception $e) {
  37. if ($e->getPrevious()) {
  38. throw $e->getPrevious();
  39. }
  40. throw $e;
  41. }
  42. }
  43. }