From 10c669e0026f7b898c73591af320e438b69da9bb Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Mon, 7 Nov 2022 16:29:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BE=AE=E4=BF=A1=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E6=96=B0=E5=BB=BA=E7=94=A8=E6=88=B7=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/Wechat/AuthErrorCode.php | 17 +++++++ app/Controller/Wechat/AuthController.php | 40 ++++++++++++++++ app/Helper/Curl.php | 34 ++++++++++++++ app/JsonRpc/UserExternalServiceInterface.php | 14 ++++++ app/Service/Wechat/AuthService.php | 48 +++++++++++++++++++ config/autoload/app.php | 8 ++++ config/autoload/server.php | 1 + config/autoload/services.php | 49 +++++++++++++++++++- 8 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 app/Constants/Wechat/AuthErrorCode.php create mode 100644 app/Controller/Wechat/AuthController.php create mode 100644 app/Helper/Curl.php create mode 100644 app/JsonRpc/UserExternalServiceInterface.php create mode 100644 app/Service/Wechat/AuthService.php create mode 100644 config/autoload/app.php diff --git a/app/Constants/Wechat/AuthErrorCode.php b/app/Constants/Wechat/AuthErrorCode.php new file mode 100644 index 0000000..0df037d --- /dev/null +++ b/app/Constants/Wechat/AuthErrorCode.php @@ -0,0 +1,17 @@ +request->input('code', ''); + + if (empty($code)) { + throw new BusinessException(AuthErrorCode::CODE_EMPTY_ERROR); + } + + return $this->service->codeToOpenID($code); + } +} diff --git a/app/Helper/Curl.php b/app/Helper/Curl.php new file mode 100644 index 0000000..06f1c14 --- /dev/null +++ b/app/Helper/Curl.php @@ -0,0 +1,34 @@ +create(); + + $client = make(Client::class, [ + 'config' => [ + 'handler' => $stack, + ] + ]); + $response = $client->get($url, ['timeout' => 2]); + + if ($response->getStatusCode() === 200) { + return json_decode($response->getBody()->getContents(), true); + } + + return []; + } + + public static function post(string $url, array $data = []) + { + } +} \ No newline at end of file diff --git a/app/JsonRpc/UserExternalServiceInterface.php b/app/JsonRpc/UserExternalServiceInterface.php new file mode 100644 index 0000000..c6a7e2b --- /dev/null +++ b/app/JsonRpc/UserExternalServiceInterface.php @@ -0,0 +1,14 @@ +wechat_auth_url . '?' . http_build_query([ + 'appid' => $this->appid, + 'secret' => $this->secret, + 'js_code' => $code, + 'grant_type' => 'authorization_code' + ]); + + $result = Curl::get($url); + $res = $this->userExternalService->wechatNewUser($result['data']['openid']); + + if (!$res['code'] !== 200) { + throw new BusinessException($res['code'], $res['msg']); + } + + return ['openid' => $result['data']['openid']]; + } +} \ No newline at end of file diff --git a/config/autoload/app.php b/config/autoload/app.php new file mode 100644 index 0000000..99d9524 --- /dev/null +++ b/config/autoload/app.php @@ -0,0 +1,8 @@ + 'wx73b7c1bc1e41f7ee', + 'wechat_punch_card_secret' => 'd4edc723b0f268dd5156096d3a293a29', +]; \ No newline at end of file diff --git a/config/autoload/server.php b/config/autoload/server.php index d72e1c5..0d82114 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -15,6 +15,7 @@ use Swoole\Constant; return [ 'mode' => SWOOLE_PROCESS, + 'type' => Hyperf\Server\CoroutineServer::class, 'servers' => [ [ 'name' => 'http', diff --git a/config/autoload/services.php b/config/autoload/services.php index c87ca08..a7df251 100644 --- a/config/autoload/services.php +++ b/config/autoload/services.php @@ -14,7 +14,54 @@ return [ 'discovery' => true, 'register' => true, ], - 'consumers' => [], + 'consumers' => value(function () { + $consumers = []; + // 这里示例自动创建代理消费者类的配置形式,顾存在 name 和 service 两个配置项,这里的做法不是唯一的,仅说明可以通过 PHP 代码来生成配置 + $services = [ + 'UserExternalService' => App\JsonRpc\UserExternalServiceInterface::class, + ]; + foreach ($services as $name => $interface) { + $consumers[] = [ + 'name' => $name, + 'service' => $interface, + 'protocol' => 'jsonrpc-http', + 'registry' => [ + 'protocol' => 'consul', + 'address' => env('CONSUL_URL', '127.0.0.1:8500'), + ], + // 配置项,会影响到 Packer 和 Transporter + 'options' => [ + 'connect_timeout' => 5.0, + 'recv_timeout' => 5.0, + 'settings' => [ + // 根据协议不同,区分配置 + 'open_eof_split' => true, + 'package_eof' => "\r\n", + // 'open_length_check' => true, + // 'package_length_type' => 'N', + // 'package_length_offset' => 0, + // 'package_body_offset' => 4, + ], + // 重试次数,默认值为 2,收包超时不进行重试。暂只支持 JsonRpcPoolTransporter + 'retry_count' => 2, + // 重试间隔,毫秒 + 'retry_interval' => 100, + // 使用多路复用 RPC 时的心跳间隔,null 为不触发心跳 + 'heartbeat' => 30, + // 当使用 JsonRpcPoolTransporter 时会用到以下配置 + 'pool' => [ + 'min_connections' => 1, + 'max_connections' => 32, + 'connect_timeout' => 10.0, + 'wait_timeout' => 3.0, + 'heartbeat' => -1, + 'max_idle_time' => 60.0, + ], + ], + ]; + } + return $consumers; + }), 'providers' => [], 'drivers' => [ 'consul' => [