<?php

namespace App\Http\Middleware;

use App\Models\AdminUserModel;
use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Auth;

class AuthMini
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(in_array($request->path(), ['api/mini/login', 'api/mini/reset', 'api/mini/test', 'api/mini/loginByWechat', 'api/mini/uploadFile'])) return $next($request);
        $token = $request->header('X-Token');
        if($token) {
            $user = User::where('token', $token)->first();
            if(!empty($user)) {
                Auth::guard('mini')->login($user);
                return $next($request);
            }
        }
        return response()->json(['code' => -100, 'msg' => '请先登录']);
    }
}
