BaseService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/11/9
  6. * Time: 上午10:28
  7. */
  8. namespace app\main\service;
  9. use app\admin\library\Auth;
  10. use app\admin\library\traits\Backend;
  11. use app\main\constants\ErrorCodeConstants;
  12. use app\main\InternalInterface\SingletonService;
  13. use app\main\model\object\ReturnObject;
  14. use think\Config;
  15. use think\Model;
  16. /**
  17. * 单例接口文件
  18. * Interface BaseService
  19. * @package cps\src\Service
  20. */
  21. abstract class BaseService implements SingletonService
  22. {
  23. use Backend;
  24. /**
  25. * @var ReturnObject
  26. */
  27. public $oReturn;
  28. /**
  29. * @var Auth
  30. */
  31. public $auth;
  32. /**
  33. * @var Model
  34. */
  35. protected $model;
  36. /**
  37. * BaseService constructor.
  38. */
  39. public function __construct()
  40. {
  41. $this->oReturn = new ReturnObject();
  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. if (ErrorCodeConstants::checkErrorCodeToLog($this->oReturn->code)) {
  52. $msg['stack'] = LogService::getStackTree(array_slice($tree, 0, 5));
  53. LogService::error(json_encode($msg, JSON_UNESCAPED_UNICODE));
  54. } else {
  55. $msg['stack'] = LogService::getStackTree(array_slice($tree, 0, 1));
  56. LogService::debug(json_encode($msg, JSON_UNESCAPED_UNICODE));
  57. }
  58. $return = $this->oReturn;
  59. $this->oReturn = new ReturnObject();
  60. return $return;
  61. }
  62. /**
  63. * 获取运行栈
  64. * @param $tree
  65. * @return array
  66. */
  67. public function getContentFromTree($tree)
  68. {
  69. $return = [];
  70. $keys = ['file', 'line', 'function', 'class', 'args'];
  71. foreach($tree as $item){
  72. $tmp = [];
  73. foreach($keys as $key){
  74. if(array_key_exists($key, $item)){
  75. $tmp[$key] = $item[$key];
  76. }
  77. }
  78. $return[] = $tmp;
  79. }
  80. return $return;
  81. }
  82. /**
  83. * @param \Exception $e
  84. * @return ReturnObject
  85. */
  86. public function getExceptionReturn(\Exception $e)
  87. {
  88. LogService::exception($e);
  89. $this->oReturn->code = ErrorCodeConstants::EXCEPTION;
  90. $this->oReturn->msg = $e->getMessage();
  91. return $this->getReturn();
  92. }
  93. /**
  94. * @param $data
  95. * @return $this
  96. */
  97. public function setData($data)
  98. {
  99. $this->oReturn->data = $data;
  100. return $this;
  101. }
  102. /**
  103. * @param $msg
  104. * @return $this
  105. */
  106. public function setMsg($msg)
  107. {
  108. $this->oReturn->msg = $msg;
  109. return $this;
  110. }
  111. /**
  112. * @param $code
  113. * @return $this
  114. */
  115. public function setCode($code)
  116. {
  117. $this->oReturn->code = $code;
  118. return $this;
  119. }
  120. }