<?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!
|
*/

$apiRoute = [
    'api/album/get-banner' => ['real' => 'Api/V1/Album/albumGetBanner','method'=>'get','auth'=>false],
    'Api/V1/Album/albumCat' => 'get',
    'Api/V1/Album/albumXyxLogin' => 'post',
    'Api/V1/Album/albumSetting' => 'get'
];
/*$auth = [
    'Api/V1/Album/albumGoods' => 'post',
    'Api/V1/Album/albumCustomerGoods' => 'post',
    'Api/V1/Album/albumCustomerGoodsDetail' => 'post',
    'Api/V1/Album/albumGetCountOfFavorite' => 'post',
    'Api/V1/AlbumBoss/getTop' => 'post',
    'Api/V1/AlbumBoss/agentCustomer' => 'post',
    'Api/V1/AlbumBoss/agentStatistical' => 'post',
    'Api/V1/AlbumBoss/albumOverviewActive' => 'post',
    'Api/V1/Album/albumGoods' => 'post',
    'Api/V1/Album/albumGoods' => 'post',
];*/
Route::get('/addons/furnitureWe7/public/', [
    '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']);
        session()->put('groupid', $_SESSION['wUser']['groupid']);
    };

    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');
        if (isset($apiRoute[$route_url])) {
            $route_url_r = $apiRoute[$route_url]['real'];
            $route_url_r = ucfirst($route_url_r);
            $route = $baseRoute = explode('/',$route_url_r);
            if (count($route) > 1) {
                unset($route[count($route) - 1]);
                $file = app_path() . '/Http/Controllers/' . implode("/", $route) . "Controller.php";
                if (file_exists($file)) {
                    $controller = implode("\\", $route) . "Controller";
                    $action = $controller . "@" . $baseRoute[count($route)];
dd($action);
                    if ($apiRoute[$route_url]['auth'] == true) {
                        Route::middleware(['auth.api'])->group(function () use ($action, $route, $auth, $route_url) {
                            switch ($auth[$route_url]['method']) {
                                case 'get':
                                    Route::get('/', ['as' => $route[0],
                                        'uses' => $action]);
                                    break;
                                case 'post':
                                    Route::post('/', ['as' => $route[0],
                                        'uses' => $action]);
                                    break;
                                case 'any':
                                    Route::any('/', ['as' => $route[0],
                                        'uses' => $action]);
                                    break;
                            }
                        });
                    } else {
                        switch ($apiRoute[$route_url]['method']) {
                            case 'get':
                                Route::get('/', ['as' => $route[0],
                                    'uses' => $action]);
                                break;
                            case 'post':
                                Route::post('/', ['as' => $route[0],
                                    'uses' => $action]);
                                break;
                            case 'any':
                                Route::any('/', ['as' => $route[0],
                                    'uses' => $action]);
                                break;
                        }
                    }

                }
            }
        }

        /*    });
        });*/

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


}
