AuthTokenMiddleware.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\http\middleware;
  3. use app\models\point_plan\UserPointPlan;
  4. use app\models\system\Site;
  5. use app\models\user\Achievement;
  6. use app\models\user\User;
  7. use app\models\user\UserLevel;
  8. use app\models\user\UserToken;
  9. use app\Request;
  10. use crmeb\exceptions\AuthException;
  11. use crmeb\interfaces\MiddlewareInterface;
  12. use crmeb\repositories\UserRepository;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\exception\DbException;
  16. /**
  17. * token验证中间件
  18. * Class AuthTokenMiddleware
  19. * @package app\http\middleware
  20. */
  21. class AuthTokenMiddleware implements MiddlewareInterface
  22. {
  23. public function handle(Request $request, \Closure $next, bool $force = true)
  24. {
  25. // var_dump($request->site_id());
  26. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  27. $authInfo = null;
  28. $token = trim(ltrim($request->header('Authori-zation'), 'Bearer'));
  29. // if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版,删除此行,某些服务器无法获取到token调整为 Authori-zation
  30. try {
  31. $authInfo = UserRepository::parseToken($token, $request->site_id());
  32. // if (!in_array($request->action(), ['binding_phone', 'user', 'userInfo', 'verify']) && ($authInfo['user']['account'] != $authInfo['user']['phone'] && $authInfo['user']['account'] != $authInfo['user']['email'])) {
  33. // return app('json')->make('400', '请先绑定手机号或邮箱');
  34. // }
  35. $authInfo['user']['level_info'] = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($authInfo['user']['uid']));
  36. $authInfo['user']['point'] = UserPointPlan::where('uid', $authInfo['user']['uid'])->select();
  37. $authInfo['user']['all_achievement'] = Achievement::where('uid', $authInfo['user']['uid'])->sum('achievement');
  38. $authInfo['user']['self_achievement'] = Achievement::where('uid', $authInfo['user']['uid'])->where('from_uid', $authInfo['user']['uid'])->sum('achievement');
  39. $authInfo['user']['new_achievement'] = Achievement::where('uid', $authInfo['user']['uid'])->where('do', 0)->sum('achievement');
  40. } catch (AuthException $e) {
  41. if ($force)
  42. return app('json')->make($e->getCode(), $e->getMessage());
  43. }
  44. if (!is_null($authInfo)) {
  45. Request::macro('user', function () use (&$authInfo) {
  46. return $authInfo['user'];
  47. });
  48. Request::macro('tokenData', function () use (&$authInfo) {
  49. return $authInfo['tokenData'];
  50. });
  51. }
  52. Request::macro('isLogin', function () use (&$authInfo) {
  53. return !is_null($authInfo);
  54. });
  55. Request::macro('uid', function () use (&$authInfo) {
  56. return is_null($authInfo) ? 0 : $authInfo['user']->uid;
  57. });
  58. return $next($request);
  59. }
  60. }