AdminApiExceptionHandle.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 app\adminapi;
  12. use crmeb\exceptions\AdminException;
  13. use crmeb\exceptions\ApiException;
  14. use crmeb\exceptions\AuthException;
  15. use think\exception\Handle;
  16. use think\exception\ValidateException;
  17. use think\facade\Env;
  18. use think\facade\Log;
  19. use think\Response;
  20. use Throwable;
  21. class AdminApiExceptionHandle extends Handle
  22. {
  23. /**
  24. * 不需要记录信息(日志)的异常类列表
  25. * @var array
  26. */
  27. protected $ignoreReport = [
  28. ValidateException::class,
  29. AuthException::class,
  30. AdminException::class,
  31. ApiException::class,
  32. ];
  33. /**
  34. * 记录异常信息(包括日志或者其它方式记录)
  35. * @access public
  36. * @param Throwable $exception
  37. * @return void
  38. */
  39. public function report(Throwable $exception):void
  40. {
  41. if (!$this->isIgnoreReport($exception)) {
  42. $data = [
  43. 'file' => $exception->getFile(),
  44. 'line' => $exception->getLine(),
  45. 'message' => $this->getMessage($exception),
  46. 'code' => $this->getCode($exception),
  47. ];
  48. //日志内容
  49. $log = [
  50. request()->adminId(), //管理员ID
  51. request()->ip(), //客户ip
  52. ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
  53. request()->rule()->getMethod(), //请求类型
  54. str_replace("/", "", request()->rootUrl()), //应用
  55. request()->baseUrl(), //路由
  56. json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //请求参数
  57. json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
  58. ];
  59. Log::write(implode("|", $log), "error");
  60. }
  61. }
  62. /**
  63. * Render an exception into an HTTP response.
  64. * @access public
  65. * @param \think\Request $request
  66. * @param Throwable $e
  67. * @return Response
  68. */
  69. public function render($request, Throwable $e):Response
  70. {
  71. $massageData = Env::get('app_debug', false) ? [
  72. 'file' => $e->getFile(),
  73. 'line' => $e->getLine(),
  74. 'trace' => $e->getTrace(),
  75. 'previous' => $e->getPrevious(),
  76. ] : [];
  77. // 添加自定义异常处理机制
  78. if ($e instanceof DbException) {
  79. return app('json')->fail('数据获取失败', $massageData);
  80. } elseif ($e instanceof AuthException || $e instanceof ValidateException || $e instanceof ApiException) {
  81. return app('json')->make($e->getCode() ? : 400, $e->getMessage());
  82. } elseif ($e instanceof AdminException) {
  83. return app('json')->fail($e->getMessage(), $massageData);
  84. } else {
  85. return app('json')->code(200)->make(400, $e->getMessage(), $massageData);
  86. }
  87. }
  88. }