ContactFactory.php
4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/12
* Time: 20:41
*/
namespace Hanson\Robot\Collections;
use Hanson\Robot\Core\Server;
use Hanson\Robot\Support\Console;
class ContactFactory
{
// protected $server;
const SPECIAL_USERS = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail',
'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle',
'lbsapp', 'shakeapp', 'medianote', 'qqfriend', 'readerapp',
'blogapp', 'facebookapp', 'masssendapp', 'meishiapp',
'feedsapp', 'voip', 'blogappweixin', 'weixin', 'brandsessionholder',
'weixinreminder', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c',
'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11',
'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages'];
public function __construct()
{
$this->getContacts();
}
public function getContacts()
{
$url = sprintf(Server::BASE_URI . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s', server()->passTicket, server()->skey, time());
$content = http()->json($url, [
'BaseRequest' => server()->baseRequest
], true);
// file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode($content));
$this->makeContactList($content['MemberList']);
}
/**
* make instance model
*
* @param $memberList
*/
protected function makeContactList($memberList)
{
foreach ($memberList as $contact) {
if($contact['VerifyFlag'] & 8 != 0){ #公众号
$type = 'public';
OfficialAccount::getInstance()->put($contact['UserName'], $contact);
}elseif (in_array($contact['UserName'], static::SPECIAL_USERS)){ # 特殊账户
$type = 'special';
SpecialAccount::getInstance()->put($contact['UserName'], $contact);
}elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊
$type = 'group';
GroupAccount::getInstance()->put($contact['UserName'], $contact);
}else{
$type = 'contact';
ContactAccount::getInstance()->put($contact['UserName'], $contact);
}
Account::getInstance()->addNormalMember($contact['UserName'], ['type' => $type, 'info' => $contact]);
}
$this->getBatchGroupMembers();
file_put_contents(server()->config['tmp'] . 'account.json', json_encode(Account::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'OfficialAccount.json', json_encode(OfficialAccount::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'SpecialAccount.json', json_encode(SpecialAccount::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'GroupAccount.json', json_encode(GroupAccount::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'ContactAccount.json', json_encode(ContactAccount::getInstance()->all()));
}
/**
* get group members by api
*/
public function getBatchGroupMembers()
{
$url = sprintf(Server::BASE_URI . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), 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' => count($list),
// 'List' => $list
// ]));
$content = http()->json($url, [
'BaseRequest' => 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);
foreach ($group['MemberList'] as $member) {
Account::getInstance()->addGroupMember($member['UserName'], ['type' => 'groupMember', 'info' => $member, 'group' => $group]);
}
}
}
}