<?php

namespace App\Http\Middleware;

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

class QuickAppUserInfoRefresh extends BaseMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 获取头部信息
        $package     = $request->header('x-package', '');

        // 判断是否登录
        $token = $this->auth->setRequest($request)->getToken();
        if ($token) {

            try {
                //获取用户信息
                $user = $this->auth->authenticate($token);

                // 设置全局信息
                (new QappUserService)->setGolableUser($user->id, $package);
            } catch (\Exception $e) {
                \Log::info('book_list_user_info_error:');
                \Log::info($e->getMessage());
            }

        }

        return $next($request);
    }
}
