AuthTokenMiddleware.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\outapi\middleware;
  12. use app\Request;
  13. use app\services\out\OutAccountServices;
  14. use app\services\out\OutInterfaceServices;
  15. use crmeb\interfaces\MiddlewareInterface;
  16. use think\facade\Config;
  17. /**
  18. * Class AuthTokenMiddleware
  19. * @package app\outapi\middleware
  20. */
  21. class AuthTokenMiddleware implements MiddlewareInterface
  22. {
  23. /**
  24. * @param Request $request
  25. * @param \Closure $next
  26. * @throws \Psr\SimpleCache\InvalidArgumentException
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function handle(Request $request, \Closure $next)
  32. {
  33. $token = trim(ltrim($request->header(Config::get('cookie.token_name', 'Authori-zation')), 'Bearer'));
  34. /** @var OutAccountServices $services */
  35. $services = app()->make(OutAccountServices::class);
  36. $outInfo = $services->parseToken($token);
  37. $request->macro('outId', function () use (&$outInfo) {
  38. return (int)$outInfo['id'];
  39. });
  40. $request->macro('outInfo', function () use (&$outInfo) {
  41. return $outInfo;
  42. });
  43. /** @var OutInterfaceServices $outInterfaceServices */
  44. $outInterfaceServices = app()->make(OutInterfaceServices::class);
  45. $outInterfaceServices->verifyAuth($request);
  46. return $next($request);
  47. }
  48. }