CheckLogin.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Model\SystemRecord;
  4. use App\Service\EmployeeService;
  5. use Closure;
  6. use App\Service\TokenService;
  7. class CheckLogin
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @param \Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. $token=$request->header('Authorization');
  19. if(empty($token)) return response()->json(['code'=>1,'msg'=>'缺少登录凭证','data'=>null]);
  20. //校验token
  21. $result = TokenService::verifyToken($token);
  22. if ($result < 0){
  23. return response()->json(['code'=>1,'msg'=>TokenService::error[$result],'data'=>null]);
  24. }
  25. //校验用户
  26. $checkResult = EmployeeService::checkUser($result);
  27. list($state, $data) = $checkResult;
  28. if(! $state) return response()->json(['code'=>1,'msg'=>$data,'data'=>null]);
  29. //人员角色
  30. $data['role'] = EmployeeService::getPersonRole($result);
  31. //部门权限
  32. $data['rule_depart'] = EmployeeService::getPersonDepart($result);
  33. //写入user信息
  34. $request->userData = $data;
  35. //系统操作日志
  36. $this->insert($request->path(),$data);
  37. return $next($request);
  38. }
  39. public function insert($uri,$data){
  40. if (getenv('HTTP_CLIENT_IP')) {
  41. $ip = getenv('HTTP_CLIENT_IP');
  42. }
  43. elseif (getenv('HTTP_X_REAL_IP')) {
  44. $ip = getenv('HTTP_X_REAL_IP');
  45. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  46. $ip = getenv('HTTP_X_FORWARDED_FOR');
  47. $ips = explode(',', $ip);
  48. $ip = $ips[0];
  49. } elseif (getenv('REMOTE_ADDR')) {
  50. $ip = getenv('REMOTE_ADDR');
  51. } else {
  52. $ip = '0.0.0.0';
  53. }
  54. $map = config("routemap.map");
  55. if(isset($map[$uri])){
  56. $content = $map[$uri];
  57. $account = $data['account'];
  58. SystemRecord::insert([
  59. 'account' => $account,
  60. 'crt_time' => time(),
  61. 'ip' => $ip,
  62. 'content' => $content
  63. ]);
  64. }
  65. }
  66. }