Commit faafecce authored by 王源's avatar 王源
Browse files

Merge branch 'feature/exception' into rpc/shopify

parents 993c55ea b0e265fd
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -27,7 +27,8 @@
    "hyperf/service-governance": "~1.1.0",
    "fzaninotto/faker": "^1.9",
    "phpoffice/phpspreadsheet": "^1.8",
    "dimsav/unix-zipper": "1.*"
    "dimsav/unix-zipper": "1.*",
    "hyperf/amqp": "~1.1.0"
  },
  "autoload": {
    "psr-4": {
+43 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Meibuyu\Micro\Amqp\Producer;

use Exception;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Model\Auth;

/**
 * @Producer(exchange="micro", routingKey="exception-log")
 */
class ExceptionLogProducer extends ProducerMessage
{

    public function __construct($data)
    {
        try {
            $data['operator'] = Auth::user()['name'];
        } catch (HttpResponseException $e) {
            put_log('获取操作人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
        }
        // 排除依赖包文件和运行缓存文件
        if (strpos($data['file'], '/vendor/') === false && strpos($data['file'], '/runtime/') === false) {
            try {
                exec("git blame -L {$data['line']},{$data['line']} {$data['file']}", $output);
                if (!empty($output[0]) && is_string($output[0])) {
                    preg_match('/(?<=\()[^ ]+/', $output[0], $matches);
                    if (!empty($matches[0])) {
                        $data['coder'] = $matches[0];
                    }
                }
            } catch (Exception $e) {
                put_log('获取编码人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
            }
        }
        $this->payload = $data;
    }

}
+44 −5
Original line number Diff line number Diff line
@@ -4,16 +4,27 @@ declare(strict_types=1);

namespace Meibuyu\Micro\Exceptions\Handler;

use Exception;
use Hyperf\Amqp\Producer;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Logger\LoggerFactory;
use Meibuyu\Micro\Amqp\Producer\ExceptionLogProducer;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;

class AppExceptionHandler extends ExceptionHandler
{

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var StdoutLoggerInterface
     */
@@ -24,20 +35,48 @@ class AppExceptionHandler extends ExceptionHandler
     */
    protected $logger;

    public function __construct(StdoutLoggerInterface $stdoutLogger, LoggerFactory $loggerFactory)
    /**
     * @var ConfigInterface
     */
    protected $config;

    public function __construct(ContainerInterface $container)
    {
        $this->stdoutLogger = $stdoutLogger;
        $this->logger = $loggerFactory->get('Uncaught Exception');
        $this->container = $container;
        $this->stdoutLogger = $container->get(StdoutLoggerInterface::class);
        $this->logger = $container->get(LoggerFactory::class)->get('Uncaught Exception');
        $this->config = $container->get(ConfigInterface::class);
    }

    public function handle(Throwable $throwable, ResponseInterface $response)
    {
        // 捕获所有未捕获的异常
        $this->stopPropagation();
        $msg = sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile());
        $message = $throwable->getMessage();
        $line = $throwable->getLine();
        $file = $throwable->getFile();
        $trace = $throwable->getTraceAsString();
        $code = $throwable->getCode();
        $data = [
            'server' => $this->config->get('app_name'),
            'file' => $file,
            'line' => $line,
            'message' => $message,
            'trace' => $trace,
            'code' => $code,
            'created_at' => now(),
        ];
        try {
            $exceptionLogProducer = new ExceptionLogProducer($data);
            $producer = $this->container->get(Producer::class);
            $producer->produce($exceptionLogProducer);
        } catch (Exception $e) {
            put_log('异常日志失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
        }
        $msg = sprintf('%s[%s] in %s', $message, $line, $file);
        $this->stdoutLogger->error($msg);
        $this->logger->error($msg);
        $this->stdoutLogger->error($throwable->getTraceAsString());
        $this->stdoutLogger->error($trace);
        return $response->withStatus(500)->withBody(new SwooleStream($msg));
    }

+7 −0
Original line number Diff line number Diff line
@@ -16,4 +16,11 @@ interface OperationServiceInterface
     * @return mixed
     */
    public function getBusinessSaleOtherDatum(array $conditions);

    /**
     * 获取销售报表其他数据
     * @param array $conditions 必传条件  site_id team_id start_time end_time
     * @return mixed
     */
    public function getBusinessSaleOtherDatumRmb(array $conditions);
}
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
@@ -93,6 +93,14 @@ interface SubOrderServiceInterface
     */
    public function purchaseError($orderId,$errorCode):array ;

    /**
     * 1688采购取消
     * @param $orderNo
     * @param $editData
     * @return bool
     */
    public function purchaseCancel($orderNo,$editData = []):bool;

    /**
     * 1688采购 修改oa子订单
     * @param $editData
Loading