<?php

namespace App\Http\Middleware;

use App\Modules\User\Services\QappUserService;
use Closure;
use Exception;
use Tymon\JWTAuth\Middleware\BaseMiddleware;

class QuickAppUserStatus extends BaseMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 判断是否登录
        if (!$token = $this->auth->setRequest($request)->getToken()) {
            return response()->error('QAPP_NOT_LOGIN');
        }
        try {
            //获取用户信息
            $user = $this->auth->authenticate($token);
            //根据uid判断用户状态 0为已注销
            $user_info = (new QappUserService)->getQAppUserByUid($user->id);
            if(!$user_info || $user_info->status == 0){
                \Log::info('user_log_off_middleware:uid:'.$user->uid);
                return response()->success();
            }else{
                \Log::info('user_ok:'.$user->id);
            }
        } catch (Exception $e) {
            myLog('QuickAppGetUserFromToken')->info($e->getMessage());
            return response()->error('QAPP_NOT_LOGIN');
        }

        return $next($request);
    }
}
