68 lines
1.6 KiB
PHP
Raw Normal View History

2019-12-02 23:04:43 +08:00
<?php
namespace catchAdmin\login\controller;
use catchAdmin\login\request\LoginRequest;
2020-01-07 17:27:55 +08:00
use catchAdmin\user\model\Users;
2019-12-12 09:13:44 +08:00
use catcher\base\CatchController;
2020-01-07 17:27:55 +08:00
use catcher\CatchAuth;
2019-12-12 09:13:44 +08:00
use catcher\CatchResponse;
2020-01-25 20:03:18 +08:00
use catcher\CatchUpload;
2020-01-07 17:27:55 +08:00
use catcher\exceptions\LoginFailedException;
2020-01-25 20:03:18 +08:00
use jaguarjack\filesystem\cloud\adapters\QiniuAdapter;
use think\facade\Filesystem;
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
{
2020-01-07 17:27:55 +08:00
/**
* 登陆
*
* @time 2019年11月28日
* @param LoginRequest $request
* @param CatchAuth $auth
* @return bool|string
*/
2020-01-25 20:03:18 +08:00
public function login(LoginRequest $request, CatchAuth $auth, CatchUpload $upload)
2019-12-02 23:04:43 +08:00
{
2020-01-25 20:03:18 +08:00
// dd(Filesystem::disk('qcloud')->putFile('ok', $file));
2019-12-12 22:33:45 +08:00
$params = $request->param();
2020-01-07 19:18:24 +08:00
2020-01-07 17:27:55 +08:00
$token = $auth->attempt($params);
$user = $auth->user();
if ($user->status == Users::DISABLE) {
throw new LoginFailedException('该用户已被禁用');
}
// 记录用户登录
$user->last_login_ip = request()->ip();
$user->last_login_time = time();
$user->save();
2019-12-12 22:33:45 +08:00
// 登录事件
2019-12-22 09:37:52 +08:00
$params['success'] = $token;
2020-01-07 17:27:55 +08:00
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,
2020-01-07 17:27:55 +08:00
], '登录成功') : CatchResponse::success('', '登录失败');
2019-12-02 23:04:43 +08:00
}
2020-01-20 14:38:32 +08:00
/**
* 登出
*
* @time 2019年11月28日
* @param CatchAuth $auth
* @return \think\response\Json
*/
public function logout(CatchAuth $auth): \think\response\Json
2019-12-02 23:04:43 +08:00
{
2020-01-20 14:38:32 +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
}
2020-01-07 17:27:55 +08:00
}