123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/11/9
- * Time: 上午10:28
- */
- namespace app\main\service;
- use app\admin\library\Auth;
- use app\admin\library\traits\Backend;
- use app\main\constants\ErrorCodeConstants;
- use app\main\InternalInterface\SingletonService;
- use app\main\model\object\ReturnObject;
- use think\Config;
- use think\Model;
- /**
- * 单例接口文件
- * Interface BaseService
- * @package cps\src\Service
- */
- abstract class BaseService implements SingletonService
- {
- use Backend;
- /**
- * @var ReturnObject
- */
- public $oReturn;
- /**
- * @var Auth
- */
- public $auth;
- /**
- * @var Model
- */
- protected $model;
- /**
- * BaseService constructor.
- */
- public function __construct()
- {
- $this->oReturn = new ReturnObject();
- }
- /**
- * @return ReturnObject
- */
- public function getReturn()
- {
- $tree = debug_backtrace();
- $msg = $this->oReturn->toArray();
- array_shift($tree);
- if (ErrorCodeConstants::checkErrorCodeToLog($this->oReturn->code)) {
- $msg['stack'] = LogService::getStackTree(array_slice($tree, 0, 5));
- LogService::error(json_encode($msg, JSON_UNESCAPED_UNICODE));
- } else {
- $msg['stack'] = LogService::getStackTree(array_slice($tree, 0, 1));
- LogService::debug(json_encode($msg, JSON_UNESCAPED_UNICODE));
- }
- $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){
- $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 = ErrorCodeConstants::EXCEPTION;
- $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;
- }
- }
|