77 lines
1.7 KiB
PHP
Raw Normal View History

2019-12-02 23:04:43 +08:00
<?php
namespace catchAdmin\login\controller;
2019-12-15 13:38:02 +08:00
use app\exceptions\LoginFailedException;
2019-12-12 18:52:56 +08:00
use catchAdmin\user\Auth;
2019-12-02 23:04:43 +08:00
use catchAdmin\login\request\LoginRequest;
2019-12-12 09:13:44 +08:00
use catcher\base\CatchController;
use catcher\CatchResponse;
2019-12-12 18:52:56 +08:00
use think\captcha\Captcha;
2019-12-02 23:04:43 +08:00
2019-12-12 09:13:44 +08:00
class Index extends CatchController
2019-12-02 23:04:43 +08:00
{
/**
* 登录
*
* @time 2019年11月30日
* @throws \Exception
* @return string
*/
2019-12-06 08:24:07 +08:00
public function index(): string
2019-12-02 23:04:43 +08:00
{
2019-12-06 08:24:07 +08:00
return $this->fetch();
2019-12-02 23:04:43 +08:00
}
/**
* 登陆
*
* @time 2019年11月28日
* @param LoginRequest $request
* @return bool|string
2019-12-12 22:33:45 +08:00
* @throws \catcher\exceptions\LoginFailedException
2019-12-12 09:13:44 +08:00
* @throws \cather\exceptions\LoginFailedException
2019-12-15 13:38:02 +08:00
* @throws LoginFailedException
2019-12-02 23:04:43 +08:00
*/
public function login(LoginRequest $request)
{
2019-12-12 22:33:45 +08:00
$params = $request->param();
2019-12-22 09:37:52 +08:00
$token = Auth::login($params);
2019-12-12 22:33:45 +08:00
// 登录事件
2019-12-22 09:37:52 +08:00
$params['success'] = $token;
2019-12-15 13:38:02 +08:00
event('loginLog', $params);
2019-12-12 22:33:45 +08:00
2019-12-22 09:37:52 +08:00
return $token ? CatchResponse::success([
'token' => $token,
], '登录成功') :
2019-12-12 22:33:45 +08:00
CatchResponse::success('', '登录失败');
2019-12-02 23:04:43 +08:00
}
/**
* 登出
*
* @time 2019年11月28日
2019-12-14 22:54:24 +08:00
* @return \think\response\Json
* @throws \Exception
2019-12-02 23:04:43 +08:00
*/
2019-12-14 22:54:24 +08:00
public function logout(): \think\response\Json
2019-12-02 23:04:43 +08:00
{
2019-12-12 18:52:56 +08:00
if (Auth::logout()) {
2019-12-14 22:54:24 +08:00
return CatchResponse::success();
2019-12-02 23:04:43 +08:00
}
2019-12-14 22:54:24 +08:00
return CatchResponse::fail('登出失败');
2019-12-02 23:04:43 +08:00
}
2019-12-12 18:52:56 +08:00
/**
*
* @time 2019年12月12日
* @param Captcha $captcha
* @param null $config
* @return \think\Response
*/
public function captcha(Captcha $captcha, $config = null): \think\Response
{
return $captcha->create($config);
}
2019-12-02 23:04:43 +08:00
}