<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/



Route::get('attachment/{md5}', [
    'as' => 'attachment.download',
    'uses' => 'Web\AttachmentController@download',
]);
Route::get('image/{md5}', [
    'as' => 'attachment.image',
    'uses' => 'Web\AttachmentController@image',
]);


if (request()->input('route') == 'geetest') {
    Route::any('/', 'Germey\Geetest\GeetestController@getGeetest');
}

//后台
if (strpos(request()->getRequestUri(), '/addons/') !== false) {
    //如未设置当前公众号则加到选择公众号列表
    if (strpos(request()->getRequestUri(), 'Admin/Auth/Login/wxLogin') !== false) {
        session_start();
        if (empty($_SESSION['store_id']) || empty($_SESSION['wUser'])) {
            return redirect('../../../web/index.php?c=wxapp&a=display');
        }//
        session()->forget('wUser');
        session()->forget('store_id');
        session()->put('store_id', $_SESSION['store_id']);
        session()->put('wUser', $_SESSION['wUser']);//dd(session()->get('wUser'));
        session()->put('groupid', $_SESSION['wUser']['groupid']);//dd(session()->get('wUser'));

//        session()->put('wAccount', $_SESSION['wAccount']);
    };

    if (strpos(request()->getRequestUri(), 'Api') !== false) {

        $api = app('Dingo\Api\Routing\Router');
     /*   $api->version('v1', ['namespace' => 'App\Http\Controllers'], function ($api) {
            $api->group(['middleware' => ['auth.api']], function ($api) {*/
// Furniture::parseRoute();
                //$route_url = $_SESSION['route'];
                $route_url = request()->input('route');
                $route_url = ucfirst($route_url);
                $route = $baseRoute = explode('/',$route_url);

                //dd($route);
                if (count($route) > 1) {
                    unset($route[count($route) - 1]);
                    $file = app_path() . '/Http/Controllers/' . implode("/", $route) . "Controller.php";
                    //dd($file);
                    if (file_exists($file)) {
                        $controller = implode("\\", $route) . "Controller";
                        $action = $controller . "@" . $baseRoute[count($route)];

                        Route::namespace('Api\V1')->group(function () use ($action, $route) {
                            Route::any('/', ['as' => $route[0],
                                'uses' => $action]);
                        });
                    }
                }
        /*    });
        });*/

    } else {
        // Furniture::parseRoute();
        //$route_url = $_SESSION['route'];
        $route_url = request()->input('route');
        $route_url = ucfirst($route_url);
        $route = $baseRoute = explode('/',$route_url);

        //dd($route);
        if (count($route) > 1) {
            unset($route[count($route) - 1]);
            $file = app_path() . '/Http/Controllers/' . implode("/", $route) . "Controller.php";
            //dd($file);
            if (file_exists($file)) {
                $controller = implode("\\", $route) . "Controller";
                $action = $controller . "@" . $baseRoute[count($route)];
                //dd($action);
                Route::any('/', ['as' => $route[0],
                    'uses' => $action]);
            }
        }
    }


}
