BaseService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/11/9
  6. * Time: 上午10:28
  7. */
  8. namespace app\common\service;
  9. use app\common\constants\ErrorCodeConstants;
  10. use app\common\helper\ArrayHelper;
  11. use app\common\internalInterface\SingletonService;
  12. use app\common\model\object\ReturnObject;
  13. use think\Model;
  14. use think\Request;
  15. use app\main\service\LogService;
  16. /**
  17. * 单例接口文件
  18. * Interface BaseService
  19. * @package cps\src\Service
  20. */
  21. abstract class BaseService implements SingletonService
  22. {
  23. /**
  24. * @var Request
  25. */
  26. public $request;
  27. /**
  28. * @var ReturnObject
  29. */
  30. public $oReturn;
  31. /**
  32. * @var Model
  33. */
  34. protected $model;
  35. /**
  36. * BaseService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->oReturn = new ReturnObject();
  41. $this->request = Request::instance();
  42. }
  43. /**
  44. * @return ReturnObject
  45. */
  46. public function getReturn()
  47. {
  48. $tree = debug_backtrace();
  49. $msg = $this->oReturn->toArray();
  50. array_shift($tree);
  51. unset($msg['data']);
  52. if (ErrorCodeConstants::checkErrorCodeToLog($this->oReturn->code)) {
  53. $msg['debug'] = $this->getContentFromTree(array_slice($tree, 0, 5));
  54. LogService::error(json_encode($msg, JSON_UNESCAPED_UNICODE));
  55. } else {
  56. $msg['debug'] = $this->getContentFromTree(array_slice($tree, 0, 1));
  57. LogService::debug(json_encode($msg, JSON_UNESCAPED_UNICODE));
  58. }
  59. if (!$this->oReturn->msg) {
  60. $this->oReturn->msg = ArrayHelper::arrayGet(ErrorCodeConstants::$desc, $this->oReturn->code);
  61. }
  62. $return = $this->oReturn;
  63. $this->oReturn = new ReturnObject();
  64. return $return;
  65. }
  66. /**
  67. * 获取运行栈
  68. * @param $tree
  69. * @return array
  70. */
  71. public function getContentFromTree($tree)
  72. {
  73. $return = [];
  74. $keys = ['file', 'line', 'function', 'class', 'args'];
  75. foreach ($tree as $item) {
  76. if(ArrayHelper::arrayGet($item, 'file')){
  77. $file = pathinfo($item['file']);
  78. $item['file'] = $file['basename'];
  79. }
  80. if(ArrayHelper::arrayGet($item, 'class')){
  81. $path = explode('\\', $item['class']);
  82. $item['class'] = array_pop($path);
  83. }
  84. $tmp = [];
  85. foreach ($keys as $key) {
  86. if (array_key_exists($key, $item)) {
  87. $tmp[$key] = $item[$key];
  88. }
  89. }
  90. $return[] = $tmp;
  91. }
  92. return $return;
  93. }
  94. /**
  95. * @param \Exception $e
  96. * @return ReturnObject
  97. */
  98. public function getExceptionReturn(\Exception $e)
  99. {
  100. LogService::exception($e);
  101. $this->oReturn->code = $e->getCode();
  102. $this->oReturn->msg = $e->getMessage();
  103. return $this->getReturn();
  104. }
  105. /**
  106. * @param $data
  107. * @return $this
  108. */
  109. public function setData($data)
  110. {
  111. $this->oReturn->data = $data;
  112. return $this;
  113. }
  114. /**
  115. * @param $msg
  116. * @return $this
  117. */
  118. public function setMsg($msg)
  119. {
  120. $this->oReturn->msg = $msg;
  121. return $this;
  122. }
  123. /**
  124. * @param $code
  125. * @return $this
  126. */
  127. public function setCode($code)
  128. {
  129. $this->oReturn->code = $code;
  130. return $this;
  131. }
  132. }