A few months ago, I developed a project related to wechat hardware. Its business is relatively simple, that is, a wechat Bluetooth hardware device, which transmits some data to our server through wechat hardware JSAPI.

Have the development of wechat hardware friends, should have understanding, the process is roughly as follows:

But recently, the customer told me that they could not receive the information sent by the hardware.

How is this possible, the code, the server has not changed, how can there be a problem? But after all, the customer is God, this problem has to check ah, I cherish a perturbed heart, looked at the server Log.

See this section of the log, more feel strange, easywechat extension pack has never been upgraded, this version 3.1. How could this possibly go wrong? I opened the guard.php file with the following code:

    /** 
     * Handle message.
     *
     * @param array $message
     *
     * @return mixed
     */
    protected function handleMessage(array $message)
    {
        $handler = $this->messageHandler;

        if(! is_callable($handler)) {
            Log::debug('No handler enabled.');

            return null;
        }

        Log::debug('Message detail:'.$message);

        $message = new Collection($message);

        $type = $this->messageTypeMapping[$message->get('MsgType')];

        $response = null;

        if ($this->messageFilter & $type) {
            $response = call_user_func_array($handler[$message]);
        }

        return $response;
    }
Copy the code

Line 393 is this line:

this->messageTypeMapping[$message->get(‘MsgType’)];

$message->get(‘MsgType’) $message->get(‘MsgType’)

All kinds of Google fruitless, finally find the super brother, easywechat author, with the help of the super brother, locate the error.

From the log of wechat, we can see that data sent from hardware has been received, but the data received is as follows:

{
	"device_id": "gh_e6a24fdc82b6_69b49a3ee626ee55"."device_type": "gh_e6a24fdc82b6"."msg_id": "524313758"."msg_type": "device_text"."create_time": "1516171166"."open_id": "o7iyW0sUwv4wH6PWhextVbtPkNVE"."session_id": "380219"."content": "AQAPAgATAwAWBAAA"
}
Copy the code

A normal text message packet looks like this:

{
	"ToUserName": "gh_e6a24fdc82b6"."FromUserName": "o7iyW0sUwv4wH6PWhextVbtPkNVE"."CreateTime": "1516171641"."MsgType": "text"."Content": "123"."MsgId": "6511907613782547557"
}
Copy the code

MsgType ??

msg_type ??

WTF? The programmers of wechat development? Are you guys kidding me?

This data structure…

This naming convention…

Unable to tease…

The problem has been confirmed. The protocol of wechat has been changed. I have adjusted the variable naming and some details of easywechat. https://github.com/todayqq/wechat/blob/device_update/src/Server/Guard.php

The modified part has submitted PR to the original wechat extension package. We hope that those who develop wechat hardware will not fall into the pit repeatedly.

Finally, thanks for chao elder brother’s help!