<?php

namespace App\Http\Middleware;

use App\Http\Response\ResponseJson;
use App\Models\ServiceUser;
use Closure;
use Illuminate\Http\Request;

class CheckServicer
{
    use ResponseJson;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $service_user_id = $request->input('service_user_id');
        $user_id = auth()->id();
        if ($service_user_id) {
            $user_id = $service_user_id;
        }

        $service_user = ServiceUser::where('user_id', $user_id)->first();
        if (empty($service_user))
            return $this->failure("无服务端权限");
        return $next($request);
    }
}
