SystemLog.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\middleware;
  8. use Webman\Event\Event;
  9. use Webman\Http\Request;
  10. use Webman\Http\Response;
  11. use Webman\MiddlewareInterface;
  12. use plugin\saiadmin\exception\ApiException;
  13. use plugin\saiadmin\app\cache\ReflectionCache;
  14. class SystemLog implements MiddlewareInterface
  15. {
  16. /**
  17. * @param Request $request
  18. * @param callable $handler
  19. * @return Response
  20. */
  21. public function process(Request $request, callable $handler): Response
  22. {
  23. // 通过反射获取控制器哪些方法不需要登录
  24. $noNeedLogin = ReflectionCache::getNoNeedLogin($request->controller);
  25. // 访问的方法需要登录
  26. if (!in_array($request->action, $noNeedLogin)) {
  27. try {
  28. // 记录日志
  29. Event::emit('user.operateLog', true);
  30. } catch (\Throwable $e) {
  31. throw new ApiException('登录凭获取失败,请检查');
  32. }
  33. }
  34. return $handler($request);
  35. }
  36. }