appId = config('system.wx_appid'); } /** * @return false|mixed|\Redis|string * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function getAccessTokenCache(): mixed { $key = CommonRedisKey::getWxAccessToken(); if ($this->redisCache->exists($key)) { return $this->redisCache->get($key) ?? ''; } $data = $this->getAccessToken(); if (empty($data) || empty($data['access_token']) || empty($data['expires_in'])) return ''; $this->redisCache->setEx($key, $data['access_token'], $data['expires_in'] - 10,RedisCode::SYSTEM_DB); return $data['access_token']; } /** * 获取 access Token * @return mixed * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ private function getAccessToken(): mixed { $url = sprintf( '/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s', config('system.wx_appid'), config('system.wx_secret') ); try { $tencentResponse = $this->clientFactory->create([ 'base_uri' => $this->BaseUri, 'timeout' => 5 ])->get($url); $contents = $tencentResponse->getBody()->getContents(); $this->log->callbackLog(__class__.':微信服务器返回getAccessToken信息:'.json_encode($contents)); $res = json_decode($contents,true); if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙'); return $res; }catch (GuzzleException $e) { $this->log->debug(__CLASS__.':debug:'.$e->getMessage()); throw new ErrException($e->getMessage()); } } /** * @param string $code * @return mixed * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function jsCodeGetOpenId(string $code): mixed { $url = sprintf( '/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code', config('system.wx_appid'), config('system.wx_secret'), $code ); try { $tencentResponse = $this->clientFactory->create([ 'base_uri' => $this->BaseUri, 'timeout' => 5 ])->get($url); $contents = $tencentResponse->getBody()->getContents(); $this->log->callbackLog(__class__.':微信服务器返回wx_login信息:'.json_encode($contents)); $res = json_decode($contents,true); if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙'); return $res; }catch (GuzzleException $e) { $this->log->debug(__CLASS__.':debug:'.$e->getMessage()); throw new ErrException($e->getMessage()); } } /** * @param string $code * @return mixed * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function jsCodeGetPhoneNumber(string $code): mixed { $url = sprintf( '/wxa/business/getuserphonenumber?access_token=%s', $this->getAccessTokenCache() ); $params = [ 'code' => $code, ]; try { $wxResponse = $this->clientFactory->create([ 'base_uri' => $this->BaseUri, 'timeout' => 5 ])->post($url,[ 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => json_encode($params) ]); $contents = $wxResponse->getBody()->getContents(); $this->log->callbackLog(__class__.':微信服务器返回get_phone_number信息:'.json_encode($contents)); $res = json_decode($contents,true); if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙'); return $res; }catch (GuzzleException $e){ $this->log->debug(__CLASS__.':debug:'.$e->getMessage()); throw new ErrException($e->getMessage()); } } /** * @param string $templateId * @param string $toUserOpenId * @param array $data * @param string|null $page * @return mixed * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function sendSubMessage(string $templateId, string $toUserOpenId, array $data ,string $page = null): mixed { $url = sprintf( '/cgi-bin/message/subscribe/send?access_token=%s', $this->getAccessTokenCache() ); $params = [ 'template_id' => $templateId, 'touser' => $toUserOpenId, 'data' => $data, 'miniprogram_state' => !SystemUtil::checkProEnv() ? 'developer' : 'formal', //不是正式环境 使用开发版 'lang' => 'zh_CN' ]; if (!empty($page)) $params['page'] = $page; try { $wxResponse = $this->clientFactory->create([ 'base_uri' => $this->BaseUri, 'timeout' => 5 ])->post($url,[ 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => json_encode($params) ]); $contents = $wxResponse->getBody()->getContents(); $this->log->callbackLog(__class__.':微信服务器返回send_sub_message信息:'.json_encode($contents)); $res = json_decode($contents,true); if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙'); return $res; }catch (GuzzleException $e){ throw new ErrException($e->getMessage()); } } }