KefuAuthTokenMiddleware.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\kefuapi\middleware;
  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. * @return mixed
  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 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. }