Commit b29bc04a by hanccc

完善群组成员

1 parent 61406210
......@@ -29,9 +29,11 @@ class Http
return $this->request($url, 'POST', [$key => $options]);
}
public function json($url, $options = [])
public function json($url, $options = [], $array = false)
{
return $this->request($url, 'POST', ['json' => $options]);
$content = $this->request($url, 'POST', ['json' => $options]);
return $array ? json_decode($content, true) : $content;
}
public function setClient(HttpClient $client)
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/14
* Time: 23:08
*/
namespace Hanson\Robot\Core;
use Closure;
class MessageHandler
{
private $handler;
public function setMessageHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('message handler must be a closure!');
}
$this->handler = $closure;
}
public function listen()
{
call_user_func_array($this->handler, []);
}
}
\ No newline at end of file
......@@ -44,7 +44,9 @@ class Server
public $http;
protected $config;
public $config;
public $messageHandler;
protected $debug = false;
......@@ -68,10 +70,11 @@ class Server
Log::echo('[INFO] init success!');
$this->statusNotify();
Log::echo('[INFO] begin to init contacts');
$this->initContact();
Log::echo('[INFO] init contacts success!');
print_r(GroupAccount::getInstance()->first());
MessageHandler::listen();
}
public function prepare()
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/15
* Time: 0:12
*/
namespace Hanson\Robot\Message;
class Message
{
}
\ No newline at end of file
......@@ -44,24 +44,73 @@ class ContactFactory
$this->makeContactList($memberList);
}
/**
* make instance model
*
* @param $memberList
*/
protected function makeContactList($memberList)
{
foreach ($memberList as $contact) {
if($contact['VerifyFlag'] & 8 != 0){ #公众号
$type = 'public';
OfficialAccount::getInstance()->push($contact);
OfficialAccount::getInstance()->put($contact['UserName'], $contact);
}elseif (in_array($contact['UserName'], static::SPECIAL_USERS)){ # 特殊账户
$type = 'special';
SpecialAccount::getInstance()->push($contact);
SpecialAccount::getInstance()->put($contact['UserName'], $contact);
}elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊
$type = 'group';
GroupAccount::getInstance()->push($contact);
GroupAccount::getInstance()->put($contact['UserName'], $contact);
}else{
$type = 'contact';
ContactAccount::getInstance()->push($contact);
ContactAccount::getInstance()->put($contact['UserName'], $contact);
}
Account::getInstance()->put($contact['UserName'], ['type' => $type, 'info' => $contact]);
}
$this->getBatchGroupMembers();
}
/**
* get group members by api
*/
public function getBatchGroupMembers()
{
$url = sprintf(Server::BASE_URI . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), $this->server->passTicket);
$list = [];
GroupAccount::getInstance()->each(function($item, $key) use (&$list){
$list[] = ['UserName' => $key, 'EncryChatRoomId' => ''];
});
file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode([
'BaseRequest' => $this->server->baseRequest,
'Count' => GroupAccount::getInstance()->count(),
'List' => $list
]));
$content = $this->server->http->json($url, [
'BaseRequest' => $this->server->baseRequest,
'Count' => GroupAccount::getInstance()->count(),
'List' => $list
], true);
$this->initGroupMembers($content);
}
/**
* init group members and chat room id
*
* @param $array
*/
private function initGroupMembers($array)
{
foreach ($array['ContactList'] as $group) {
$groupAccount = GroupAccount::getInstance()->get($group['UserName']);
$groupAccount['MemberList'] = $group['MemberList'];
$groupAccount['ChatRoomId'] = $group['EncryChatRoomId'];
GroupAccount::getInstance()->put($group['UserName'], $groupAccount);
}
}
}
\ 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!