12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Http\Middleware;
- use App\Model\SystemRecord;
- use App\Service\EmployeeService;
- use Closure;
- use App\Service\TokenService;
- class CheckLogin
- {
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle($request, Closure $next)
- {
- $token=$request->header('Authorization');
- if (!isset($token)){
- return response()->json(['code'=>1,'msg'=>'缺少token','data'=>null]);
- }
- //校验token
- $result = TokenService::verifyToken($token);
- if ($result < 0){
- return response()->json(['code'=>1,'msg'=>TokenService::error[$result],'data'=>null]);
- }
- //校验用户
- $checkResult = EmployeeService::checkUser($result);
- list($state, $data) = $checkResult;
- if(! $state) return response()->json(['code'=>1,'msg'=>$data,'data'=>null]);
- //人员角色
- $data['role'] = EmployeeService::getPersonRole($result);
- //部门权限
- $data['rule_depart'] = EmployeeService::getPersonDepart($result);
- //写入user信息
- $request->userData = $data;
- //系统操作日志
- $this->insert($request->path(),$data);
- return $next($request);
- }
- public function insert($uri,$data){
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- }
- elseif (getenv('HTTP_X_REAL_IP')) {
- $ip = getenv('HTTP_X_REAL_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- $ips = explode(',', $ip);
- $ip = $ips[0];
- } elseif (getenv('REMOTE_ADDR')) {
- $ip = getenv('REMOTE_ADDR');
- } else {
- $ip = '0.0.0.0';
- }
- $map = config("routemap.map");
- if(isset($map[$uri])){
- $content = $map[$uri];
- $account = $data['account'];
- SystemRecord::insert([
- 'account' => $account,
- 'crt_time' => time(),
- 'ip' => $ip,
- 'content' => $content
- ]);
- }
- }
- }
|