JsonReturn.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare (strict_types=1);
  3. namespace laytp\traits;
  4. use app\model\api\Log;
  5. /**
  6. * 所有接口success和error返回数据格式定义
  7. */
  8. trait JsonReturn
  9. {
  10. /**
  11. * 操作成功返回数据
  12. * @param string $msg
  13. * @param null $data
  14. * @return false|string|\think\response\Json
  15. */
  16. public function success($msg = '', $data = null)
  17. {
  18. global $_GPC;
  19. $result = [
  20. 'code' => 0,
  21. 'msg' => $msg,
  22. 'time' => time(),
  23. 'data' => $data ?? new \stdClass(),
  24. ];
  25. //response返回的Content-Type:text/html; charset=utf-8,使用浏览器单独访问接口地址方便调试,
  26. //但是注意在javascript进行ajax请求时,要设置dataType: "json",这样,javascript得到字符串后,会自动使用JSON.parse对字符串进行解析
  27. // return json_encode($result,JSON_UNESCAPED_UNICODE);
  28. //使用json方法进行返回的好处:
  29. // 1.客户端在进行ajax请求时,不需要设置dataType: "json",依旧会认为得到的数据是json
  30. // 2.APP_DEBUG = true,客户端得到的也仅仅是json部分数据,不会夹杂trace等debug代码
  31. // return json($result);
  32. //记录Api请求日志
  33. if (app('http')->getName() === 'api') {
  34. Log::create([
  35. 'rule' => request()->url(),
  36. 'request_body' => json_encode(request()->post(), JSON_UNESCAPED_UNICODE),
  37. 'request_header' => json_encode(request()->header(), JSON_UNESCAPED_UNICODE),
  38. 'ip' => request()->ip(),
  39. 'status_code' => 200,
  40. 'response_body' => json_encode($result, JSON_UNESCAPED_UNICODE),
  41. 'create_time' => date('Y-m-d H:i:s'),
  42. 'uniacid' => $_GPC['uniacid']
  43. ]);
  44. }
  45. //最终做如下处理:在测试环境且传入的debug=1的参数,就返回html,否则返回json数据
  46. //在测试环境可以单独用浏览器打开接口地址,传入debug=1的参数对接口进行调试
  47. if (env('APP_DEBUG') && request()->param('debug')) {
  48. return json_encode($result, JSON_UNESCAPED_UNICODE);
  49. } else {
  50. return json($result);
  51. }
  52. }
  53. /**
  54. * 操作失败返回数据
  55. * @param string $msg
  56. * @param int $code 错误码,为0表示没有错误,为1表示常规错误,前端仅需提示msg,其他值有具体含义,比如10401为未登录,前端需要跳转至登录界面
  57. * @param array $data
  58. * @return \think\response\Json
  59. */
  60. public function error($msg = '', $code = 1, $data = null)
  61. {
  62. global $_GPC;
  63. $result = [
  64. 'code' => $code,
  65. 'msg' => $msg,
  66. 'time' => time(),
  67. 'data' => $data ?? new \stdClass(),
  68. ];
  69. //记录Api请求日志
  70. if (app('http')->getName() === 'api') {
  71. Log::create([
  72. 'rule' => request()->url(),
  73. 'request_body' => json_encode(request()->post(), JSON_UNESCAPED_UNICODE),
  74. 'request_header' => json_encode(request()->header(), JSON_UNESCAPED_UNICODE),
  75. 'ip' => request()->ip(),
  76. 'status_code' => 200,
  77. 'response_body' => json_encode($result, JSON_UNESCAPED_UNICODE),
  78. 'create_time' => date('Y-m-d H:i:s'),
  79. 'uniacid' => $_GPC['uniacid']
  80. ]);
  81. }
  82. if (env('APP_DEBUG') && request()->param('debug')) {
  83. return json_encode($result, JSON_UNESCAPED_UNICODE);
  84. } else {
  85. return json($result);
  86. }
  87. }
  88. /**
  89. * 捕获异常后返回数据
  90. * @param $e \Exception
  91. * @return \think\response\Json
  92. */
  93. public function exceptionError($e){
  94. $data = [];
  95. if(env('APP_DEBUG')){
  96. $data = [
  97. 'message' => $e->getMessage(),
  98. 'file' => $e->getFile(),
  99. 'line' => $e->getLine(),
  100. 'trace' => $e->getTrace()
  101. ];
  102. }
  103. return $this->error($e->getMessage(), 1, $data);
  104. }
  105. }