Json.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\utils;
  12. use think\facade\Config;
  13. use think\facade\Lang;
  14. use think\Log;
  15. use think\Response;
  16. /**
  17. * Json输出类
  18. * Class Json
  19. * @package crmeb\utils
  20. */
  21. class Json
  22. {
  23. private $code = 200;
  24. public function code(int $code): self
  25. {
  26. $this->code = $code;
  27. return $this;
  28. }
  29. /**
  30. * 压缩数据
  31. * @param $str
  32. * @return string
  33. * @author 等风来
  34. * @email 136327134@qq.com
  35. * @date 2022/11/10
  36. */
  37. protected function compress($str)
  38. {
  39. return base64_encode(gzdeflate(json_encode($str, JSON_UNESCAPED_UNICODE), 9));
  40. }
  41. /**
  42. * @param int $status
  43. * @param string $msg
  44. * @param array|null $data
  45. * @return Response
  46. * @author 等风来
  47. * @email 136327134@qq.com
  48. * @date 2022/11/10
  49. */
  50. public function make(int $status, string $msg, ?array $data = null): Response
  51. {
  52. $request = app()->request;
  53. $res = compact('status', 'msg');
  54. if (!is_null($data)) {
  55. $jsonData = json_encode($data);
  56. //在debug关闭的时候返回压缩数据
  57. $compressData = null;
  58. if (strstr('/' . app()->request->rule()->getRule(), '/api/') !== false && strlen($jsonData) > 1024 && app()->config->get('cache.is_gzde', false)) {
  59. $compressData = $this->compress($data);
  60. $res['gzde'] = 1;
  61. }
  62. $res['data'] = $compressData ?: $data;
  63. }
  64. if ($res['msg'] && !is_numeric($res['msg'])) {
  65. if (!$range = $request->get('lang')) {
  66. $range = $request->cookie(Config::get('lang.cookie_var'));
  67. }
  68. $langData = array_values(Config::get('lang.accept_language', []));
  69. if (!in_array($range, $langData)) {
  70. $range = 'zh-cn';
  71. }
  72. $res['msg'] = Lang::get($res['msg'], [], $range);
  73. }
  74. //记录原始数据
  75. $response = $res;
  76. $response['data'] = $data;
  77. response_log_write((array)$res, Log::INFO);
  78. return Response::create($res, 'json', $this->code);
  79. }
  80. public function success($msg = 'ok', ?array $data = null): Response
  81. {
  82. if (is_array($msg)) {
  83. $data = $msg;
  84. $msg = 'ok';
  85. }
  86. return $this->make(200, $msg, $data);
  87. }
  88. public function successful(...$args): Response
  89. {
  90. return $this->success(...$args);
  91. }
  92. public function fail($msg = 'fail', ?array $data = null): Response
  93. {
  94. if (is_array($msg)) {
  95. $data = $msg;
  96. $msg = 'ok';
  97. }
  98. return $this->make(400, $msg, $data);
  99. }
  100. public function status($status, $msg = 'ok', $result = [])
  101. {
  102. $status = strtoupper($status);
  103. if (is_array($msg)) {
  104. $result = $msg;
  105. $msg = 'ok';
  106. }
  107. return $this->success($msg, compact('status', 'result'));
  108. }
  109. }