<?php
namespace app\adminapi\controller\v1;

use app\adminapi\ApiController;
use think\facade\Db;
use think\facade\Session;
use thans\jwt\facade\JWTAuth;
use think\facade\Log;

class Auth extends ApiController
{
    public function Login()
    {
        $username=input('username');
        $password=input('password');

        // $captcha=input('captcha');

        // Log::write(json_encode(Session::all()));

        // if(!captcha_check($captcha)){
        //     return $this->Error('驗證碼錯誤','請求失敗',301);                
        // }
        $result=Db::name('admin')
        ->where('username',$username)
        ->find();

        if(!$result){
            return $this->Error('帳號或密碼錯誤','請求失敗',302);                
        }

        if(!password_verify($password , $result['password'])){
            return $this->Error('帳號或密碼錯誤','請求失敗',303);                
        }

        $token = JWTAuth::builder(['uid' => $result['id']]);
        
        $result=[
            'user'=>[
                'uid'=>$result['id'],
                "name" => $result['username'], 
                "avatar" => "https://gw.alipayobjects.com/zos/rmsportal/ubnKSIfAJTxIgXOKlciN.png", 
                "address" => "固原市", 
                "position" => [
                   "CN" => "產品分析師 | 螞蟻金服-計算服務事業群-IOS平臺部", 
                   "TW" => "產品分析師 | 螞蟻金服-計算服務事業群-IOS平臺部", 
                   "US" => "Product analyst | Ant Financial - Computing services business group - IOS platform division" 
                ] 
            ],
            'permissions'=>[
                [
                    'id'=>'queryForm',
                    'operation'=>['add','edit','delete']    
                ]
            ],
            'roles'=>[
                [
                    'id'=>'admin',
                    'operation'=>['add','edit','delete']    
                ]
            ],
            'token'=>$token,
            'expireAt'=>time()+30*60*1000
        ];
        return $this->Success($result);    

    }

    public function check(){
        print_r(JWTAuth::auth());
    }

    public function captcha($id=''){
        return captcha($id);
    }

    public function checkC($value){
        print_r(Session::all());

        if(!captcha_check($value)){
            //驗證失敗
            echo 'failure';
        };
        echo 'Success';
    }

    public function getRoute(){
        $routes=[
            [
                  "router" => "root", 
                  "children" => [
                     "DashBoard", 
                     [
                        "router" => "system",
                        "children" => [
                           [
                              "router" => "systemConfig", 
                              "name" => "站台設置", 
                              "authority" => [
                                 "permission" => "demo", 
                                 "role" => "admin" 
                              ] 
                           ] 
                        ] 
                     ], 
                    [
                        "router" => "admin", 
                        "children" => [
                            "adminUser",
                            "adminLog",
                            "adminRole",
                        ] 
                    ], 
                    [
                        "router" => "goods", 
                        "children" => [
                            "goodsList",
                            "goodsCategory",
                            "goodsType",
                        ] 
                    ], 
                    [
                        "router" => "order", 
                        "children" => [
                            "orderList",
                        ] 
                    ], 
                    [
                        "router" => "room", 
                        "children" => [
                            "roomList",
                        ] 
                    ], 
                    [
                        "router" => "user", 
                        "children" => [
                            "userList"
                        ] 
                    ], 
                    [
                        "router" => "setting", 
                        "children" => [
                            "settingBase",
                            "settingConfig"
                        ] 
                    ] 
                  ] 
               ] 
         ]; 

         return $this->Success($routes);    
    }
}
