AdminEditorTokenMiddleware.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\exceptions\AuthException;
  15. use crmeb\interfaces\MiddlewareInterface;
  16. use think\facade\Config;
  17. /**
  18. * 后台登陆验证中间件
  19. * Class AdminAuthTokenMiddleware
  20. * @package app\adminapi\middleware
  21. */
  22. class AdminEditorTokenMiddleware implements MiddlewareInterface
  23. {
  24. public function handle(Request $request, \Closure $next)
  25. {
  26. $authInfo = null;
  27. $token = trim(ltrim($request->header(Config::get('cookie.token_name', 'Authori-zation')), 'Bearer'));
  28. /** @var AdminAuthServices $service */
  29. $service = app()->make(AdminAuthServices::class);
  30. $adminInfo = $service->parseToken($token,110008);
  31. if($adminInfo['type'] !== 'admin_edit')
  32. {
  33. throw new AuthException('登陆异常,请重新登录',[],110008);
  34. }
  35. Request::macro('isAdminLogin', function () use (&$adminInfo) {
  36. return !is_null($adminInfo);
  37. });
  38. Request::macro('adminId', function () use (&$adminInfo) {
  39. return $adminInfo['id'];
  40. });
  41. Request::macro('adminInfo', function () use (&$adminInfo) {
  42. return $adminInfo;
  43. });
  44. return $next($request);
  45. }
  46. }