ApiException.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Base\Exceptions;
  3. use App\Base\Services\AuthUser;
  4. class ApiException extends \Exception
  5. {
  6. use AuthUser;
  7. /**
  8. * 错误信息参数
  9. * @var array
  10. */
  11. protected $params = [];
  12. /**
  13. * 携带的参数
  14. * @var array
  15. */
  16. protected $data = [];
  17. protected $langs = [];
  18. /** 处理异常错误
  19. * @param string $id 语言包中的key or 错误代码
  20. * @param null $message 错误信息 or 替换参数
  21. * @param array $params 替换参数 or 附加数据
  22. * @param int $code 错误代码 or 无用
  23. * @param null $locale 语言 or 无用
  24. * @param array $data or 无用
  25. */
  26. public function __construct($id, $message = null, $params = [], $code = 0, $locale = null, $data = [])
  27. {
  28. if (empty($id)) {
  29. return parent::__construct($message, $code, null);
  30. }
  31. $locale = $locale ?: app('translator')->getLocale();
  32. if (empty($locale)) {
  33. $locale = "zh-cn";
  34. }
  35. $this->params = $params;
  36. $this->data = $data;
  37. if (is_numeric($id)) {
  38. //兼容原本的调用方式和顺序
  39. //$id, $params=[], $data=[], $message = null
  40. $this->params = $message ? (array)$message : [];
  41. $this->data = $params;
  42. if (!$code) {
  43. //根据中间件设置的语言处理多语言消息
  44. $message = $this->parseMessage($id);
  45. } else {
  46. $message = $code;
  47. }
  48. $code = $id;
  49. } else {
  50. $message = $this->parseNewMessage($id, $message, $code, $locale);
  51. }
  52. parent::__construct($message, $code, null);
  53. }
  54. /**
  55. * 解析错误信息
  56. * @param $code
  57. * @return string
  58. */
  59. protected function parseMessage($code)
  60. {
  61. $errors = config('error');
  62. if (!isset($errors[$code])) {
  63. return '服务器繁忙,请稍候重试.';
  64. }
  65. $message = trans($errors[$code], $this->params);
  66. if ($message != $code && !empty($this->params)) {
  67. foreach ($this->params as $key => $item) {
  68. $message = str_replace('{' . $key . '}', $item, $message);
  69. }
  70. }
  71. return $message;
  72. }
  73. /**
  74. * 获取参数数据
  75. * @return array
  76. */
  77. public function getData()
  78. {
  79. return $this->data;
  80. }
  81. /**
  82. * 解析语言包文件
  83. * @param $id
  84. * @param $message
  85. * @param $code
  86. * @param null $locale
  87. * @return array
  88. */
  89. public function parseNewMessage($id, $message, &$code, $locale = null)
  90. {
  91. $code = -1;
  92. if (is_null($id)) {
  93. return $message ?: $id;
  94. }
  95. $keys = explode('.', $id);
  96. $this->getLang($keys[0], $locale);
  97. if (!$this->langs[$locale][$keys[0]]) {
  98. return $message ?: $id;
  99. }
  100. $data = isset($this->langs[$locale][$keys[0]][$keys[1]]) ? $this->langs[$locale][$keys[0]][$keys[1]] : [];
  101. if ($data) {
  102. if (is_array($data)) {
  103. $code = $data[0];
  104. $message = $data[1];
  105. } else {
  106. $message = $data;
  107. }
  108. } else {
  109. $message = $message ?: $id;
  110. }
  111. if (!empty($this->params)) {
  112. foreach ($this->params as $key => $item) {
  113. $message = str_replace('{' . $key . '}', $item, $message);
  114. }
  115. }
  116. return $message;
  117. }
  118. /*
  119. * 取语言包文件
  120. */
  121. public function getLang($key, $locale = 'zh-cn')
  122. {
  123. $path = 'lang/' . $locale . '/' . $key;
  124. if (!isset($this->langs[$locale]) || !isset($this->langs[$locale][$key])) {
  125. $this->langs[$locale][$key] = include resource_path($path . '.php');
  126. }
  127. }
  128. }