<?php

namespace App\Http\Middleware;

use App\Exceptions\AlertException;
use Closure;

class AppVerify
{
    protected $except = [];

    /**
     * @param  \Illuminate\Http\Request  $request
     * @param  Closure  $next
     * @return mixed
     * @throws \ApiException
     */
    public function handle($request, Closure $next)
    {
        $app = $request->header('app') ?? null;
        if (!is_null($app) && $app != 'gh_01c089b58dda') {
            $config['miniprogram'] = [
                'public_id' => env("LITTLE_{$app}_PUBLIC_ID", null),
                'public_name' => env("LITTLE_{$app}_PUBLIC_NAME", ''),
                'app_id' => env("LITTLE_{$app}_APP_ID", ''),
                'app_secret' => env("LITTLE_{$app}_APP_SECRET", '')
            ];
            if (is_null($config['miniprogram']['public_id'])) {
                throw new AlertException('header参数错误', 101);
            }
            config($config);
        }
        return $next($request);
    }
}
