KefuAuthTokenMiddleware.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\kefu;
  12. use app\Request;
  13. use app\services\kefu\LoginServices;
  14. use crmeb\interfaces\MiddlewareInterface;
  15. use think\facade\Config;
  16. /**
  17. * Class KefuAuthTokenMiddleware
  18. * @package app\kefu\middleware
  19. */
  20. class KefuAuthTokenMiddleware 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. $authInfo = null;
  33. $token = trim(ltrim($request->header(Config::get('cookie.token_name', 'Authori-zation')), 'Bearer'));
  34. /** @var LoginServices $services */
  35. $services = app()->make(LoginServices::class);
  36. $kefuInfo = $services->parseToken($token);
  37. $request->macro('kefuId', function () use (&$kefuInfo) {
  38. return (int)$kefuInfo['id'];
  39. });
  40. $request->macro('kefuInfo', function () use (&$kefuInfo) {
  41. return $kefuInfo;
  42. });
  43. return $next($request);
  44. }
  45. }