AuthTokenMiddleware.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $store_id = $request->header('store-id', 0);
  30. $location = $request->header('LatLon', '0,0');
  31. if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版,删除此行,某些服务器无法获取到token调整为 Authori-zation
  32. try {
  33. $authInfo = UserRepository::parseToken($token);
  34. $authInfo['user']['store_info'] = [];
  35. if ($authInfo['user']->admin_id) {
  36. $adminInfo = SystemAdmin::get($authInfo['user']->admin_id);
  37. if (!$adminInfo || !$adminInfo['status']) {
  38. $adminInfo = [];
  39. } else {
  40. $adminInfo = $adminInfo->toArray();
  41. if (in_array(sys_config('default_store_admin', 7), explode(',', $adminInfo['roles']))) {
  42. $authInfo['user']['store_info'] = SystemStore::verificWhere()->where('id', $adminInfo['store_id'])->find();
  43. }
  44. // $adminInfo['auth'] = $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']);
  45. }
  46. } else {
  47. $adminInfo = [];
  48. }
  49. } catch (AuthException $e) {
  50. if ($force)
  51. return app('json')->make($e->getCode(), $e->getMessage());
  52. }
  53. if (!is_null($authInfo)) {
  54. Request::macro('user', function () use (&$authInfo) {
  55. return $authInfo['user'];
  56. });
  57. Request::macro('tokenData', function () use (&$authInfo) {
  58. return $authInfo['tokenData'];
  59. });
  60. }
  61. Request::macro('location', function () use ($location) {
  62. return $location;
  63. });
  64. Request::macro('isLogin', function () use (&$authInfo) {
  65. return !is_null($authInfo);
  66. });
  67. Request::macro('admin_info', function () use (&$adminInfo) {
  68. return $adminInfo;
  69. });
  70. Request::macro('store_id', function () use (&$store_id) {
  71. return $store_id;
  72. });
  73. Request::macro('uid', function () use (&$authInfo) {
  74. return is_null($authInfo) ? 0 : $authInfo['user']->uid;
  75. });
  76. return $next($request);
  77. }
  78. }