easy-swoole/wechat

企业微信: echostr校验失败,请检查是否正确解密并输出明文echostr

LMFName opened this issue · 2 comments

image
image

{
"require": {
"easyswoole/easyswoole": "3.4.x",
"easyswoole/fast-cache": "^2.0",
"easyswoole/task": "^1.1",
"easyswoole/queue": "^3.0",
"easyswoole/http": "^2.0",
"easyswoole/wechat": "^2.0"
},
"autoload": {
"psr-4": {
"App\": "App/"
}
}
}

感谢反馈,这个是我们的问题,没有对消息自动解密做兼容,后续我们会做好兼容自动处理。目前解决方法:需要解密企业微信发过来的密文字符串为明文,然后响应给企业微信。如下所示:

<?php

namespace App\HttpController;

use EasySwoole\WeChat\Factory;

class Index extends BaseController
{
    public function index()
    {
        $config = [
            // 企业微信后台的 企业 ID
            'corpId' => 'xxxxxxxxxxxxxxxxx',
            // 企业微信后台的 secret
            'corpSecret' => 'xxxxxxxxxxxxxxxxx',
            // server config
            'token' => 'xxxxxxxxx',
            'aesKey' => 'xxxxxxxxxxxxxxxxxx',
        ];

        $work = Factory::work($config);

        /** 注册消息事件回调 */
        $work->server->push(function (\EasySwoole\WeChat\Kernel\Contracts\MessageInterface $message) {
            return new \EasySwoole\WeChat\Kernel\Messages\Text('Hello EasySwoole WeChat!');
        });
        /** @var \Psr\Http\Message\ServerRequestInterface $psr7Request */
        $psr7Request = $this->request();

        // 解密企业微信发过来的密文字符串为明文【重点】
        $echoStr = $psr7Request['echostr'];
        $decryptEchoStr = $work->encryptor->decrypt($echoStr, $config['aesKey'], $config['corpId']);
        $response = $work->server->serve($psr7Request);

        /**
         * $response 是一个显式实现了 PSR-7 的对象,用户只需要处理该对象即可正确响应给微信
         * 下面是一个使用 EasySwoole 的响应方法
         */
        $this->response()->withStatus($response->getStatusCode());
        /**
         * PSR-7 的 Header 并不是单纯的 k => v 结构
         */
        foreach ($response->getHeaders() as $name => $values) {
            $this->response()->withHeader($name, implode(", ", $values));
        }

        // 响应给企业微信
        $this->response()->write($decryptEchoStr);
    }
}