12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Auth\GenericUser;
- use Illuminate\Support\Facades\Auth;
- use App\Facades\UserFacade;
- class CheckToken
- {
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle($request, Closure $next)
- {
- $typeName = 'api用户登陆验证 ';
- $startTime = microtime(TRUE);
- UserFacade::setLog($typeName.'开始', $startTime, ['token'=>$request->header('token')]);
- $token = $request->header('token');
- try {
- $user = UserFacade::findLoginBy($token);
- Auth::login(new GenericUser(
- array(
- 'id'=>$user->getData()->data->uid,
- 'password'=>"123456",
- 'remember_token'=>$token
- )
- ));
- UserFacade::setLog($typeName.'查找用户结果', $startTime, [$user->getData()]);
- } catch (\Exception $e) {
- $auth = [
- 'code' => 1006,
- 'data' => [
- 'ip' => $request->ip(),
- 'token' => $request->header('token')
- ],
- 'msg' => '登陆验证失败,重新登陆'
- ];
- UserFacade::setLog($typeName.'失败', $startTime, [$auth]);
- return response()->json($auth);
- }
- return $next($request);
- }
- }
|