首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
hypef集成使用easywechat公众号的问题?
**直接贴代码吧** ### easywechat 修改 ```php <?php namespace App\Repository\Service; use App\Repository\Contract\EasyWeChatInterface; use EasyWeChat\Factory; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Hyperf\Guzzle\CoroutineHandler; use Hyperf\Guzzle\HandlerStackFactory; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\Utils\ApplicationContext; use Overtrue\Socialite\Providers\AbstractProvider; use Psr\SimpleCache\CacheInterface; use Symfony\Component\HttpFoundation\Request; class EasyWeChatService implements EasyWeChatInterface { private $config; private $request; public function __construct(array $config, RequestInterface $request) { // 接收值并储存于类属性中 $this->config = $config; // 设置 OAuth 授权的 Guzzle 配置 AbstractProvider::setGuzzleOptions([ 'http_errors' => false, 'handler' => HandlerStack::create(new CoroutineHandler()), ]); $this->request = $request; } public function miniProgram($config = []) { $app = Factory::miniProgram($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } public function work($config = []) { $app = Factory::work($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } public function officialAccount($config = []) { $app = Factory::officialAccount($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } public function payment($config = []) { $app = Factory::payment($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } protected function buildConfig($config = [], $module = '') { return array_merge($config, $this->config[$module]['default']); } protected function rebindSomethings(object $app) { $config = $app['config']->get('http', []); $config['handler'] = ApplicationContext::getContainer()->get(HandlerStackFactory::class)->create(); $app->rebind('http_client', new Client($config)); // 重写 Handler $app->rebind('guzzle_handler', new CoroutineHandler()); // 重新 Cache $app->rebind('cache', ApplicationContext::getContainer()->get(CacheInterface::class)); $get = $this->request->getQueryParams(); $post = $this->request->getParsedBody(); $cookie = $this->request->getCookieParams(); $files = $this->request->getUploadedFiles(); $server = $this->request->getServerParams(); $xml = $this->request->getBody()->getContents(); $app['request'] = new Request($get, $post, [], $cookie, $files, $server, $xml); return $app; } } ``` ### 控制器代码 ```php public function serve( ResponseInterface $response, EasyWeChatInterface $easyWeChat ) { $app = $easyWeChat->officialAccount(); $message = $app->server->getMessage(); var_dump($message); $app->server->push(function ($message) { return "您好!欢迎使用 EasyWeChat"; }); $response = $app->server->serve(); return $response; } ``` ### 目前的问题 1. 上面的easywechat修改有没有问题? 2. 控制器里面如何返回消息给微信?
发布于4年前 · 20 次浏览 · 来自
开发框架
z
zhimma
**直接贴代码吧** ### easywechat 修改 ```php <?php namespace App\Repository\Service; use App\Repository\Contract\EasyWeChatInterface; use EasyWeChat\Factory; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Hyperf\Guzzle\CoroutineHandler; use Hyperf\Guzzle\HandlerStackFactory; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\Utils\ApplicationContext; use Overtrue\Socialite\Providers\AbstractProvider; use Psr\SimpleCache\CacheInterface; use Symfony\Component\HttpFoundation\Request; class EasyWeChatService implements EasyWeChatInterface { private $config; private $request; public function __construct(array $config, RequestInterface $request) { // 接收值并储存于类属性中 $this->config = $config; // 设置 OAuth 授权的 Guzzle 配置 AbstractProvider::setGuzzleOptions([ 'http_errors' => false, 'handler' => HandlerStack::create(new CoroutineHandler()), ]); $this->request = $request; } public function miniProgram($config = []) { $app = Factory::miniProgram($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } public function work($config = []) { $app = Factory::work($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } public function officialAccount($config = []) { $app = Factory::officialAccount($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } public function payment($config = []) { $app = Factory::payment($this->buildConfig($config, uncamelize(__FUNCTION__))); return $this->rebindSomethings($app); } protected function buildConfig($config = [], $module = '') { return array_merge($config, $this->config[$module]['default']); } protected function rebindSomethings(object $app) { $config = $app['config']->get('http', []); $config['handler'] = ApplicationContext::getContainer()->get(HandlerStackFactory::class)->create(); $app->rebind('http_client', new Client($config)); // 重写 Handler $app->rebind('guzzle_handler', new CoroutineHandler()); // 重新 Cache $app->rebind('cache', ApplicationContext::getContainer()->get(CacheInterface::class)); $get = $this->request->getQueryParams(); $post = $this->request->getParsedBody(); $cookie = $this->request->getCookieParams(); $files = $this->request->getUploadedFiles(); $server = $this->request->getServerParams(); $xml = $this->request->getBody()->getContents(); $app['request'] = new Request($get, $post, [], $cookie, $files, $server, $xml); return $app; } } ``` ### 控制器代码 ```php public function serve( ResponseInterface $response, EasyWeChatInterface $easyWeChat ) { $app = $easyWeChat->officialAccount(); $message = $app->server->getMessage(); var_dump($message); $app->server->push(function ($message) { return "您好!欢迎使用 EasyWeChat"; }); $response = $app->server->serve(); return $response; } ``` ### 目前的问题 1. 上面的easywechat修改有没有问题? 2. 控制器里面如何返回消息给微信?
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2020-03-13
李铭昕
代码应该没有问题 数据返回的时候,我记得是有 `raw` 方法,可以直接返回文本的。
赞
0
回复
2020-04-08
李铭昕
回复
zhimma
肯定要放在控制器里的啊。。。
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。