WebLogin.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Helpers\Jwt;
  4. use App\Services\RedisService;
  5. use App\Services\ConfigService;
  6. use Closure;
  7. use Illuminate\Auth\Middleware\Authenticate as Middleware;
  8. class WebLogin extends Middleware
  9. {
  10. /**
  11. * 执行句柄
  12. * @param \Illuminate\Http\Request $request
  13. * @param Closure $next
  14. * @param mixed ...$guards
  15. * @return mixed
  16. * @throws \Illuminate\Auth\AuthenticationException
  17. * @since 2020/8/31
  18. * @author wesmiler
  19. */
  20. public function handle($request, Closure $next, ...$guards)
  21. {
  22. $action = app('request')->route()->getAction();
  23. $controller = class_basename($action['controller']);
  24. list($controller, $action) = explode('@', $controller);
  25. // $noLoginActs = ['LoginController','TestController','NotifyController','IndexController','ArticleController','UploadController','TaskController'];
  26. // $noSignActions = ['UploadController','setAvatar','NotifyController','TestController','TaskController'];
  27. $token = $request->headers->get('Authorization');
  28. if (strpos($token, 'Bearer ') !== false) {
  29. $token = str_replace("Bearer ", null, $token);
  30. $token = trim($token);
  31. if($token == 'app123'){
  32. $userId = ConfigService::make()->getConfigByCode('test_uid');
  33. $userId = $userId? $userId : 0;
  34. }else{
  35. // JWT解密token
  36. $jwt = new Jwt('jwt_rrc_app');
  37. $userId = $jwt->verifyToken($token);
  38. }
  39. } else {
  40. $userId = 0;
  41. }
  42. // 接口验证
  43. $sign = $request->headers->get('sign');
  44. $params = $request->except('s');
  45. $checkSign = getSign($params);
  46. if($sign != 'test6688') {
  47. if ($sign != $checkSign) {
  48. return response()->json(message(1005, false, [], 403))->setEncodingOptions(256);
  49. }
  50. $time = isset($params['time']) ? $params['time'] : 0;
  51. if ($time && $time < time() - 20) {
  52. return response()->json(message(1012, false, null, 403))->setEncodingOptions(256);
  53. }
  54. }
  55. if (!$userId && !in_array($controller,['LoginController','IndexController'])) {
  56. // 判断用户未登录就跳转至登录页面
  57. // 在这里可以定制你想要的返回格式, 亦或者是 JSON 编码格式
  58. return response()->json(message(1004, false, [], 403))->setEncodingOptions(256);
  59. }
  60. $request->headers->set('token_uid' , $userId);
  61. //如果已登录则执行正常的请求
  62. return $next($request);
  63. }
  64. }