新增退出登录接口

This commit is contained in:
wucongxing 2023-03-04 10:15:49 +08:00
parent efd4718255
commit d7983a1f2b
3 changed files with 53 additions and 0 deletions

View File

@ -52,4 +52,17 @@ class UserController extends AbstractController
$data = $UserService->putUserName();
return $this->response->json($data);
}
/**
* 退出登陆
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function putLoginout(): ResponseInterface
{
$UserService = new UserService();
$data = $UserService->putLoginout();
return $this->response->json($data);
}
}

View File

@ -13,6 +13,7 @@ use App\Utils\PcreMatch;
use Extend\Wechat\Wechat;
use Hyperf\Amqp\Result;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
@ -203,6 +204,42 @@ class UserService extends BaseService
return success();
}
/**
* 退出登陆
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function putLoginout(){
$user_info = $this->request->getAttribute("userInfo") ?? [];
// 获取用户信息
$params = array();
$params['user_id'] = $user_info['user_id'];
$user = User::getOne($params);
if (empty($user)){
return fail();
}
// 获取token
$bearer_token = $this->request->getHeader('Authorization');
if (empty($bearer_token)){
return fail();
}
$token = explode(' ', $bearer_token[0]);
$redis = $this->container->get(Redis::class);
// 旧token加入黑名单 5天有效期5天内无法继续进行访问
$res = $redis->set('jwt_black_' . $token, time(), 60*60*24*5);
if (!$res) {
return fail(HttpEnumCode::SERVER_ERROR);
}
return success();
}
/**
* 通过user_id获取用户openid
* @param string|int $user_id

View File

@ -348,4 +348,7 @@ Router::addGroup('/user', function () {
// 修改用户名
Router::put('/name', [UserController::class, 'putUserName']);
// 退出登陆
Router::put('/loginout', [UserController::class, 'putLoginout']);
});