From c5f301b9ada04e2188641cb264fc4d5d8c39bd5d Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Tue, 8 Nov 2022 11:38:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E7=BD=91=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E8=80=83=E5=8B=A4=E7=B3=BB=E7=BB=9F=E5=8A=9F=E8=83=BD=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=89=8D=E7=BC=80=EF=BC=8C=E5=8F=8A=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86=E5=85=AC=E5=85=B1=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => PunchCard}/Wechat/AuthController.php | 8 +-- app/Helper/Curl.php | 58 +++++++++++++++++-- app/Service/Wechat/AuthService.php | 23 ++++++++ 3 files changed, 81 insertions(+), 8 deletions(-) rename app/Controller/{ => PunchCard}/Wechat/AuthController.php (79%) diff --git a/app/Controller/Wechat/AuthController.php b/app/Controller/PunchCard/Wechat/AuthController.php similarity index 79% rename from app/Controller/Wechat/AuthController.php rename to app/Controller/PunchCard/Wechat/AuthController.php index 76a0123..fa8ee32 100644 --- a/app/Controller/Wechat/AuthController.php +++ b/app/Controller/PunchCard/Wechat/AuthController.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Controller\Wechat; +namespace App\Controller\PunchCard\Wechat; use App\Controller\BaseController; use App\Exception\BusinessException; @@ -10,12 +10,12 @@ use app\Constants\Wechat\AuthErrorCode; use App\Service\Wechat\AuthService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; -use Hyperf\HttpServer\Annotation\PostMapping; +use Hyperf\HttpServer\Annotation\RequestMapping; /** * 微信授权处理控制器 */ -#[Controller] +#[Controller(prefix: "kq")] class AuthController extends BaseController { #[Inject] @@ -26,7 +26,7 @@ class AuthController extends BaseController * * @return array */ - #[PostMapping(path: "auth/code2OpenID")] + #[RequestMapping(path: "auth/code2OpenID", methods: "get,post")] public function code2OpenID() : array { $code = $this->request->input('code', ''); diff --git a/app/Helper/Curl.php b/app/Helper/Curl.php index 06f1c14..309cac1 100644 --- a/app/Helper/Curl.php +++ b/app/Helper/Curl.php @@ -7,19 +7,46 @@ namespace App\Helper; use GuzzleHttp\Client; use Hyperf\Guzzle\HandlerStackFactory; +/** + * 请求处理公共类 + */ class Curl { - public static function get(string $url) : array + /** + * 获取Guzzle客户端实例(使用连接池) + * + * @return Client + */ + protected static function getClient() : Client { $factory = new HandlerStackFactory(); $stack = $factory->create(); - $client = make(Client::class, [ + return make(Client::class, [ 'config' => [ 'handler' => $stack, ] ]); - $response = $client->get($url, ['timeout' => 2]); + } + + /** + * 发送Get请求 + * + * @param string $url + * @param array $headers + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public static function get(string $url, array $headers = []) : array + { + $client = self::getClient(); + $options = ['timeout' => 2, 'verify' => false]; + + if (!empty($headers)) { + $options['headers'] = $headers; + } + + $response = $client->get($url, $options); if ($response->getStatusCode() === 200) { return json_decode($response->getBody()->getContents(), true); @@ -28,7 +55,30 @@ class Curl return []; } - public static function post(string $url, array $data = []) + /** + * 发送POST请求(application/x-www-form-urlencoded) + * + * @param string $url + * @param array $data + * @param array $headers + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public static function post(string $url, array $data = [], array $headers = []) : array { + $client = self::getClient(); + $options = ['timeout' => 2, 'verify' => false, 'form_params' => $data]; + + if (!empty($headers)) { + $options['headers'] = $headers; + } + + $response = $client->post($url, $options); + + if ($response->getStatusCode() === 200) { + return json_decode($response->getBody()->getContents(), true); + } + + return []; } } \ No newline at end of file diff --git a/app/Service/Wechat/AuthService.php b/app/Service/Wechat/AuthService.php index 6889db9..3bd2605 100644 --- a/app/Service/Wechat/AuthService.php +++ b/app/Service/Wechat/AuthService.php @@ -8,17 +8,40 @@ use App\JsonRpc\UserExternalServiceInterface; use Hyperf\Config\Annotation\Value; use Hyperf\Di\Annotation\Inject; +/** + * 微信授权处理服务层 + */ class AuthService { + /** + * 考勤系统小程序APPID + * + * @var string + */ #[Value("app.wechat_punch_card_appid")] protected string $appid; + /** + * 考勤系统小程序APPSecret + * + * @var string + */ #[Value("app.wechat_punch_card_secret")] protected string $secret; + /** + * 用户中心对外RPC服务 + * + * @var UserExternalServiceInterface + */ #[Inject] protected UserExternalServiceInterface $userExternalService; + /** + * 微信通过code获取session信息请求地址 + * + * @var string + */ protected string $wechat_auth_url = 'https://api.weixin.qq.com/sns/jscode2session'; /**