<?php

namespace app\admin\controller\auth;

use app\admin\service\auth\AuthService;
use app\admin\validate\auth\AuthValidate;
use app\model\SysUser;
use support\Redis;
use support\Request;
use Tinywan\Jwt\JwtToken;

class AuthController
{
    public $notNeedLogin = ['login'];

    public function login(Request $request)
    {
        $validate = new AuthValidate();
        if (!$validate->scene('login')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return AuthService::login($request->post());
    }

    public function logout(Request $request)
    {
        try {
            $userId = JwtToken::getCurrentId();
            SysUser::where('user_id',$userId)->update(['user_login_status'=>"OFFLINE"]);

            Redis::del("JWT:TOKEN:admin:".$userId);
        }catch (\Exception $e){
            dump($e->getMessage());
            return json_fail('退出登录失败');
        }


        return json_success('退出登录成功');

    }

    /**
     * @Desc 刷新token
     * @Author Gorden
     * @Date 2024/2/21 17:10
     *
     * @return \support\Response
     */
    public function refreshToken()
    {
        return AuthService::refreshToken();
    }

    public function userInfo(Request $request)
    {
        return AuthService::userInfo($request);
    }
}
