123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/11/9
- * Time: 上午10:28
- */
- namespace app\common\service;
- use app\common\constants\ErrorCodeConstants;
- use app\common\helper\ArrayHelper;
- use app\common\internalInterface\SingletonService;
- use app\common\model\object\ReturnObject;
- use think\Model;
- use think\Request;
- use app\main\service\LogService;
- /**
- * 单例接口文件
- * Interface BaseService
- * @package cps\src\Service
- */
- abstract class BaseService implements SingletonService
- {
- /**
- * @var Request
- */
- public $request;
- /**
- * @var ReturnObject
- */
- public $oReturn;
- /**
- * @var Model
- */
- protected $model;
- /**
- * BaseService constructor.
- */
- public function __construct()
- {
- $this->oReturn = new ReturnObject();
- $this->request = Request::instance();
- }
- /**
- * @return ReturnObject
- */
- public function getReturn()
- {
- $tree = debug_backtrace();
- $msg = $this->oReturn->toArray();
- array_shift($tree);
- unset($msg['data']);
- if (ErrorCodeConstants::checkErrorCodeToLog($this->oReturn->code)) {
- $msg['debug'] = $this->getContentFromTree(array_slice($tree, 0, 5));
- LogService::error(json_encode($msg, JSON_UNESCAPED_UNICODE));
- } else {
- $msg['debug'] = $this->getContentFromTree(array_slice($tree, 0, 1));
- LogService::debug(json_encode($msg, JSON_UNESCAPED_UNICODE));
- }
- if (!$this->oReturn->msg) {
- $this->oReturn->msg = ArrayHelper::arrayGet(ErrorCodeConstants::$desc, $this->oReturn->code);
- }
- $return = $this->oReturn;
- $this->oReturn = new ReturnObject();
- return $return;
- }
- /**
- * 获取运行栈
- * @param $tree
- * @return array
- */
- public function getContentFromTree($tree)
- {
- $return = [];
- $keys = ['file', 'line', 'function', 'class', 'args'];
- foreach ($tree as $item) {
- if(ArrayHelper::arrayGet($item, 'file')){
- $file = pathinfo($item['file']);
- $item['file'] = $file['basename'];
- }
- if(ArrayHelper::arrayGet($item, 'class')){
- $path = explode('\\', $item['class']);
- $item['class'] = array_pop($path);
- }
- $tmp = [];
- foreach ($keys as $key) {
- if (array_key_exists($key, $item)) {
- $tmp[$key] = $item[$key];
- }
- }
- $return[] = $tmp;
- }
- return $return;
- }
- /**
- * @param \Exception $e
- * @return ReturnObject
- */
- public function getExceptionReturn(\Exception $e)
- {
- LogService::exception($e);
- $this->oReturn->code = $e->getCode();
- $this->oReturn->msg = $e->getMessage();
- return $this->getReturn();
- }
- /**
- * @param $data
- * @return $this
- */
- public function setData($data)
- {
- $this->oReturn->data = $data;
- return $this;
- }
- /**
- * @param $msg
- * @return $this
- */
- public function setMsg($msg)
- {
- $this->oReturn->msg = $msg;
- return $this;
- }
- /**
- * @param $code
- * @return $this
- */
- public function setCode($code)
- {
- $this->oReturn->code = $code;
- return $this;
- }
- }
|