AuthTokenMiddleware.php 1.8 KB

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