BaseService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use yii\web\NotFoundHttpException;
  5. use yii\web\UnprocessableEntityHttpException;
  6. /**
  7. * Class BaseService
  8. * @package services\common
  9. */
  10. class BaseService
  11. {
  12. /**
  13. * 当前版本号
  14. *
  15. * @return mixed|string
  16. * @throws NotFoundHttpException
  17. */
  18. public function version()
  19. {
  20. $update = Yii::$app->services->addons->findAuthority();
  21. return $update->version ?? '未授权';
  22. }
  23. /**
  24. * @return int
  25. * @throws \yii\db\Exception
  26. */
  27. public function getDefaultDbSize()
  28. {
  29. $models = Yii::$app->db->createCommand('SHOW TABLE STATUS')->queryAll();
  30. $models = array_map('array_change_key_case', $models);
  31. // 数据库大小
  32. $mysqlSize = 0;
  33. foreach ($models as $model) {
  34. $mysqlSize += $model['data_length'];
  35. }
  36. return $mysqlSize;
  37. }
  38. /**
  39. * @return false|int|string
  40. */
  41. public function getUserIp()
  42. {
  43. return Yii::$app->request->userIP ?? '0.0.0.0';
  44. }
  45. /**
  46. * 打印
  47. *
  48. * @param mixed ...$array
  49. */
  50. public function p(...$array)
  51. {
  52. echo "<pre>";
  53. if (count($array) == 1) {
  54. print_r($array[0]);
  55. } else {
  56. print_r($array);
  57. }
  58. echo '</pre>';
  59. }
  60. /**
  61. * 解析系统报错
  62. *
  63. * @param \Exception $e
  64. * @return array
  65. */
  66. public function getErrorInfo(\Exception $e)
  67. {
  68. return [
  69. 'errorMessage' => $e->getMessage(),
  70. 'type' => get_class($e),
  71. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  72. 'line' => $e->getLine(),
  73. 'stack-trace' => explode("\n", $e->getTraceAsString()),
  74. ];
  75. }
  76. /**
  77. * 解析微信是否报错
  78. *
  79. * @param array $message 微信回调数据
  80. * @param bool $direct 是否直接报错
  81. * @return bool
  82. * @throws UnprocessableEntityHttpException
  83. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  84. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  85. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  86. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  87. * @throws \Psr\SimpleCache\InvalidArgumentException
  88. */
  89. public function getWechatError($message, $direct = true)
  90. {
  91. if (isset($message['errcode']) && $message['errcode'] != 0) {
  92. // token过期 强制重新从微信服务器获取 token.
  93. if ($message['errcode'] == 40001) {
  94. Yii::$app->wechat->app->access_token->getToken(true);
  95. }
  96. if ($direct) {
  97. throw new UnprocessableEntityHttpException($message['errmsg']);
  98. }
  99. return $message['errmsg'];
  100. }
  101. return false;
  102. }
  103. /**
  104. * 解析错误
  105. *
  106. * @param $fistErrors
  107. * @return string
  108. */
  109. public function analysisErr($firstErrors)
  110. {
  111. if (!is_array($firstErrors) || empty($firstErrors)) {
  112. return false;
  113. }
  114. $errors = array_values($firstErrors)[0];
  115. return $errors ?? '未捕获到错误信息';
  116. }
  117. }
粤ICP备19079148号