| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- /**
- * 描述.
- *
- * @author nj <nj@gmail.com>
- * @date 2019-11-01 15:05:31
- */
- use Hyperf\Contract\StdoutLoggerInterface;
- use Hyperf\Utils\ApplicationContext;
- use Psr\EventDispatcher\EventDispatcherInterface;
- if (! function_exists('app')) {
- /**
- * Get the available container instance.
- *
- * @param string $abstract
- * @param array $parameters
- * @return mixed|\Psr\Container\ContainerInterface
- */
- function app($abstract = null, array $parameters = [])
- {
- if (is_null($abstract)) {
- if (\Hyperf\Utils\ApplicationContext::hasContainer()) {
- return \Hyperf\Utils\ApplicationContext::getContainer();
- }
- }
- return make($abstract, $parameters);
- }
- }
- if (! function_exists('response')) {
- /**
- * Return a new response from the application.
- *
- * @param string $content
- * @param int $status
- * @param array $headers
- * @return Psr\Http\Message\ResponseInterface
- */
- function response($content = '', $status = 200, array $headers = []) : \Psr\Http\Message\ResponseInterface
- {
- $factory = app(Hyperf\HttpServer\Contract\ResponseInterface::class);
- if (func_num_args() === 0) {
- return $factory;
- }
- $res = $factory->withStatus($status);
- if (is_array($headers)) {
- foreach ($headers as $k => $v) {
- $res = $res->withHeader($k, $v);
- }
- }
- return $res->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream((string) $content));
- }
- }
- if (! function_exists('request')) {
- /**
- * Get an instance of the current request or an input item from the request.
- *
- * @param array|string $key
- * @param mixed $default
- * @return \Hyperf\HttpServer\Contract\RequestInterface|string|array
- */
- function request($key = null, $default = null)
- {
- if (is_null($key)) {
- return make(\Hyperf\HttpServer\Contract\RequestInterface::class);
- }
- if (is_array($key)) {
- $value = make(\Hyperf\HttpServer\Contract\RequestInterface::class)->inputs($key);
- } else {
- $value = app(\Hyperf\HttpServer\Contract\RequestInterface::class)->input($key);
- }
- return is_null($value) ? value($default) : $value;
- }
- }
- if (! function_exists('collect')) {
- /**
- * Create a collection from the given value.
- *
- * @param mixed $value
- * @return \Hyperf\Utils\Collection
- */
- function collect($value = null)
- {
- return new \Hyperf\Utils\Collection($value);
- }
- }
- if (! function_exists('base_path')) {
- /**
- * Get the path to the base of the install.
- *
- * @param string $path
- * @return string
- */
- function base_path($path = '')
- {
- return BASE_PATH.($path ? DIRECTORY_SEPARATOR.$path : $path);
- }
- }
- if (! function_exists('app_path')) {
- /**
- * Get the path to the application folder.
- *
- * @param string $path
- * @return string
- */
- function app_path($path = '')
- {
- $appPath = BASE_PATH . DIRECTORY_SEPARATOR . 'app';
- return $appPath.($path ? DIRECTORY_SEPARATOR.$path : $path);
- }
- }
- if (! function_exists('public_path')) {
- /**
- * Get the path to the public folder.
- *
- * @param string $path
- * @return string
- */
- function public_path($path = '')
- {
- $publicPath = BASE_PATH . DIRECTORY_SEPARATOR . 'public';
- return $publicPath.($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path);
- }
- }
- if (! function_exists('logger')) {
- /**
- * 日志记录.
- * @param string $message
- * @param array $context
- * @param string $level
- */
- function logger(string $message, array $context = [], $level = 'info')
- {
- $logger = make(\Hyperf\Logger\LoggerFactory::class);
- $logObj = $logger->get('log', 'default');
- if ($level != 'log' && method_exists($logObj, $level)) {
- $logObj->{$level}($message, $context);
- }
- }
- }
- if (! function_exists('payLogger')) {
- /**
- * @param string 支付通道
- * @param string 信息
- * @param array 支付错误内容
- * @param string $level
- */
- function payLogger(string $payName,string $message = '请求异常', array $context = [], $level = 'info')
- {
- $payLogger = make(\Hyperf\Logger\LoggerFactory::class);
- $payLogObj = $payLogger->make('pay','pay');
- if ($level != 'pay' && method_exists($payLogObj, $level)) {
- $payLogObj->{$level}("支付通道(".$payName.'), 错误信息:('.$message.')', $context);
- }
- dispatch_job(new \App\Jobs\SendTgMegJob($payName,$message,$context));
- }
- }
- if (! function_exists('dispatch_job')) {
- /**
- * Dispatch a job to its appropriate handler.
- *
- * @param mixed $job
- * @param int $delay
- * @param string $queueName
- * @return bool
- */
- function dispatch_job($job, $delay = 0, $queueName = 'default') : bool
- {
- $driverFactory = app(\Hyperf\AsyncQueue\Driver\DriverFactory::class);
- $driver = $driverFactory->get($queueName);
- return $driver->push($job, $delay);
- }
- }
- if (! function_exists('dispatch_event')) {
- /**
- * 触发具体事件.
- * @param object $event
- * @return object
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- function dispatch_event(object $event)
- {
- $eventDispatcher = ApplicationContext::getContainer()->get(EventDispatcherInterface::class);
- return $eventDispatcher->dispatch($event);
- }
- }
|