hyperf-gateway/app/Controller/PunchCard/User/WechatAuthController.php

61 lines
1.4 KiB
PHP
Raw Normal View History

2022-11-07 16:29:37 +08:00
<?php
declare(strict_types=1);
2022-11-17 10:14:12 +08:00
namespace App\Controller\PunchCard\User;
2022-11-07 16:29:37 +08:00
use App\Controller\BaseController;
use App\Exception\BusinessException;
use app\Constants\Wechat\AuthErrorCode;
2022-11-17 10:14:12 +08:00
use App\Service\User\WechatAuthService;
2022-11-07 16:29:37 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
2022-11-17 10:14:12 +08:00
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
2022-11-17 10:14:12 +08:00
use Phper666\JWTAuth\Middleware\JWTAuthDefaultSceneMiddleware;
2022-11-07 16:29:37 +08:00
/**
* 微信授权处理控制器
*/
#[Controller(prefix: "kq")]
2022-11-17 10:14:12 +08:00
class WechatAuthController extends BaseController
2022-11-07 16:29:37 +08:00
{
2022-11-17 10:14:12 +08:00
/**
* 微信授权服务
*
* @var WechatAuthService
*/
2022-11-07 16:29:37 +08:00
#[Inject]
2022-11-17 10:14:12 +08:00
protected WechatAuthService $service;
2022-11-07 16:29:37 +08:00
/**
* 通过微信授权新建用户
*
* @return array
*/
#[RequestMapping(path: "auth/code2OpenID", methods: "get,post")]
2022-11-07 16:29:37 +08:00
public function code2OpenID() : array
{
$code = $this->request->input('code', '');
if (empty($code)) {
throw new BusinessException(AuthErrorCode::CODE_EMPTY_ERROR);
}
return $this->service->codeToOpenID($code);
}
2022-11-17 10:14:12 +08:00
/**
* 刷新token
*
* @return array
*/
#[Middleware(JWTAuthDefaultSceneMiddleware::class)]
#[GetMapping(path: "auth/refresh_token")]
public function refreshToken() : array
{
return $this->service->refreshToken();
}
2022-11-07 16:29:37 +08:00
}