helpers.func.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2019-11-01 15:05:31
  7. */
  8. use Hyperf\Contract\StdoutLoggerInterface;
  9. use Hyperf\Utils\ApplicationContext;
  10. use Psr\EventDispatcher\EventDispatcherInterface;
  11. if (! function_exists('app')) {
  12. /**
  13. * Get the available container instance.
  14. *
  15. * @param string $abstract
  16. * @param array $parameters
  17. * @return mixed|\Psr\Container\ContainerInterface
  18. */
  19. function app($abstract = null, array $parameters = [])
  20. {
  21. if (is_null($abstract)) {
  22. if (\Hyperf\Utils\ApplicationContext::hasContainer()) {
  23. return \Hyperf\Utils\ApplicationContext::getContainer();
  24. }
  25. }
  26. return make($abstract, $parameters);
  27. }
  28. }
  29. if (! function_exists('response')) {
  30. /**
  31. * Return a new response from the application.
  32. *
  33. * @param string $content
  34. * @param int $status
  35. * @param array $headers
  36. * @return Psr\Http\Message\ResponseInterface
  37. */
  38. function response($content = '', $status = 200, array $headers = []) : \Psr\Http\Message\ResponseInterface
  39. {
  40. $factory = app(Hyperf\HttpServer\Contract\ResponseInterface::class);
  41. if (func_num_args() === 0) {
  42. return $factory;
  43. }
  44. $res = $factory->withStatus($status);
  45. if (is_array($headers)) {
  46. foreach ($headers as $k => $v) {
  47. $res = $res->withHeader($k, $v);
  48. }
  49. }
  50. return $res->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream((string) $content));
  51. }
  52. }
  53. if (! function_exists('request')) {
  54. /**
  55. * Get an instance of the current request or an input item from the request.
  56. *
  57. * @param array|string $key
  58. * @param mixed $default
  59. * @return \Hyperf\HttpServer\Contract\RequestInterface|string|array
  60. */
  61. function request($key = null, $default = null)
  62. {
  63. if (is_null($key)) {
  64. return make(\Hyperf\HttpServer\Contract\RequestInterface::class);
  65. }
  66. if (is_array($key)) {
  67. $value = make(\Hyperf\HttpServer\Contract\RequestInterface::class)->inputs($key);
  68. } else {
  69. $value = app(\Hyperf\HttpServer\Contract\RequestInterface::class)->input($key);
  70. }
  71. return is_null($value) ? value($default) : $value;
  72. }
  73. }
  74. if (! function_exists('collect')) {
  75. /**
  76. * Create a collection from the given value.
  77. *
  78. * @param mixed $value
  79. * @return \Hyperf\Utils\Collection
  80. */
  81. function collect($value = null)
  82. {
  83. return new \Hyperf\Utils\Collection($value);
  84. }
  85. }
  86. if (! function_exists('base_path')) {
  87. /**
  88. * Get the path to the base of the install.
  89. *
  90. * @param string $path
  91. * @return string
  92. */
  93. function base_path($path = '')
  94. {
  95. return BASE_PATH.($path ? DIRECTORY_SEPARATOR.$path : $path);
  96. }
  97. }
  98. if (! function_exists('app_path')) {
  99. /**
  100. * Get the path to the application folder.
  101. *
  102. * @param string $path
  103. * @return string
  104. */
  105. function app_path($path = '')
  106. {
  107. $appPath = BASE_PATH . DIRECTORY_SEPARATOR . 'app';
  108. return $appPath.($path ? DIRECTORY_SEPARATOR.$path : $path);
  109. }
  110. }
  111. if (! function_exists('public_path')) {
  112. /**
  113. * Get the path to the public folder.
  114. *
  115. * @param string $path
  116. * @return string
  117. */
  118. function public_path($path = '')
  119. {
  120. $publicPath = BASE_PATH . DIRECTORY_SEPARATOR . 'public';
  121. return $publicPath.($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path);
  122. }
  123. }
  124. if (! function_exists('logger')) {
  125. /**
  126. * 日志记录.
  127. * @param string $message
  128. * @param array $context
  129. * @param string $level
  130. */
  131. function logger(string $message, array $context = [], $level = 'info')
  132. {
  133. $logger = make(\Hyperf\Logger\LoggerFactory::class);
  134. $logObj = $logger->get('log', 'default');
  135. if ($level != 'log' && method_exists($logObj, $level)) {
  136. $logObj->{$level}($message, $context);
  137. }
  138. }
  139. }
  140. if (! function_exists('payLogger')) {
  141. /**
  142. * @param string 支付通道
  143. * @param string 信息
  144. * @param array 支付错误内容
  145. * @param string $level
  146. */
  147. function payLogger(string $payName,string $message = '请求异常', array $context = [], $level = 'info')
  148. {
  149. $payLogger = make(\Hyperf\Logger\LoggerFactory::class);
  150. $payLogObj = $payLogger->make('pay','pay');
  151. if ($level != 'pay' && method_exists($payLogObj, $level)) {
  152. $payLogObj->{$level}("支付通道(".$payName.'), 错误信息:('.$message.')', $context);
  153. }
  154. dispatch_job(new \App\Jobs\SendTgMegJob($payName,$message,$context));
  155. }
  156. }
  157. if (! function_exists('dispatch_job')) {
  158. /**
  159. * Dispatch a job to its appropriate handler.
  160. *
  161. * @param mixed $job
  162. * @param int $delay
  163. * @param string $queueName
  164. * @return bool
  165. */
  166. function dispatch_job($job, $delay = 0, $queueName = 'default') : bool
  167. {
  168. $driverFactory = app(\Hyperf\AsyncQueue\Driver\DriverFactory::class);
  169. $driver = $driverFactory->get($queueName);
  170. return $driver->push($job, $delay);
  171. }
  172. }
  173. if (! function_exists('dispatch_event')) {
  174. /**
  175. * 触发具体事件.
  176. * @param object $event
  177. * @return object
  178. * @throws \Psr\Container\ContainerExceptionInterface
  179. * @throws \Psr\Container\NotFoundExceptionInterface
  180. */
  181. function dispatch_event(object $event)
  182. {
  183. $eventDispatcher = ApplicationContext::getContainer()->get(EventDispatcherInterface::class);
  184. return $eventDispatcher->dispatch($event);
  185. }
  186. }