AuthTokenMiddleware.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\http\middleware;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\UserToken;
  6. use app\Request;
  7. use crmeb\exceptions\AuthException;
  8. use crmeb\interfaces\MiddlewareInterface;
  9. use crmeb\repositories\UserRepository;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\db\exception\DbException;
  13. /**
  14. * token验证中间件
  15. * Class AuthTokenMiddleware
  16. * @package app\http\middleware
  17. */
  18. class AuthTokenMiddleware implements MiddlewareInterface
  19. {
  20. public function handle(Request $request, \Closure $next, bool $force = true)
  21. {
  22. $request->filter(['htmlspecialchars', 'strip_tags', 'addslashes', 'trim']);
  23. $authInfo = null;
  24. $token = trim(ltrim($request->header('Authori-zation'), 'Bearer'));
  25. if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版,删除此行,某些服务器无法获取到token调整为 Authori-zation
  26. try {
  27. $authInfo = UserRepository::parseToken($token);
  28. if (!is_null($authInfo)) {
  29. // $sub_uid = $request->header('UID', 0);
  30. // if ($authInfo['user']['main_uid'] == 0) {
  31. // $check = function ($item) use ($authInfo) {
  32. // return User::getUserInfo($item)['main_uid'] == $authInfo['user']['uid'] || User::getUserInfo($item)['uid'] == $authInfo['user']['uid'];
  33. // };
  34. // } else {
  35. // $check = function ($item) use ($authInfo) {
  36. // return (User::getUserInfo($item)['main_uid'] == $authInfo['user']['main_uid']) || User::getUserInfo($item)['uid'] == $authInfo['user']['main_uid'];
  37. // };
  38. // }
  39. // if ($sub_uid) {
  40. // if ($check($sub_uid)) {
  41. // $authInfo['user'] = User::where('uid', $sub_uid)->find();
  42. // }
  43. // $main_user = User::getUserInfo($authInfo['user']['main_uid'] ?: $authInfo['user']['uid']);
  44. // if ($main_user['real_check'] == 1) {
  45. // $authInfo['user']['real_check'] = 1;
  46. // $authInfo['user']['real_name'] = $main_user['real_name'];
  47. // $authInfo['user']['card_id'] = $main_user['card_id'];
  48. // }
  49. // }
  50. if (!$authInfo['user']['real_check'] && !in_array($request->action(), ['realNameCheck', 'logout', 'user', 'userinfo', 'upload_image', 'image'])) {
  51. throw new AuthException('请先进行实名认证');
  52. }
  53. if (!$authInfo['user']->is_promoter) {
  54. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $authInfo['user']['uid']])->sum('pay_price');
  55. $status = is_brokerage_statu($price);
  56. } else {
  57. $status = false;
  58. }
  59. User::edit(['is_promoter' => $status ? 1 : $authInfo['user']->is_promoter], $authInfo['user']['uid'], 'uid');
  60. $authInfo['user']->is_promoter = $status ? 1 : $authInfo['user']->is_promoter;
  61. }
  62. } catch (AuthException $e) {
  63. if ($force)
  64. return app('json')->make($e->getCode(), $e->getMessage());
  65. }
  66. if (!is_null($authInfo)) {
  67. Request::macro('user', function () use (&$authInfo) {
  68. return $authInfo['user'];
  69. });
  70. Request::macro('tokenData', function () use (&$authInfo) {
  71. return $authInfo['tokenData'];
  72. });
  73. }
  74. Request::macro('isLogin', function () use (&$authInfo) {
  75. return !is_null($authInfo);
  76. });
  77. Request::macro('uid', function () use (&$authInfo) {
  78. return is_null($authInfo) ? 0 : $authInfo['user']->uid;
  79. });
  80. Request::macro('isReal', function () use (&$authInfo) {
  81. return !is_null($authInfo) ? 0 : $authInfo['user']->real_check;
  82. });
  83. return $next($request);
  84. }
  85. }