Commit d15fb1b9 by HanSon

修改了大部分collection

1 parent 288f7b2a
{ {
"name": "hanson/wx-bot", "name": "hanson/vbot",
"type": "library", "type": "library",
"license": "mit", "license": "mit",
"authors": [ "authors": [
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
"endroid/qrcode": "^1.7", "endroid/qrcode": "^1.7",
"symfony/css-selector": "^3.2", "symfony/css-selector": "^3.2",
"pimple/pimple": "^3.0", "pimple/pimple": "^3.0",
"illuminate/support": "^5.3" "illuminate/support": "^5.3",
"nesbot/carbon": "^1.21"
}, },
"autoload": { "autoload": {
"files": [ "files": [
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "6af130a3acc2b00f4ce8782188ada4cf", "hash": "37f52ae2334d2a7d5f4435c7d835c45a",
"content-hash": "a8ac3a77959b316bf6ef9568195ba937", "content-hash": "45acbc8560c0df766dc94338616aefe5",
"packages": [ "packages": [
{ {
"name": "doctrine/inflector", "name": "doctrine/inflector",
...@@ -461,6 +461,53 @@ ...@@ -461,6 +461,53 @@
"time": "2016-09-22 11:01:11" "time": "2016-09-22 11:01:11"
}, },
{ {
"name": "nesbot/carbon",
"version": "1.21.0",
"source": {
"type": "git",
"url": "https://github.com/briannesbitt/Carbon.git",
"reference": "7b08ec6f75791e130012f206e3f7b0e76e18e3d7"
},
"dist": {
"type": "zip",
"url": "https://packagist.phpcomposer.com/files/briannesbitt/Carbon/7b08ec6f75791e130012f206e3f7b0e76e18e3d7.zip",
"reference": "7b08ec6f75791e130012f206e3f7b0e76e18e3d7",
"shasum": ""
},
"require": {
"php": ">=5.3.0",
"symfony/translation": "~2.6|~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Carbon\\": "src/Carbon/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Brian Nesbitt",
"email": "brian@nesbot.com",
"homepage": "http://nesbot.com"
}
],
"description": "A simple API extension for DateTime.",
"homepage": "http://carbon.nesbot.com",
"keywords": [
"date",
"datetime",
"time"
],
"time": "2015-11-04 20:07:17"
},
{
"name": "paragonie/random_compat", "name": "paragonie/random_compat",
"version": "v2.0.4", "version": "v2.0.4",
"source": { "source": {
...@@ -867,6 +914,70 @@ ...@@ -867,6 +914,70 @@
"shim" "shim"
], ],
"time": "2016-11-14 01:06:16" "time": "2016-11-14 01:06:16"
},
{
"name": "symfony/translation",
"version": "v3.2.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/translation.git",
"reference": "5fd18eca88f4d187807a1eba083bc99feaa8635b"
},
"dist": {
"type": "zip",
"url": "https://packagist.phpcomposer.com/files/symfony/translation/5fd18eca88f4d187807a1eba083bc99feaa8635b.zip",
"reference": "5fd18eca88f4d187807a1eba083bc99feaa8635b",
"shasum": ""
},
"require": {
"php": ">=5.5.9",
"symfony/polyfill-mbstring": "~1.0"
},
"conflict": {
"symfony/config": "<2.8"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/config": "~2.8|~3.0",
"symfony/intl": "~2.8|~3.0",
"symfony/yaml": "~2.8|~3.0"
},
"suggest": {
"psr/log": "To use logging capability in translator",
"symfony/config": "",
"symfony/yaml": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Translation\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Translation Component",
"homepage": "https://symfony.com",
"time": "2016-11-30 14:40:17"
} }
], ],
"packages-dev": [ "packages-dev": [
......
<?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;
$robot = new Robot([
'tmp' => __DIR__ . './../tmp/',
]);
$flag = false;
$robot->server->setCustomerHandler(function() use (&$flag){
/** @var $message Message */
if(!$flag){
Message::send('custom', contact()->getUsernameById('L907159127'));
$flag = true;
}
Message::send('测试' . \Carbon\Carbon::now()->toDateTimeString(), contact()->getUsernameById('L907159127'));
});
$robot->server->run();
...@@ -8,18 +8,19 @@ ...@@ -8,18 +8,19 @@
require_once __DIR__ . './../vendor/autoload.php'; require_once __DIR__ . './../vendor/autoload.php';
$robot = new \Hanson\Robot\Foundation\Robot([ use Hanson\Robot\Foundation\Robot;
'tmp' => realpath('./tmp') . '/', use Hanson\Robot\Message\Message;
'debug' => true,
]);
$client = new \GuzzleHttp\Client(); $robot = new Robot([
'tmp' => __DIR__ . './../tmp/',
'debug' => true
]);
$robot->server->setMessageHandler(function($message) use ($client, $robot){ $robot->server->setMessageHandler(function($message){
if($message->type === 'Text'){ if($message->type === 'Text'){
$contact = \Hanson\Robot\Collections\Account::getInstance()->getUsernameById('hanson1994'); /** @var $message Message */
\Hanson\Robot\Message\Message::send('hi', $message->rawMsg['FromUserName']); $contact = contact()->getUsernameById('L907159127');
\Hanson\Robot\Message\Message::send($message->content, $contact); Message::send($message->content, $contact);
} }
}); });
$robot->server->setCustomerHandler(function(){ $robot->server->setCustomerHandler(function(){
......
...@@ -8,17 +8,17 @@ ...@@ -8,17 +8,17 @@
require_once __DIR__ . './../vendor/autoload.php'; require_once __DIR__ . './../vendor/autoload.php';
$robot = new \Hanson\Robot\Foundation\Robot([ use Hanson\Robot\Foundation\Robot;
'tmp' => realpath('./tmp') . '/', use Hanson\Robot\Message\Message;
'debug' => true,
]);
$client = new \GuzzleHttp\Client(); $robot = new Robot([
'tmp' => __DIR__ . './../tmp/',
]);
$robot->server->setMessageHandler(function($message) use ($client, $robot){ $robot->server->setMessageHandler(function($message){
/** @var $message \Hanson\Robot\Message\Message */ /** @var $message Message */
if($message->type === 'Text'){ if($message->type === 'Text'){
\Hanson\Robot\Message\Message::send($message->fromType, $message->username); Message::send($message->fromType, $message->username);
} }
}); });
$robot->server->run(); $robot->server->run();
<?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;
$robot = new Robot([
'tmp' => __DIR__ . './../tmp/',
'debug' => true
]);
$flag = false;
$robot->server->setCustomerHandler(function() use (&$flag){
/** @var $message Message */
Message::send('测试' . \Carbon\Carbon::now()->toDateTimeString(), contact()->getUsernameById('L907159127'));
});
$robot->server->run();
...@@ -8,23 +8,23 @@ ...@@ -8,23 +8,23 @@
require_once __DIR__ . './../vendor/autoload.php'; require_once __DIR__ . './../vendor/autoload.php';
$robot = new \Hanson\Robot\Foundation\Robot([ use Hanson\Robot\Foundation\Robot;
'tmp' => realpath('./tmp') . '/', use Hanson\Robot\Message\Message;
'debug' => true,
]);
$client = new \GuzzleHttp\Client(); $robot = new Robot([
'tmp' => __DIR__ . './../tmp/',
]);
$robot->server->setMessageHandler(function($message) use ($client, $robot){ $robot->server->setMessageHandler(function($message){
/** @var $message Message */
if($message->type === 'Text'){ if($message->type === 'Text'){
$url = 'http://www.tuling123.com/openapi/api'; $url = 'http://www.tuling123.com/openapi/api';
$result = $robot->server->http->post($url, [ $result = http()->post($url, [
'key' => '1dce02aef026258eff69635a06b0ab7d', 'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->content 'info' => $message->content
], true); ], true);
print_r($message);
print_r($result);
return $result['text']; return $result['text'];
} }
}); });
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/13
* Time: 20:56
*/
namespace Hanson\Robot\Collections;
use Illuminate\Support\Collection;
class Contact extends Collection
{
/**
* @var Contact
*/
static $instance = null;
/**
* create a single instance
*
* @return Contact
*/
public static function getInstance()
{
if(static::$instance === null){
static::$instance = new Contact();
}
return static::$instance;
}
public function isContact($id)
{
return static::$instance->get($id, false);
}
/**
* 根据username获取联系人
*
* @param $id
* @return array
*/
public function getContactByUsername($id)
{
$contact = $this->get($id);
return $contact ?? null;
}
/**
* 根据微信号获取联系人
*
* @param $id
* @return mixed
*/
public function getContactById($id)
{
$contact = $this->filter(function($item, $key) use ($id){
if($item['Alias'] === $id){
return true;
}
})->first();
return $contact;
}
/**
* 根据微信号获取联系username
*
* @param $id
* @return mixed
*/
public function getUsernameById($id)
{
$contact = $this->search(function($item, $key) use ($id){
if($item['Alias'] === $id){
return true;
}
});
return $contact;
}
}
\ No newline at end of file \ No newline at end of file
...@@ -14,7 +14,6 @@ use Hanson\Robot\Support\Console; ...@@ -14,7 +14,6 @@ use Hanson\Robot\Support\Console;
class ContactFactory class ContactFactory
{ {
// protected $server;
const SPECIAL_USERS = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail', const SPECIAL_USERS = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail',
'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle', 'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle',
...@@ -37,7 +36,6 @@ class ContactFactory ...@@ -37,7 +36,6 @@ class ContactFactory
$content = http()->json($url, [ $content = http()->json($url, [
'BaseRequest' => server()->baseRequest 'BaseRequest' => server()->baseRequest
], true); ], true);
// file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode($content));
$this->makeContactList($content['MemberList']); $this->makeContactList($content['MemberList']);
} }
...@@ -51,50 +49,41 @@ class ContactFactory ...@@ -51,50 +49,41 @@ class ContactFactory
{ {
foreach ($memberList as $contact) { foreach ($memberList as $contact) {
if($contact['VerifyFlag'] & 8 != 0){ #公众号 if($contact['VerifyFlag'] & 8 != 0){ #公众号
$type = 'public';
OfficialAccount::getInstance()->put($contact['UserName'], $contact); OfficialAccount::getInstance()->put($contact['UserName'], $contact);
}elseif (in_array($contact['UserName'], static::SPECIAL_USERS)){ # 特殊账户 }elseif (in_array($contact['UserName'], static::SPECIAL_USERS)){ # 特殊账户
$type = 'special';
SpecialAccount::getInstance()->put($contact['UserName'], $contact); SpecialAccount::getInstance()->put($contact['UserName'], $contact);
}elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊 }elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊
$type = 'group'; group()->put($contact['UserName'], $contact);
GroupAccount::getInstance()->put($contact['UserName'], $contact);
}else{ }else{
$type = 'contact'; contact()->put($contact['UserName'], $contact);
ContactAccount::getInstance()->put($contact['UserName'], $contact);
} }
Account::getInstance()->addNormalMember($contact['UserName'], ['type' => $type, 'info' => $contact]);
} }
$this->getBatchGroupMembers(); $this->getBatchGroupMembers();
file_put_contents(server()->config['tmp'] . 'account.json', json_encode(Account::getInstance()->all())); if(server()->config['debug']){
file_put_contents(server()->config['tmp'] . 'contact.json', json_encode(contact()->all()));
file_put_contents(server()->config['tmp'] . 'member.json', json_encode(member()->all()));
file_put_contents(server()->config['tmp'] . 'group.json', json_encode(group()->all()));
file_put_contents(server()->config['tmp'] . 'OfficialAccount.json', json_encode(OfficialAccount::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'] . '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() public function getBatchGroupMembers()
{ {
$url = sprintf(Server::BASE_URI . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), server()->passTicket); $url = sprintf(Server::BASE_URI . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), server()->passTicket);
$list = []; $list = [];
GroupAccount::getInstance()->each(function($item, $key) use (&$list){ group()->each(function($item, $key) use (&$list){
$list[] = ['UserName' => $key, 'EncryChatRoomId' => '']; $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, [ $content = http()->json($url, [
'BaseRequest' => server()->baseRequest, 'BaseRequest' => server()->baseRequest,
'Count' => GroupAccount::getInstance()->count(), 'Count' => group()->count(),
'List' => $list 'List' => $list
], true); ], true);
...@@ -102,19 +91,19 @@ class ContactFactory ...@@ -102,19 +91,19 @@ class ContactFactory
} }
/** /**
* init group members and chat room id * 初始化群组成员
* *
* @param $array * @param $array
*/ */
private function initGroupMembers($array) private function initGroupMembers($array)
{ {
foreach ($array['ContactList'] as $group) { foreach ($array['ContactList'] as $group) {
$groupAccount = GroupAccount::getInstance()->get($group['UserName']); $groupAccount = group()->get($group['UserName']);
$groupAccount['MemberList'] = $group['MemberList']; $groupAccount['MemberList'] = $group['MemberList'];
$groupAccount['ChatRoomId'] = $group['EncryChatRoomId']; $groupAccount['ChatRoomId'] = $group['EncryChatRoomId'];
GroupAccount::getInstance()->put($group['UserName'], $groupAccount); group()->put($group['UserName'], $groupAccount);
foreach ($group['MemberList'] as $member) { foreach ($group['MemberList'] as $member) {
Account::getInstance()->addGroupMember($member['UserName'], ['type' => 'groupMember', 'info' => $member, 'group' => $group]); member()->put($member['UserName'], $member);
} }
} }
......
...@@ -11,7 +11,7 @@ namespace Hanson\Robot\Collections; ...@@ -11,7 +11,7 @@ namespace Hanson\Robot\Collections;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class GroupAccount extends Collection class Group extends Collection
{ {
static $instance = null; static $instance = null;
...@@ -19,19 +19,50 @@ class GroupAccount extends Collection ...@@ -19,19 +19,50 @@ class GroupAccount extends Collection
/** /**
* create a single instance * create a single instance
* *
* @return GroupAccount * @return Group
*/ */
public static function getInstance() public static function getInstance()
{ {
if(static::$instance === null){ if(static::$instance === null){
static::$instance = new GroupAccount(); static::$instance = new Group();
} }
return static::$instance; return static::$instance;
} }
/**
* 判断是否群组
*
* @param $userName
* @return bool
*/
public static function isGroup($userName){ public static function isGroup($userName){
return strstr($userName, '@@') !== false; return strstr($userName, '@@') !== false;
} }
/**
* 根据群名筛选群组
*
* @param $name
* @param bool $blur
* @param bool $onlyUsername
* @return static
*/
public function getGroupsByNickname($name, $blur = false, $onlyUsername = false)
{
$groups = $this->filter(function($value, $key) use ($name, $blur){
if($blur){
return $value['NickName'] === $name;
}else{
return str_contains($value['NickName'], $name);
}
});
if($onlyUsername){
$groups = $groups->only(['UserName']);
}
return $groups;
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -11,31 +11,39 @@ namespace Hanson\Robot\Collections; ...@@ -11,31 +11,39 @@ namespace Hanson\Robot\Collections;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class ContactAccount extends Collection class Member extends Collection
{ {
/** /**
* @var ContactAccount * @var Member
*/ */
static $instance = null; static $instance = null;
/** /**
* create a single instance * create a single instance
* *
* @return ContactAccount * @return Member
*/ */
public static function getInstance() public static function getInstance()
{ {
if(static::$instance === null){ if(static::$instance === null){
static::$instance = new ContactAccount(); static::$instance = new Member();
} }
return static::$instance; return static::$instance;
} }
public function isContact($id) /**
* 根据username获取群成员
*
* @param $id
* @return array
*/
public function getMemberByUsername($id)
{ {
return static::$instance->get($id, false); $member = $this->get($id);
return $member ?? null;
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -39,6 +39,12 @@ class MessageHandler ...@@ -39,6 +39,12 @@ class MessageHandler
return static::$instance; return static::$instance;
} }
/**
* 消息处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setMessageHandler(Closure $closure) public function setMessageHandler(Closure $closure)
{ {
if(!$closure instanceof Closure){ if(!$closure instanceof Closure){
...@@ -48,6 +54,12 @@ class MessageHandler ...@@ -48,6 +54,12 @@ class MessageHandler
$this->handler = $closure; $this->handler = $closure;
} }
/**
* 自定义处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setCustomHandler(Closure $closure) public function setCustomHandler(Closure $closure)
{ {
if(!$closure instanceof Closure){ if(!$closure instanceof Closure){
...@@ -66,7 +78,7 @@ class MessageHandler ...@@ -66,7 +78,7 @@ class MessageHandler
while (true){ while (true){
if($this->customHandler){ if($this->customHandler instanceof Closure){
call_user_func_array($this->customHandler, []); call_user_func_array($this->customHandler, []);
} }
...@@ -96,35 +108,11 @@ class MessageHandler ...@@ -96,35 +108,11 @@ class MessageHandler
if($message['AddMsgList']){ if($message['AddMsgList']){
foreach ($message['AddMsgList'] as $msg) { foreach ($message['AddMsgList'] as $msg) {
$content = (new Message)->make($selector, $msg); $content = (new Message)->make($selector, $msg);
$response = call_user_func_array($this->handler, [$content]); if($this->handler instanceof Closure){
$this->send($response, $content); $reply = call_user_func_array($this->handler, [$content]);
} Message::send($reply, $content->username);
} }
} }
private function send($word, $content)
{
if(!$word && !is_string($word)){
return false;
}
$random = strval(time() * 1000) . '0' . strval(rand(100, 999));
$result = http()->post(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . server()->passTicket,
json_encode([
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 1,
'Content' => $word,
'FromUserName' => myself()->userName,
'ToUserName' => $content->rawMsg['FromUserName'],
'LocalID' => $random,
'ClientMsgId' => $random,
],
'Scene' => 0
], JSON_UNESCAPED_UNICODE), true);
if($result['BaseResponse']['Ret'] != 0){
Console::log('发送消息失败');
} }
} }
......
...@@ -13,7 +13,7 @@ use Endroid\QrCode\QrCode; ...@@ -13,7 +13,7 @@ use Endroid\QrCode\QrCode;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Hanson\Robot\Collections\Account; use Hanson\Robot\Collections\Account;
use Hanson\Robot\Collections\ContactFactory; use Hanson\Robot\Collections\ContactFactory;
use Hanson\Robot\Collections\GroupAccount; use Hanson\Robot\Collections\Group;
use Hanson\Robot\Support\Console; use Hanson\Robot\Support\Console;
use QueryPath\Exception; use QueryPath\Exception;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
...@@ -56,6 +56,8 @@ class Server ...@@ -56,6 +56,8 @@ class Server
public function __construct($config = []) public function __construct($config = [])
{ {
$this->config = $config; $this->config = $config;
$this->config['debug'] = $this->config['debug'] ?? false;
} }
/** /**
...@@ -241,9 +243,8 @@ class Server ...@@ -241,9 +243,8 @@ class Server
{ {
if($contactList){ if($contactList){
foreach ($contactList as $contact) { foreach ($contactList as $contact) {
if(GroupAccount::isGroup($contact['UserName'])){ if(Group::isGroup($contact['UserName'])){
GroupAccount::getInstance()->put($contact['UserName'], $contact); group()->put($contact['UserName'], $contact);
Account::getInstance()->addNormalMember($contact['UserName'], ['type' => 'group', 'info' => $contact]);
} }
} }
} }
......
...@@ -9,19 +9,20 @@ ...@@ -9,19 +9,20 @@
namespace Hanson\Robot\Message; namespace Hanson\Robot\Message;
use Carbon\Carbon;
use Hanson\Robot\Core\Server; use Hanson\Robot\Core\Server;
use Hanson\Robot\Collections\Account; use Hanson\Robot\Collections\Contact;
use Hanson\Robot\Collections\ContactAccount;
use Hanson\Robot\Collections\OfficialAccount; use Hanson\Robot\Collections\OfficialAccount;
use Hanson\Robot\Collections\SpecialAccount; use Hanson\Robot\Collections\SpecialAccount;
use Hanson\Robot\Models\Content;
use Hanson\Robot\Models\Sender;
use Hanson\Robot\Support\FileManager; use Hanson\Robot\Support\FileManager;
use Hanson\Robot\Support\Console; use Hanson\Robot\Support\Console;
class Message class Message
{ {
/**
* @var array 消息来源
*/
public $from; public $from;
/** /**
...@@ -29,12 +30,24 @@ class Message ...@@ -29,12 +30,24 @@ class Message
*/ */
public $sender; public $sender;
/**
* @var string 来源的username,用于回复
*/
public $username; public $username;
/**
* @var array 消息接收者
*/
public $to; public $to;
/**
* @var string 经过处理的内容
*/
public $content; public $content;
/**
* @var Carbon 时间
*/
public $time; public $time;
/** /**
...@@ -47,8 +60,6 @@ class Message ...@@ -47,8 +60,6 @@ class Message
*/ */
public $type; public $type;
static $message = [];
const USER_TYPE = [ const USER_TYPE = [
0 => 'Init', 0 => 'Init',
1 => 'Self', 1 => 'Self',
...@@ -62,20 +73,14 @@ class Message ...@@ -62,20 +73,14 @@ class Message
public $rawMsg; public $rawMsg;
// const MESSAGE_TYPE = [
// 0 => 'Text',
// ]
public function make($selector, $msg) public function make($selector, $msg)
{ {
$this->rawMsg = $msg; $this->rawMsg = $msg;
$this->time = Carbon::now();
$this->setFrom(); $this->setFrom();
$this->setTo(); $this->setTo();
$this->setFromType(); $this->setFromType();
$this->setType(); $this->setType();
return $this; return $this;
...@@ -86,13 +91,13 @@ class Message ...@@ -86,13 +91,13 @@ class Message
*/ */
private function setFrom() private function setFrom()
{ {
$this->from = Account::getInstance()->getContactByUsername($this->rawMsg['FromUserName']); $this->from = contact()->getContactByUsername($this->rawMsg['FromUserName']);
$this->username = $this->rawMsg['FromUserName']; $this->username = $this->rawMsg['FromUserName'];
} }
private function setTo() private function setTo()
{ {
$this->to = Account::getInstance()->getContactByUsername($this->rawMsg['ToUserName']); $this->to = contact()->getContactByUsername($this->rawMsg['ToUserName']);
} }
private function setFromType() private function setFromType()
...@@ -107,7 +112,7 @@ class Message ...@@ -107,7 +112,7 @@ class Message
$this->fromType = 'FileHelper'; $this->fromType = 'FileHelper';
} elseif (substr($this->rawMsg['FromUserName'], 0, 2) === '@@') { # group } elseif (substr($this->rawMsg['FromUserName'], 0, 2) === '@@') { # group
$this->fromType = 'Group'; $this->fromType = 'Group';
} elseif (ContactAccount::getInstance()->isContact($this->rawMsg['FromUserName'])) { } elseif (Contact::getInstance()->isContact($this->rawMsg['FromUserName'])) {
$this->fromType = 'Contact'; $this->fromType = 'Contact';
} elseif (OfficialAccount::getInstance()->isPublic($this->rawMsg['FromUserName'])) { } elseif (OfficialAccount::getInstance()->isPublic($this->rawMsg['FromUserName'])) {
$this->fromType = 'Public'; $this->fromType = 'Public';
...@@ -151,6 +156,7 @@ class Message ...@@ -151,6 +156,7 @@ class Message
case 1: case 1:
if(Location::isLocation($this->rawMsg['Content'])){ if(Location::isLocation($this->rawMsg['Content'])){
$this->type = 'Location'; $this->type = 'Location';
$this->content = Location::getLocationText($this->rawMsg['Content']);
}else{ }else{
$this->type = 'Text'; $this->type = 'Text';
$this->content = $this->rawMsg['Content']; $this->content = $this->rawMsg['Content'];
...@@ -160,13 +166,13 @@ class Message ...@@ -160,13 +166,13 @@ class Message
$this->type = 'Image'; $this->type = 'Image';
$this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey); $this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content); $content = http()->get($this->content);
FileManager::download(server()->config['tmp'] . $this->rawMsg['MsgId'] . '.jpg', $content); FileManager::download($this->rawMsg['MsgId'], $content, 'jpg');
break; break;
case 34: case 34:
$this->type = 'Voice'; $this->type = 'Voice';
$this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey); $this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content); $content = http()->get($this->content);
FileManager::download(server()->config['tmp'] . $this->rawMsg['MsgId'] . '.mp3', $content); FileManager::download($this->rawMsg['MsgId'], $content, 'mp3');
break; break;
case 37: case 37:
$this->type = 'AddUser'; $this->type = 'AddUser';
...@@ -196,31 +202,19 @@ class Message ...@@ -196,31 +202,19 @@ class Message
default: default:
$this->type = 'Unknown'; $this->type = 'Unknown';
break; break;
} }
} }
/** /**
* 设置当前message 为 location * 处理群发消息的内容
*/
private function setLocationMessage()
{
$this->fromType = 'Location';
// $this->url = $this->rawMsg['Url'];
$this->content->msg = Location::getLocationText($this->rawMsg['Content']);
}
/**
* handle group content
* *
* @param $content * @param $content string 内容
*/ */
private function handleGroupContent($content) private function handleGroupContent($content)
{ {
list($uid, $content) = explode('<br/>', $content, 2); list($uid, $content) = explode('<br/>', $content, 2);
$this->sender = Account::getInstance()->getGroupMember(substr($uid, 0, -1)); $this->sender = member()->getMemberByUsername(substr($uid, 0, -1));
$this->rawMsg['Content'] = $this->formatContent($content); $this->rawMsg['Content'] = $this->formatContent($content);
} }
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/17
* Time: 14:59
*/
namespace Hanson\Robot\Models;
class Content
{
public $type;
public $msg;
public $raw;
}
\ No newline at end of file \ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/17
* Time: 13:55
*/
namespace Hanson\Robot\Models;
class Origin
{
public $type;
public $id;
const GROUP = 'Group';
const CONTACT = 'Contact';
}
\ No newline at end of file \ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/17
* Time: 14:58
*/
namespace Hanson\Robot\Models;
class Sender
{
/**
* @var String 消息来源名称
*/
public $name;
public $type;
public $from;
public $to;
/**
* @var array 显示的聊天窗口
*/
public $contact;
}
\ No newline at end of file \ No newline at end of file
...@@ -12,9 +12,14 @@ namespace Hanson\Robot\Support; ...@@ -12,9 +12,14 @@ namespace Hanson\Robot\Support;
class FileManager class FileManager
{ {
public static function download($name, $data) public static function download($msgId, $data, $type)
{ {
file_put_contents($name, $data); $path = server()->config['tmp'] . $type;
if(!is_dir($path)){
mkdir($path, 0700, true);
}
file_put_contents("$path/$msgId.$type", $data);
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
use Hanson\Robot\Core\Server; use Hanson\Robot\Core\Server;
use Hanson\Robot\Core\Myself; use Hanson\Robot\Core\Myself;
use Hanson\Robot\Core\Http; use Hanson\Robot\Core\Http;
use Hanson\Robot\Collections\Account;
use Hanson\Robot\Collections\Member;
use Hanson\Robot\Collections\Contact;
use Hanson\Robot\Collections\Group;
if (! function_exists('server')) { if (! function_exists('server')) {
/** /**
...@@ -44,3 +48,47 @@ if (! function_exists('http')) { ...@@ -44,3 +48,47 @@ if (! function_exists('http')) {
return Http::getInstance(); return Http::getInstance();
} }
} }
if (! function_exists('account')) {
/**
* Get the available container instance.
*
* @return Account
*/
function account()
{
return Account::getInstance();
}
}
if (! function_exists('contact')) {
/**
* Get the available container instance.
*
* @return Contact
*/
function contact()
{
return Contact::getInstance();
}
}
if (! function_exists('member')) {
/**
* Get the available container instance.
*
* @return Member
*/
function member()
{
return Member::getInstance();
}
}
if (! function_exists('group')) {
/**
* Get the available container instance.
*
* @return Group
*/
function group()
{
return Group::getInstance();
}
}
\ No newline at end of file \ 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!