tapi/app/controller/admin/auth/AuthController.php
2025-08-29 17:17:35 +08:00

96 lines
3.2 KiB
PHP

<?php
namespace app\controller\admin\auth;
use app\BaseController;
use app\entity\SysUser;
use app\entity\SysUserClient;
use app\entity\SysUserClientLog;
use app\enum\UserLoginEnum;
use app\service\admin\LoginService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
/**
* 用户Auth控制器
*/
class AuthController extends BaseController
{
/**
* 查询用户信息
* @return Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function user(): Json
{
$user = SysUser::findOrFail($this->auth->userId);
$data = $user->append(['authorities', 'roles'])->toArray();
return $this->writeSuccess('', $data);
}
/**
* 退出登录
* @return Json
*/
public function logout(): Json
{
$srv = new LoginService();
$srv->logout($this->request, $this->auth->getUser());
return $this->writeSuccess('退出成功');
}
public function unlock()
{
$client = $this->request->getClient();
$password = $this->request->post('password');
$clientModel = SysUserClient::where([
'client_id' => $client->id,
'client_name'=> $client->name,
'user_id' => $this->auth->userId,
])->find();
if($clientModel && $clientModel['is_lock']) {
if (empty($clientModel['lock_password'])) {
if(!password_verify($password, $this->auth->getUser()->get('password'))) {
SysUserClientLog::create([
'event' => 'UNLOCK_ERROR',
'message'=> "1. 尝试解锁,密码错误",
'data' => json_encode(['inputPass'=> password_hash($password)]),
'create_time' => date('Y-m-d H:i:s'),
'client_data_id'=> $clientModel['id']
]);
return $this->writeError('密码错误');
}
}else{
if($clientModel['lock_password'] !== $password){
SysUserClientLog::create([
'event' => 'UNLOCK_ERROR',
'message'=> "2. 尝试解锁,密码错误",
'data' => json_encode(['inputPass'=> $password]),
'create_time' => date('Y-m-d H:i:s'),
'client_data_id'=> $clientModel['id']
]);
return $this->writeError('密码错误');
}
}
$clientModel->save(['is_lock'=>0,'lock_password'=>'','lock_time'=>null]);
SysUserClientLog::create([
'event' => 'UNLOCK_SUCCESS',
'message'=> "0. 解锁成功",
'data' => json_encode(['inputPass'=> $password]),
'create_time' => date('Y-m-d H:i:s'),
'client_data_id'=> $clientModel['id']
]);
}else{
return $this->writeError('客户端错误');
}
return $this->writeSuccess('解锁成功');
}
}