76 lines
1.7 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\login\controller;
use catchAdmin\login\request\LoginRequest;
use catchAdmin\permissions\model\Users;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchResponse;
2020-09-04 19:01:57 +08:00
use catcher\Code;
2020-04-29 17:37:45 +08:00
use catcher\exceptions\LoginFailedException;
2020-05-18 13:41:46 +08:00
use thans\jwt\facade\JWTAuth;
2020-04-29 17:37:45 +08:00
class Index extends CatchController
{
/**
* 登陆
*
* @time 2019年11月28日
* @param LoginRequest $request
* @param CatchAuth $auth
* @return bool|string
*/
public function login(LoginRequest $request, CatchAuth $auth)
{
$params = $request->param();
$token = $auth->attempt($params);
$user = $auth->user();
if ($user->status == Users::DISABLE) {
2020-09-04 19:01:57 +08:00
throw new LoginFailedException('该用户已被禁用', Code::USER_FORBIDDEN);
2020-04-29 17:37:45 +08:00
}
// 记录用户登录
$user->last_login_ip = request()->ip();
$user->last_login_time = time();
$user->save();
// 登录事件
$params['success'] = $token;
event('loginLog', $params);
return $token ? CatchResponse::success([
'token' => $token,
], '登录成功') : CatchResponse::success('', '登录失败');
}
/**
* 登出
*
* @time 2019年11月28日
* @return \think\response\Json
*/
2020-05-18 10:12:33 +08:00
public function logout(): \think\response\Json
2020-04-29 17:37:45 +08:00
{
2020-05-18 10:12:33 +08:00
return CatchResponse::success();
2020-04-29 17:37:45 +08:00
}
2020-05-18 13:41:46 +08:00
/**
* refresh token
*
* @author JaguarJack
* @email njphper@gmail.com
* @time 2020/5/18
* @return \think\response\Json
*/
public function refreshToken()
{
return CatchResponse::success([
'token' => JWTAuth::refresh()
]);
}
2020-04-29 17:37:45 +08:00
}