Commit ebfb9d7f by HanSon

处理content

1 parent 1889119d
......@@ -18,6 +18,18 @@ Robot->special
Robot->contact
Robot->group
# API
## Message
### sender
* user 发送者
> * uid 发送者ID
> *
* gid 群聊ID
* type 消息发送者类型
# what can wx-robot do?
* 转发消息
......
......@@ -90,10 +90,16 @@ class MessageHandler
}
$message = $this->sync();
// Message::make($selector, $message);
// print_r($message);
foreach ($message['AddMsgList'] as $msg) {
}
Log::echo(json_encode($message));
// $messages = (new Message)->make($selector, $message);
//
// foreach ($messages as $message) {
// call_user_func_array($this->handler, [$message]);
// }
}
/**
......
......@@ -11,10 +11,16 @@ namespace Hanson\Robot\Message;
use Hanson\Robot\Core\Server;
use Hanson\Robot\Models\Account;
use Hanson\Robot\Models\ContactAccount;
use Hanson\Robot\Models\OfficialAccount;
use Hanson\Robot\Models\SpecialAccount;
class Message
{
/**
* @
*/
public $sender;
public $receiver;
......@@ -27,7 +33,7 @@ class Message
static $message = [];
const USER_MAP = [
const USER_TYPE = [
0 => 'Init',
1 => 'Self',
2 => 'FileHelper',
......@@ -38,9 +44,16 @@ class Message
99 => 'UnKnown',
];
public function make($selector, $message)
public $rawMsg;
// const MESSAGE_TYPE = [
// 0 => 'Text',
// ]
public function make($selector, $msg)
{
$msg = $message['AddMsgList'][0];
$this->rawMsg = $msg;
if ($msg['MsgType'] == 51) {
$this->sender->name = 'system';
......@@ -53,10 +66,74 @@ class Message
} elseif ($msg['ToUserName'] === 'filehelper') {
$this->sender->name = 'file_helper';
$this->sender->type = 2;
} elseif (substr($msg['FromUserName'], 0, 2) === '@@'){
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::GROUP_MEMBER, true);
} elseif (substr($msg['FromUserName'], 0, 2) === '@@') {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 3;
} elseif (ContactAccount::getInstance()->isContact($msg['FromUserName'])) {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 4;
} elseif (OfficialAccount::getInstance()->isPublic($msg['FromUserName'])) {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 5;
} elseif (SpecialAccount::getInstance()->get($msg['FromUserName'], false)) {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 6;
} else {
$this->sender->name = 'unknown';
$this->sender->type = 99;
}
$this->sender->name = html_entity_decode($this->sender->name);
$this->handleContent();
}
private function handleContent()
{
// $msgType = $msg['MsgType'];
$content = html_entity_decode($this->rawMsg['Content']);
// $msgId = $msg['MsgId'];
$this->handleContentByType($content);
$this->handleMessageByType();
}
private function handleContentByType($content)
{
if($this->sender->type === 0){
$this->type = 'Empty';
}elseif ($this->sender->type === 2){
$this->type = 'Text';
$this->content = $this->formatContent($content);
}elseif ($this->sender->type === 3){
$this->handleGroupContent($content);
}
}
private function handleMessageByType()
{
if($this->rawMsg['MsgType'] == 1){
}
}
/**
* handle group content
*
* @param $content
*/
private function handleGroupContent($content)
{
list($uid, $content) = explode('<br/>', $content, 2);
$this->sender->user = Account::getInstance()->get('normalMember')[substr($uid, 0, -1)];
$this->content = $this->formatContent($content);
}
private function formatContent($content)
{
return str_replace('<br/>', '\n', $content);
}
}
\ No newline at end of file
......@@ -14,6 +14,9 @@ use Illuminate\Support\Collection;
class ContactAccount extends Collection
{
/**
* @var ContactAccount
*/
static $instance = null;
/**
......@@ -30,4 +33,9 @@ class ContactAccount extends Collection
return static::$instance;
}
public function isContact($id)
{
return static::$instance->get($id, false);
}
}
\ No newline at end of file
......@@ -14,6 +14,9 @@ use Illuminate\Support\Collection;
class OfficialAccount extends Collection
{
/**
* @var OfficialAccount
*/
static $instance = null;
/**
......@@ -30,4 +33,9 @@ class OfficialAccount extends Collection
return static::$instance;
}
public function isPublic($id)
{
return static::$instance->get($id, false);
}
}
\ 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!