AdminAuthTokenMiddleware.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\adminapi\middleware;
  12. use app\Request;
  13. use app\services\system\admin\AdminAuthServices;
  14. use crmeb\interfaces\MiddlewareInterface;
  15. use think\facade\Config;
  16. use crmeb\services\CacheService;
  17. /**
  18. * 后台登陆验证中间件
  19. * Class AdminAuthTokenMiddleware
  20. * @package app\adminapi\middleware
  21. */
  22. class AdminAuthTokenMiddleware implements MiddlewareInterface
  23. {
  24. /**
  25. * @param Request $request
  26. * @param \Closure $next
  27. * @return mixed
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @author 吴汐
  32. * @email 442384644@qq.com
  33. * @date 2023/04/07
  34. */
  35. public function handle(Request $request, \Closure $next)
  36. {
  37. $token = trim(ltrim($request->header(Config::get('cookie.token_name', 'Authori-zation')), 'Bearer'));
  38. if (!$token) {
  39. $token = trim(ltrim($request->get('token')));
  40. }
  41. /** @var AdminAuthServices $service */
  42. $service = app()->make(AdminAuthServices::class);
  43. $adminInfo = $service->parseToken($token);
  44. $request->macro('isAdminLogin', function () use (&$adminInfo) {
  45. return !is_null($adminInfo);
  46. });
  47. $request->macro('adminId', function () use (&$adminInfo) {
  48. return $adminInfo['id'];
  49. });
  50. $request->macro('adminInfo', function () use (&$adminInfo) {
  51. return $adminInfo;
  52. });
  53. return $next($request);
  54. }
  55. }