WebLogin.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $locale = session('locale_lang');
  44. if($userId>0){
  45. $locale = RedisService::get("caches:locale:lang_{$userId}");
  46. $locale = $locale? $locale : session('locale_lang');
  47. if($locale){
  48. session(['locale_lang'=>$locale]);
  49. app()->setLocale($locale);
  50. }
  51. }else if ($locale){
  52. session(['locale_lang'=>$locale]);
  53. app()->setLocale($locale);
  54. }
  55. // 接口验证
  56. $sign = $request->headers->get('sign');
  57. $params = $request->except('s');
  58. $checkSign = getSign($params);
  59. if($sign != 'test6688' && !in_array($action, $noSignActions) && !in_array($controller, $noSignActions)) {
  60. // if ($sign != $checkSign) {
  61. // return response()->json(message(1005, false, [], 403))->setEncodingOptions(256);
  62. // }
  63. $time = isset($params['time']) ? $params['time'] : 0;
  64. if ($time && $time < time() - 20) {
  65. return response()->json(message(1012, false, null, 403))->setEncodingOptions(256);
  66. }
  67. }
  68. if (!$userId && !in_array($controller, $noLoginActs)) {
  69. // 判断用户未登录就跳转至登录页面
  70. // 在这里可以定制你想要的返回格式, 亦或者是 JSON 编码格式
  71. return response()->json(message(1004, false, [], 403))->setEncodingOptions(256);
  72. }
  73. $request->headers->set('token_uid' , $userId);
  74. //如果已登录则执行正常的请求
  75. return $next($request);
  76. }
  77. }