From c7e65258b0a90d115f3cb084e6c8c64caac0a46c Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Mon, 20 Feb 2023 14:15:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84wechat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/Wechat/Wechat.php | 220 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 extend/Wechat/Wechat.php diff --git a/extend/Wechat/Wechat.php b/extend/Wechat/Wechat.php new file mode 100644 index 0000000..ec06235 --- /dev/null +++ b/extend/Wechat/Wechat.php @@ -0,0 +1,220 @@ +config = config("we_chat.patient"); + }elseif ($user_type == 2){ + $this->config = config("we_chat.doctor"); + }elseif ($user_type == 3){ + $this->config = config("we_chat.pharmacist"); + } + + if (empty($this->config)){ + throw new BusinessException("系统配置错误", HttpEnumCode::SERVER_ERROR); + } + } + + /** + * 获取wechat类 + * @param string $appId + * @param string $appSecret + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function createApp(string $appId, string $appSecret) + { + $config = array( + "app_id" => $appId, + "secret" => $appSecret, + "http" => [ + 'throw' => false, // 状态码非 200、300 时是否抛出异常,默认为开启 + 'timeout' => 5.0, + // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + + 'retry' => true, // 使用默认重试配置 + // 'retry' => [ + // // 仅以下状态码重试 + // 'http_codes' => [429, 500] + // // 最大重试次数 + // 'max_retries' => 3, + // // 请求间隔 (毫秒) + // 'delay' => 1000, + // // 如果设置,每次重试的等待时间都会增加这个系数 + // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) + // 'multiplier' => 3 + // ], + ] + ); + + try { + // 获取WeChat客户端 + $this->app = new Application($config); + + // 替换缓存 + $this->app->setCache(ApplicationContext::getContainer()->get(CacheInterface::class)); + + } catch (InvalidArgumentException $e) { + throw new BusinessException('实例化EasyWeChat类失败:' . $e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + + } + + /** + * 获取wechat客户端 + * @param string $appId + * @param string $appSecret + * @return AccessTokenAwareClient + */ + public function createClient(string $appId, string $appSecret): AccessTokenAwareClient + { + try { + // 获取WeChat客户端 + $this->createApp($appId, $appSecret); + + return $this->app->getClient(); + + } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { + throw new BusinessException('实例化EasyWeChat类失败:' . $e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } + + /** + * 根据 jsCode 获取用户 session 信息 + * @param string $code code + * @return array + * @throws ClientExceptionInterface + * @throws ContainerExceptionInterface + * @throws DecodingExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ServerExceptionInterface + * @throws TransportExceptionInterface + */ + public function codeToSession(string $code): array + { + try { + $this->createApp($this->config['app_id'],$this->config['secret']); + $utils = $this->app->getUtils(); + return $utils->codeToSession($code); + } catch (\Exception $e) { + throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } + + /** + * 解密微信会话信息 + * @param string $sessionKey 会话密钥 + * @param string $iv 加密算法的初始向量 + * @param string $encryptedData 用户信息的加密数据 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function decryptSession(string $sessionKey, string $iv, string $encryptedData): array + { + try { + $this->createApp($this->config['app_id'],$this->config['secret']); + + $utils = $this->app->getUtils(); + return $utils->decryptSession($sessionKey, $iv, $encryptedData); + } catch (\Exception $e) { + throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } + + /** + * 获取 access_token + * @return string + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function getAccessToken(): string + { + try { + $this->createApp($this->config['app_id'],$this->config['secret']); + + $accessToken = $this->app->getAccessToken(); + return $accessToken->getToken(); + } catch (\Exception $e) { + throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } + + /** + * 获取手机号 + * @param string $code + * @return array + * @throws ClientExceptionInterface + * @throws DecodingExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ServerExceptionInterface + * @throws TransportExceptionInterface|BadResponseException + */ + public function getPhone(string $code): array + { + try { + $this->createApp($this->config['app_id'],$this->config['secret']); + + $client = $this->createClient($this->config['app_id'],$this->config['secret']); + + $options = [ + "code" => $code, + ]; + + $response = $client->postJson('wxa/business/getuserphonenumber', $options); + + if ($response->isFailed()) { + // 出错了,处理异常 + $result = $response->toArray(); + if(empty($result)){ + // 返回值为空 + return []; + } + if (isset($result['errcode'])){ + if ($result['errcode'] == "40029"){ + // code过期 + return []; +// throw new BusinessException( HttpEnumCode::getMessage(HttpEnumCode::GET_WX_ERROR),HttpEnumCode::GET_WX_ERROR); + } + } + return []; + } + + return $response->toArray(false); + } catch (\Exception $e) { + throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } +} \ No newline at end of file