AuthTokenMiddleware.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\cashier;
  12. use app\Request;
  13. use app\services\cashier\LoginServices;
  14. use crmeb\interfaces\MiddlewareInterface;
  15. use think\facade\Config;
  16. /**
  17. * Class AuthTokenMiddleware
  18. * @package app\http\middleware\store
  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('storeId', function () use (&$outInfo) {
  37. return (int)$outInfo['store_id'];
  38. });
  39. $request->macro('cashierId', function () use (&$outInfo) {
  40. return (int)$outInfo['id'];
  41. });
  42. $request->macro('cashierInfo', function () use (&$outInfo) {
  43. return $outInfo;
  44. });
  45. return $next($request);
  46. }
  47. }