Commit 0e2c03ad by hanccc

构造message

1 parent 38cdcdf8
......@@ -48,7 +48,7 @@ class Account extends Collection
$account[static::GROUP_MEMBER][] = $groupMember;
static::$instance->make($account);
static::$instance = static::$instance->make($account);
}
/**
......@@ -62,7 +62,7 @@ class Account extends Collection
$account[static::NORMAL_MEMBER][] = $normalMember;
static::$instance->make($account);
static::$instance = static::$instance->make($account);
}
/**
......@@ -100,12 +100,11 @@ class Account extends Collection
* 获取联系人
*
* @param $id
* @param string $type 类型
* @return array
*/
public function getContact($id, $type)
public function getContact($id)
{
$target = static::$instance->get($type);
$target = static::$instance->get(static::NORMAL_MEMBER);
return $target[$id] ?? null;
}
......
......@@ -67,7 +67,9 @@ class ContactFactory
}
Account::getInstance()->addNormalMember([$contact['UserName'] => ['type' => $type, 'info' => $contact]]);
}
$this->getBatchGroupMembers();
file_put_contents($this->server->config['tmp'] . 'account.json', json_encode(Account::getInstance()->all()));
}
/**
......
......@@ -9,6 +9,7 @@
namespace Hanson\Robot\Core;
use Closure;
use Hanson\Robot\Collections\Account;
use Hanson\Robot\Message\Message;
use Hanson\Robot\Support\Log;
......@@ -90,16 +91,30 @@ class MessageHandler
}
$message = $this->sync();
foreach ($message['AddMsgList'] as $msg) {
Log::echo((new Message)->make($selector, $msg)->content->msg);
$content = (new Message)->make($selector, $msg);
$response = call_user_func_array($this->handler, [$content]);
$this->send($response, $content);
}
// Log::echo(json_encode($message));
}
private function send($response, $content)
{
if(!$response && !is_string($response)){
return false;
}
Log::echo(json_encode($message));
// $messages = (new Message)->make($selector, $message);
//
// foreach ($messages as $message) {
// call_user_func_array($this->handler, [$message]);
// }
$this->server->http->json(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . $this->server->passTicket, [
'BaseRequest' => $this->server->baseRequest,
'Msg' => [
'Type' => 1,
'Content' => urlencode($response),
'FromUserName' => $this->server->getMyAccount(),
'ToUserName' => $content->to
]
]);
}
/**
......
......@@ -264,6 +264,11 @@ class Server
return $fromUserName === static::$myAccount['UserName'];
}
public function getMyAccount()
{
return static::$myAccount['UserName'];
}
public function setMessageHandler(\Closure $closure)
{
if(!is_callable($closure)){
......
......@@ -76,7 +76,7 @@ class Message
$this->setTo();
$this->setContent();
// $this->setContent();
$this->setType();
......@@ -90,32 +90,12 @@ class Message
*/
private function setFrom()
{
$account = Account::getInstance();
$from = $this->rawMsg['FromUserName'];
$fromType = substr($this->rawMsg['FromUserName'], 0, 2) === '@@' ? Account::GROUP_MEMBER : Account::NORMAL_MEMBER;
$this->from = $account->getContact($from, $fromType);
//
// if($this->sender->type !== 'Group'){
// $this->sender->from = $account->getContact($this->rawMsg['FromUserName'], Account::NORMAL_MEMBER);
// }
//
// $this->sender->name = html_entity_decode($this->sender->name);
$this->from = Account::getInstance()->getContact($this->rawMsg['FromUserName']);
}
private function setTo()
{
$account = Account::getInstance();
$from = $this->rawMsg['ToUserName'];
$fromType = substr($this->rawMsg['ToUserName'], 0, 2) === '@@' ? Account::GROUP_MEMBER : Account::NORMAL_MEMBER;
$this->to = $account->getContact($from, $fromType);
$this->to = Account::getInstance()->getContact($this->rawMsg['ToUserName']);
}
private function setFromType()
......@@ -197,8 +177,24 @@ class Message
$this->type = 'Animation';
break;
case 49:
$this->type = 'Animation';
$this->type = 'Share';
break;
case 62:
$this->type = 'Video';
break;
case 53:
$this->type = 'VideoCall';
break;
case 10002:
$this->type = 'Redraw';
break;
case 10000:
$this->type = 'Unknown';
break;
default:
$this->type = 'Unknown';
break;
}
}
......
......@@ -36,5 +36,7 @@ $robot->server->setMessageHandler(function($message){
// # do something;
// }
print_r($message);
});
$robot->server->run();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!