Commit 01f92cf9 by hanccc

增加撤回

(cherry picked from commit 528517201c04eec76afac225076272f433fdac8f)
1 parent 7284522d
...@@ -303,6 +303,7 @@ $robot->server->setCustomHandler(function(){ ...@@ -303,6 +303,7 @@ $robot->server->setCustomHandler(function(){
# to do list # to do list
- [ ] 命令行操作信息发送 - [ ] 命令行操作信息发送
- [ ] 增加消息集合存储
- [ ] 消息处理 - [ ] 消息处理
# 已知bug # 已知bug
......
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:33
*/
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Robot\Foundation\Robot;
use Hanson\Robot\Message\Message;
use Hanson\Robot\Support\Console;
$robot = new Robot([
'tmp' => __DIR__ . '/./../tmp/',
]);
$robot->server->setMessageHandler(function($message){
/** @var $message Message */
Console::log('我的username'.myself()->username);
if($message->type === 'Recall' && $message->rawMsg['FromUserName'] !== myself()->username){
Console::log($message->content);
Message::send($message->content, $message->username);
}
});
$robot->server->run();
...@@ -55,7 +55,9 @@ class Account ...@@ -55,7 +55,9 @@ class Account
{ {
$account = static::$group->get($username, null); $account = static::$group->get($username, null);
return $account ? : static::$contact->get($username, null); $account = $account ? : static::$contact->get($username, null);
return $account ? : member()->get($username, null);
} }
} }
\ No newline at end of file \ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/13
* Time: 20:56
*/
namespace Hanson\Robot\Collections;
use Hanson\Robot\Support\Console;
use Illuminate\Support\Collection;
class Message extends Collection
{
static $instance = null;
/**
* create a single instance
*
* @return Message
*/
public static function getInstance()
{
if(static::$instance === null){
static::$instance = new Message();
}
return static::$instance;
}
}
\ No newline at end of file \ No newline at end of file
...@@ -16,7 +16,7 @@ class Myself ...@@ -16,7 +16,7 @@ class Myself
public $nickname; public $nickname;
public $userName; public $username;
public $uin; public $uin;
...@@ -34,7 +34,7 @@ class Myself ...@@ -34,7 +34,7 @@ class Myself
public function init($user) public function init($user)
{ {
$this->nickname = $user['NickName']; $this->nickname = $user['NickName'];
$this->userName = $user['UserName']; $this->username = $user['UserName'];
$this->sex = $user['Sex']; $this->sex = $user['Sex'];
$this->uin= $user['Uin']; $this->uin= $user['Uin'];
} }
......
...@@ -265,8 +265,8 @@ class Server ...@@ -265,8 +265,8 @@ class Server
http()->json($url, [ http()->json($url, [
'BaseRequest' => $this->baseRequest, 'BaseRequest' => $this->baseRequest,
'Code' => 3, 'Code' => 3,
'FromUserName' => myself()->userName, 'FromUserName' => myself()->username,
'ToUserName' => myself()->userName, 'ToUserName' => myself()->username,
'ClientMsgId' => time() 'ClientMsgId' => time()
]); ]);
} }
......
...@@ -85,7 +85,7 @@ class Message ...@@ -85,7 +85,7 @@ class Message
$this->setFromType(); $this->setFromType();
$this->setType(); $this->setType();
$this->rawMsg['selector'] = $selector; $this->rawMsg['selector'] = $selector;
$this->addMessageCollection();
return $this; return $this;
} }
...@@ -109,7 +109,7 @@ class Message ...@@ -109,7 +109,7 @@ class Message
$this->fromType = 'System'; $this->fromType = 'System';
} elseif ($this->rawMsg['MsgType'] == 37) { } elseif ($this->rawMsg['MsgType'] == 37) {
$this->fromType = 'FriendRequest'; $this->fromType = 'FriendRequest';
} elseif ($this->rawMsg['FromUserName'] === myself()->userName) { } elseif ($this->rawMsg['FromUserName'] === myself()->username) {
$this->fromType = 'Self'; $this->fromType = 'Self';
} elseif ($this->rawMsg['ToUserName'] === 'filehelper') { } elseif ($this->rawMsg['ToUserName'] === 'filehelper') {
$this->fromType = 'FileHelper'; $this->fromType = 'FileHelper';
...@@ -155,6 +155,7 @@ class Message ...@@ -155,6 +155,7 @@ class Message
*/ */
private function handleMessageByType() private function handleMessageByType()
{ {
print_r($this->rawMsg);
switch($this->rawMsg['MsgType']){ switch($this->rawMsg['MsgType']){
case 1: case 1:
if(Location::isLocation($this->rawMsg)){ if(Location::isLocation($this->rawMsg)){
...@@ -169,13 +170,13 @@ class Message ...@@ -169,13 +170,13 @@ class Message
$this->type = 'Image'; $this->type = 'Image';
$this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey); $this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content); $content = http()->get($this->content);
FileManager::download(time().$this->rawMsg['MsgId'].'.jpg', $content, 'jpg'); FileManager::download($this->rawMsg['MsgId'].'.jpg', $content, 'jpg');
break; break;
case 34: case 34:
$this->type = 'Voice'; $this->type = 'Voice';
$this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey); $this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content); $content = http()->get($this->content);
FileManager::download(time().$this->rawMsg['MsgId'].'.mp3', $content, 'mp3'); FileManager::download($this->rawMsg['MsgId'].'.mp3', $content, 'mp3');
break; break;
case 37: case 37:
$this->type = 'AddUser'; $this->type = 'AddUser';
...@@ -197,7 +198,13 @@ class Message ...@@ -197,7 +198,13 @@ class Message
$this->type = 'VideoCall'; $this->type = 'VideoCall';
break; break;
case 10002: case 10002:
$this->type = 'Redraw'; $this->type = 'Recall'; // 撤回
$msgId = $this->parseMsgId($this->rawMsg['Content']);
$message = message()->get($msgId);
$nickname = $message['sender'] ? $message['sender']['NickName'] : account()->getAccount($message['username'])['NickName'];
print_r($message);
Console::log('nickname:'.$nickname);
$this->content = "{$nickname} 刚撤回了消息 \"{$message['content']}\"";
break; break;
case 10000: case 10000:
if($this->rawMsg['Status'] == 4){ if($this->rawMsg['Status'] == 4){
...@@ -219,7 +226,7 @@ class Message ...@@ -219,7 +226,7 @@ class Message
*/ */
private function handleGroupContent($content) private function handleGroupContent($content)
{ {
if(!$content){ if(!$content || $this->rawMsg['MsgType'] == 10002){
return; return;
} }
list($uid, $content) = explode('<br/>', $content, 2); list($uid, $content) = explode('<br/>', $content, 2);
...@@ -235,6 +242,34 @@ class Message ...@@ -235,6 +242,34 @@ class Message
} }
/** /**
* 解析message获取msgId
*
* @param $xml
* @return string msgId
*/
private function parseMsgId($xml)
{
preg_match('/<msgid>(\d+)<\/msgid>/', $xml, $matches);
return $matches[1];
}
/**
* 存储消息到 Message 集合
*/
public function addMessageCollection()
{
message()->put($this->rawMsg['MsgId'], [
'content' => $this->content,
'username' => $this->username,
'sender' => $this->sender,
'msg_type' => $this->rawMsg['MsgType'],
'type' => $this->type,
'created_at' => $this->rawMsg['CreateTime'],
'from_type' => $this->fromType
]);
}
/**
* 发送消息 * 发送消息
* *
* @param $word string 消息内容 * @param $word string 消息内容
...@@ -254,7 +289,7 @@ class Message ...@@ -254,7 +289,7 @@ class Message
'Msg' => [ 'Msg' => [
'Type' => 1, 'Type' => 1,
'Content' => $word, 'Content' => $word,
'FromUserName' => myself()->userName, 'FromUserName' => myself()->username,
'ToUserName' => $username, 'ToUserName' => $username,
'LocalID' => $random, 'LocalID' => $random,
'ClientMsgId' => $random, 'ClientMsgId' => $random,
......
...@@ -12,6 +12,7 @@ use Hanson\Robot\Core\Http; ...@@ -12,6 +12,7 @@ use Hanson\Robot\Core\Http;
use Hanson\Robot\Collections\Account; use Hanson\Robot\Collections\Account;
use Hanson\Robot\Collections\Member; use Hanson\Robot\Collections\Member;
use Hanson\Robot\Collections\Contact; use Hanson\Robot\Collections\Contact;
use Hanson\Robot\Collections\Message;
use Hanson\Robot\Collections\Group; use Hanson\Robot\Collections\Group;
if (! function_exists('server')) { if (! function_exists('server')) {
...@@ -91,4 +92,15 @@ if (! function_exists('group')) { ...@@ -91,4 +92,15 @@ if (! function_exists('group')) {
{ {
return Group::getInstance(); return Group::getInstance();
} }
}
if (! function_exists('message')) {
/**
* Get the available container instance.
*
* @return Message
*/
function message()
{
return Message::getInstance();
}
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!