AuthTokenMiddleware.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\http\middleware;
  3. use app\admin\model\system\SystemAdmin;
  4. use app\models\system\SystemStore;
  5. use app\models\system\SystemStoreArea;
  6. use app\models\user\User;
  7. use app\models\user\UserToken;
  8. use app\Request;
  9. use crmeb\exceptions\AuthException;
  10. use crmeb\interfaces\MiddlewareInterface;
  11. use crmeb\repositories\UserRepository;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\exception\DbException;
  15. /**
  16. * token验证中间件
  17. * Class AuthTokenMiddleware
  18. * @package app\http\middleware
  19. */
  20. class AuthTokenMiddleware implements MiddlewareInterface
  21. {
  22. public function handle(Request $request, \Closure $next, bool $force = true)
  23. {
  24. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  25. $authInfo = null;
  26. // var_dump($request->action());
  27. // var_dump($request->controller());
  28. $token = trim(ltrim($request->header('Authori-zation'), 'Bearer'));
  29. $location = $request->header('LatLon', '0,0');
  30. if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版,删除此行,某些服务器无法获取到token调整为 Authori-zation
  31. try {
  32. $authInfo = UserRepository::parseToken($token);
  33. $authInfo['user']['store_info'] = [];
  34. if ($authInfo['user']->admin_id) {
  35. $adminInfo = SystemAdmin::get($authInfo['user']->admin_id);
  36. if (!$adminInfo || !$adminInfo['status']) {
  37. $adminInfo = [];
  38. } else {
  39. $adminInfo = $adminInfo->toArray();
  40. if (in_array(sys_config('default_store_admin', 7), explode(',', $adminInfo['roles']))) {
  41. $authInfo['user']['store_info'] = SystemStore::verificWhere()->where('id', $adminInfo['store_id'])->find();
  42. }
  43. // $adminInfo['auth'] = $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']);
  44. }
  45. } else {
  46. $adminInfo = [];
  47. }
  48. } catch (AuthException $e) {
  49. if ($force)
  50. return app('json')->make($e->getCode(), $e->getMessage());
  51. }
  52. if (!is_null($authInfo)) {
  53. Request::macro('user', function () use (&$authInfo) {
  54. return $authInfo['user'];
  55. });
  56. Request::macro('tokenData', function () use (&$authInfo) {
  57. return $authInfo['tokenData'];
  58. });
  59. }
  60. Request::macro('location', function () use ($location) {
  61. return $location;
  62. });
  63. Request::macro('isLogin', function () use (&$authInfo) {
  64. return !is_null($authInfo);
  65. });
  66. Request::macro('admin_info', function () use (&$adminInfo) {
  67. return $adminInfo;
  68. });
  69. Request::macro('uid', function () use (&$authInfo) {
  70. return is_null($authInfo) ? 0 : $authInfo['user']->uid;
  71. });
  72. return $next($request);
  73. }
  74. }