<?php

namespace app\http\middleware;

use app\http\HttpClient;
use Closure;
use think\Middleware;
use think\Request;

class ClientMiddleware extends Middleware
{
    public function handle(Request $request, Closure $next)
    {
        $clientName = $request->header('client', '');
        if(empty($clientName)) {
            return response('设备未授权',200);
        }
        /*
         * 客户端信息
         */
        $client = new HttpClient(
            $clientName,
            $request->header('clientId', ''),
            $request->header('clientVersion', ''),
        );
        /*
         * 获取客户端
         */
        \app\Request::macro('getClient', function () use ($client) {
            return $client;
        });

        // Next.
        return $next($request);
    }
}