token获取:
一开始我还以为,监听哪个店铺的消息,就用哪个店铺的appAuthToken获取长链接的token,但实际上,不管是使用哪个店铺的appAuthToken返回的内容是一样的,同时也意味着,我们可以获取所有已开启即时通讯店铺的所有消息,客户发给商家、商家发给客户。
so,只需要生成一次就可以写死了。我们需要的值connectionToken、connectionId、imSecret。
开发文档:https://developer.meituan.com/openapi#10.4.6
var mtSocket = new WebSocket('wss://wpush.meituan.com/websocket/{connectionId}_WMOPEN/{connectionToken}');mtSocket.onopen = function(){mtSocket.send('XXX连接成功');};mtSocket.onmessage = function(e){console.log(e.data)}
获取到的数据是这样的
文本内容:rQoVHeLTUf+wvmefyRvl6uqIYSOKRCDD1abu8N5Yw9V9M8GaDNwgQgQnvviEcclmaE46mFsa9dhT3JPIFxkXvtd8Hp44WqC02GD6LOluAKUQ33TJQl4n5jFrVdY2c/JCtUEVZ9ik9aIZ30+YW/7sC62Jh0q5O/3FC0gcb8LkZ0mxe5mi2HS/CWCcUA+GarGi2Z1HOt1g/xkZ+aHlftlGWEwHBZcxcddn3SVdnrg9osU=
消息内容是需要解密,所以接收到消息了就把内容提交到接口
var mtSocket = new WebSocket('wss://wpush.meituan.com/websocket/{connectionId}_WMOPEN/{connectionToken}');mtSocket.onopen = function(){mtSocket.send('XXX连接成功');};mtSocket.onmessage = function(e){console.log(e.data)$.ajax({url:'{:url("im/websocket")}',type:"post",data:{data:e.data},async:false,success:function (res) {console.log(res)},error:function (data) {}})}
public function websocket(){$data =json_decode(input('data'), true);if ($data) {$msg_content = \meituan\Im::decrypt($data['msg_content']);if (empty($msg_content)) {return '解密失败';}return $msg_content;}}
<?php
namespace meituan;class Im {protected static $iv = "imSecret";//获取长连接token得到的imSecretprotected static $encryptKey = "imSecret";//获取长连接token得到的imSecret//加密public static function encrypt($encryptStr) {$localIV = self::$iv;$encryptKey = self::encryptKey;//Open module$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);//print "module = $module <br/>" ;mcrypt_generic_init($module, $encryptKey, $localIV);//Padding$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples//encrypt$encrypted = mcrypt_generic($module, $encryptStr);//Closemcrypt_generic_deinit($module);mcrypt_module_close($module);return base64_encode($encrypted);}//解密public static function decrypt($encryptStr) {$localIV = self::$iv;$encryptKey = self::$encryptKey;//Open module$module = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);//print "module = $module <br/>" ;@mcrypt_generic_init($module, $encryptKey, $localIV);$encryptedData = base64_decode($encryptStr);$encryptedData = @mdecrypt_generic($module, $encryptedData);return trim ($encryptedData);}
}?>
初步使用结束。
你好,我是勤勤学长。
www.11ak.cn
QQ微信同号318692996