AuthTokenMiddleware.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\http\middleware;
  3. use app\models\user\User;
  4. use app\models\user\UserLevel;
  5. use app\models\user\UserSpread;
  6. use app\models\user\UserToken;
  7. use app\Request;
  8. use crmeb\exceptions\AuthException;
  9. use crmeb\interfaces\MiddlewareInterface;
  10. use crmeb\repositories\UserRepository;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\exception\DbException;
  14. /**
  15. * token验证中间件
  16. * Class AuthTokenMiddleware
  17. * @package app\http\middleware
  18. */
  19. class AuthTokenMiddleware implements MiddlewareInterface
  20. {
  21. public function handle(Request $request, \Closure $next, bool $force = true)
  22. {
  23. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  24. $authInfo = null;
  25. $token = trim(ltrim($request->header('Authori-zation'), 'Bearer'));
  26. if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版,删除此行,某些服务器无法获取到token调整为 Authori-zation
  27. try {
  28. $authInfo = UserRepository::parseToken($token);
  29. if (!is_null($authInfo)) {
  30. $authInfo['user']['lock_spread_user'] = User::where('uid', $authInfo['user']->spread_uid)->field('nickname,phone,avatar')->find();
  31. $authInfo['user']['level_info'] = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($authInfo['user']->uid));
  32. if (!$authInfo['user']['lock_spread_user']) {
  33. $spread_user = UserSpread::where('uid', $authInfo['user']->uid)->order('spread_time', 'desc')->find();
  34. if ($spread_user) {
  35. $sp = $spread_user['spread_uid'];
  36. $userlist = User::column('uid,spread_uid', 'uid');
  37. while ($sp) {
  38. if ($sp == $authInfo['user']->uid) {
  39. $spread_user['spread_uid'] = 0;
  40. break;
  41. }
  42. $sp = $userlist[$sp]['spread_uid'];
  43. }
  44. if (!$spread_user['spread_uid']) {
  45. $authInfo['user']['unlock_spread_user'] = null;
  46. } else {
  47. $authInfo['user']['unlock_spread_user'] = User::where('uid', $spread_user['spread_uid'])->field('nickname,phone,avatar')->find();
  48. }
  49. } else {
  50. $authInfo['user']['unlock_spread_user'] = null;
  51. }
  52. } else {
  53. $authInfo['user']['unlock_spread_user'] = null;
  54. }
  55. }
  56. } catch (AuthException $e) {
  57. if ($force)
  58. return app('json')->make($e->getCode(), $e->getMessage());
  59. }
  60. if (!is_null($authInfo)) {
  61. Request::macro('user', function () use (&$authInfo) {
  62. return $authInfo['user'];
  63. });
  64. Request::macro('tokenData', function () use (&$authInfo) {
  65. return $authInfo['tokenData'];
  66. });
  67. }
  68. Request::macro('isLogin', function () use (&$authInfo) {
  69. return !is_null($authInfo);
  70. });
  71. Request::macro('uid', function () use (&$authInfo) {
  72. return is_null($authInfo) ? 0 : $authInfo['user']->uid;
  73. });
  74. return $next($request);
  75. }
  76. }