Commit 01f92cf9 by hanccc

增加撤回

(cherry picked from commit 528517201c04eec76afac225076272f433fdac8f)
1 parent 7284522d
......@@ -303,6 +303,7 @@ $robot->server->setCustomHandler(function(){
# to do list
- [ ] 命令行操作信息发送
- [ ] 增加消息集合存储
- [ ] 消息处理
# 已知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
{
$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
<?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
......@@ -16,7 +16,7 @@ class Myself
public $nickname;
public $userName;
public $username;
public $uin;
......@@ -34,7 +34,7 @@ class Myself
public function init($user)
{
$this->nickname = $user['NickName'];
$this->userName = $user['UserName'];
$this->username = $user['UserName'];
$this->sex = $user['Sex'];
$this->uin= $user['Uin'];
}
......
......@@ -265,8 +265,8 @@ class Server
http()->json($url, [
'BaseRequest' => $this->baseRequest,
'Code' => 3,
'FromUserName' => myself()->userName,
'ToUserName' => myself()->userName,
'FromUserName' => myself()->username,
'ToUserName' => myself()->username,
'ClientMsgId' => time()
]);
}
......
......@@ -85,7 +85,7 @@ class Message
$this->setFromType();
$this->setType();
$this->rawMsg['selector'] = $selector;
$this->addMessageCollection();
return $this;
}
......@@ -109,7 +109,7 @@ class Message
$this->fromType = 'System';
} elseif ($this->rawMsg['MsgType'] == 37) {
$this->fromType = 'FriendRequest';
} elseif ($this->rawMsg['FromUserName'] === myself()->userName) {
} elseif ($this->rawMsg['FromUserName'] === myself()->username) {
$this->fromType = 'Self';
} elseif ($this->rawMsg['ToUserName'] === 'filehelper') {
$this->fromType = 'FileHelper';
......@@ -155,6 +155,7 @@ class Message
*/
private function handleMessageByType()
{
print_r($this->rawMsg);
switch($this->rawMsg['MsgType']){
case 1:
if(Location::isLocation($this->rawMsg)){
......@@ -169,13 +170,13 @@ class Message
$this->type = 'Image';
$this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download(time().$this->rawMsg['MsgId'].'.jpg', $content, 'jpg');
FileManager::download($this->rawMsg['MsgId'].'.jpg', $content, 'jpg');
break;
case 34:
$this->type = 'Voice';
$this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download(time().$this->rawMsg['MsgId'].'.mp3', $content, 'mp3');
FileManager::download($this->rawMsg['MsgId'].'.mp3', $content, 'mp3');
break;
case 37:
$this->type = 'AddUser';
......@@ -197,7 +198,13 @@ class Message
$this->type = 'VideoCall';
break;
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;
case 10000:
if($this->rawMsg['Status'] == 4){
......@@ -219,7 +226,7 @@ class Message
*/
private function handleGroupContent($content)
{
if(!$content){
if(!$content || $this->rawMsg['MsgType'] == 10002){
return;
}
list($uid, $content) = explode('<br/>', $content, 2);
......@@ -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 消息内容
......@@ -254,7 +289,7 @@ class Message
'Msg' => [
'Type' => 1,
'Content' => $word,
'FromUserName' => myself()->userName,
'FromUserName' => myself()->username,
'ToUserName' => $username,
'LocalID' => $random,
'ClientMsgId' => $random,
......
......@@ -12,6 +12,7 @@ use Hanson\Robot\Core\Http;
use Hanson\Robot\Collections\Account;
use Hanson\Robot\Collections\Member;
use Hanson\Robot\Collections\Contact;
use Hanson\Robot\Collections\Message;
use Hanson\Robot\Collections\Group;
if (! function_exists('server')) {
......@@ -91,4 +92,15 @@ if (! function_exists('group')) {
{
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
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!