Commit d55e635b by HanSon

修改类名与格式

1 parent 34f1e2ce
......@@ -8,10 +8,10 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Entity\Text;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
]);
......@@ -19,12 +19,14 @@ $flag = false;
$robot->server->setCustomerHandler(function() use (&$flag){
$contact = contact()->getUsernameById('matts8023');
if(!$flag){
Text::send('custom', contact()->getUsernameById('L907159127'));
Text::send($contact, '来轰炸吧');
$flag = true;
}
Text::send('测试' . \Carbon\Carbon::now()->toDateTimeString(), contact()->getUsernameById('L907159127'));
Text::send($contact, '测试' . \Carbon\Carbon::now()->toDateTimeString());
});
......
......@@ -8,10 +8,10 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
'debug' => true
]);
......
......@@ -8,10 +8,10 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
]);
......
......@@ -8,10 +8,10 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Entity\Text;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
'debug' => true
]);
......
......@@ -8,10 +8,10 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
'debug' => true
]);
......
......@@ -8,11 +8,11 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
use Hanson\Vbot\Support\Console;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
]);
......
......@@ -8,12 +8,12 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
use Hanson\Vbot\Message\Image;
use Hanson\Vbot\Support\Console;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
]);
......
......@@ -8,7 +8,7 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Entity\Message;
use Hanson\Vbot\Message\Entity\Image;
use Hanson\Vbot\Message\Entity\Text;
......@@ -25,7 +25,7 @@ use Hanson\Vbot\Message\Entity\Touch;
use Hanson\Vbot\Message\Entity\RequestFriend;
$path = __DIR__ . '/./../tmp/';
$robot = new Robot([
$robot = new Vbot([
'tmp' => $path,
'debug' => true
]);
......@@ -45,22 +45,22 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// }
// 文字信息
// if ($message instanceof Text) {
// // 联系人自动回复
// if ($message->fromType === 'Contact') {
//
// return http()->post('http://www.tuling123.com/openapi/api', [
// 'key' => '1dce02aef026258eff69635a06b0ab7d',
// 'info' => $message->content
// ], true)['text'];
// // 群组@我回复
// } elseif ($message->fromType === 'Group' && $message->isAt) {
// return http()->post('http://www.tuling123.com/openapi/api', [
// 'key' => '1dce02aef026258eff69635a06b0ab7d',
// 'info' => $message->content
// ], true)['text'];
// }
// }
if ($message instanceof Text) {
// 联系人自动回复
if ($message->fromType === 'Contact') {
return http()->post('http://www.tuling123.com/openapi/api', [
'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->content
], true)['text'];
// 群组@我回复
} elseif ($message->fromType === 'Group' && $message->isAt) {
return http()->post('http://www.tuling123.com/openapi/api', [
'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->content
], true)['text'];
}
}
// 图片信息 返回接收到的图片
// if ($message instanceof Image) {
......@@ -151,11 +151,11 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// return $reply;
// }
//
// // 手机点击聊天事件
// if($message instanceof Touch){
// print_r($message);
// Text::send($message->to['UserName'], "我点击了此群");
// }
// 手机点击聊天事件
if($message instanceof Touch){
print_r($message);
Text::send($message->to['UserName'], "我点击了此群");
}
return false;
......
......@@ -8,11 +8,11 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
use Hanson\Vbot\Support\Console;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
]);
......
......@@ -8,7 +8,7 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
use Hanson\Vbot\Message\Image;
use Hanson\Vbot\Message\Entity\Text;
......@@ -16,7 +16,7 @@ use Hanson\Vbot\Message\Emoticon;
use Hanson\Vbot\Message\Video;
$path = __DIR__ . '/./../tmp/';
$robot = new Robot([
$robot = new Vbot([
'tmp' => $path,
'debug' => true
]);
......
......@@ -8,10 +8,10 @@
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Robot;
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Message;
$robot = new Robot([
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
]);
......
......@@ -9,22 +9,10 @@
namespace Hanson\Vbot\Collections;
use Illuminate\Support\Collection;
class Account
{
/**
* @var Group
*/
static $group;
/**
* @var Contact
*/
static $contact;
/**
* @var Account
*/
static $instance = null;
......@@ -36,10 +24,8 @@ class Account
*/
public static function getInstance()
{
if(static::$instance === null){
if (static::$instance === null) {
static::$instance = new Account();
static::$group = group();
static::$contact = contact();
}
return static::$instance;
......@@ -53,11 +39,11 @@ class Account
*/
public function getAccount($username)
{
$account = static::$group->get($username, null);
$account = group()->get($username, null);
$account = $account ? : static::$contact->get($username, null);
$account = $account ?: contact()->get($username, null);
return $account ? : member()->get($username, []);
return $account ?: member()->get($username, []);
}
}
\ No newline at end of file
......@@ -34,19 +34,6 @@ class Contact extends Collection
}
/**
* 根据username获取联系人
*
* @param $id
* @return array
*/
public function getContactByUsername($id)
{
$contact = $this->get($id);
return $contact ?? [];
}
/**
* 根据微信号获取联系人
*
* @param $id
......@@ -54,13 +41,11 @@ class Contact extends Collection
*/
public function getContactById($id)
{
$contact = $this->filter(function($item, $key) use ($id){
return $this->filter(function($item, $key) use ($id){
if($item['Alias'] === $id){
return true;
}
})->first();
return $contact;
}
/**
......@@ -71,13 +56,11 @@ class Contact extends Collection
*/
public function getUsernameById($id)
{
$contact = $this->search(function($item, $key) use ($id){
return $this->search(function($item, $key) use ($id){
if($item['Alias'] === $id){
return true;
}
});
return $contact;
}
}
\ No newline at end of file
......@@ -51,7 +51,7 @@ class ContactFactory
if(official()->isOfficial($contact['VerifyFlag'])){ #公众号
Official::getInstance()->put($contact['UserName'], $contact);
}elseif (in_array($contact['UserName'], static::SPECIAL_USERS)){ # 特殊账户
SpecialAccount::getInstance()->put($contact['UserName'], $contact);
Special::getInstance()->put($contact['UserName'], $contact);
}elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊
group()->put($contact['UserName'], $contact);
}else{
......@@ -64,8 +64,8 @@ class ContactFactory
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(Official::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'SpecialAccount.json', json_encode(SpecialAccount::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'official.json', json_encode(Official::getInstance()->all()));
file_put_contents(server()->config['tmp'] . 'special.json', json_encode(Special::getInstance()->all()));
}
}
......
......@@ -46,7 +46,6 @@ class Group extends Collection
*
* @param $name
* @param bool $blur
* @param bool $onlyUsername
* @return static
*/
public function getGroupsByNickname($name, $blur = false)
......
......@@ -33,17 +33,4 @@ class Member extends Collection
return static::$instance;
}
/**
* 根据username获取群成员
*
* @param $id
* @return array
*/
public function getMemberByUsername($id)
{
$member = $this->get($id);
return $member ?? null;
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ namespace Hanson\Vbot\Collections;
use Illuminate\Support\Collection;
class SpecialAccount extends Collection
class Special extends Collection
{
static $instance = null;
......@@ -19,12 +19,12 @@ class SpecialAccount extends Collection
/**
* create a single instance
*
* @return SpecialAccount
* @return Special
*/
public static function getInstance()
{
if(static::$instance === null){
static::$instance = new SpecialAccount();
static::$instance = new Special();
}
return static::$instance;
......
......@@ -12,6 +12,7 @@ namespace Hanson\Vbot\Core;
use Hanson\Vbot\Message\Entity\Emoticon;
use Hanson\Vbot\Message\Entity\Image;
use Hanson\Vbot\Message\Entity\Location;
use Hanson\Vbot\Message\Entity\Message;
use Hanson\Vbot\Message\Entity\Recall;
use Hanson\Vbot\Message\Entity\Recommend;
use Hanson\Vbot\Message\Entity\RedPacket;
......@@ -22,7 +23,6 @@ use Hanson\Vbot\Message\Entity\Touch;
use Hanson\Vbot\Message\Entity\Transfer;
use Hanson\Vbot\Message\Entity\Video;
use Hanson\Vbot\Message\Entity\Voice;
use Hanson\Vbot\Support\Console;
class MessageFactory
{
......@@ -36,7 +36,7 @@ class MessageFactory
/**
* 处理消息类型
* @param $msg
* @return Touch
* @return Message
*/
private function handleMessageByType($msg)
{
......@@ -65,9 +65,9 @@ class MessageFactory
// else if(str_contains($msg['Content'], '好友') || str_contains($msg['Content'], 'friend')){
// print_r($msg);
// }
else{
print_r($msg);
}
// else{
// print_r($msg);
// }
break;
case 49:
if($msg['Status'] == 3 && $msg['FileName'] === '微信转账'){
......
......@@ -125,6 +125,7 @@ class MessageHandler
$content = $this->messageFactory->make($selector, $msg);
if($content){
$this->addToMessageCollection($content);
if($this->handler){
$reply = call_user_func_array($this->handler, [$content]);
if($reply){
if($reply instanceof Image){
......@@ -141,6 +142,7 @@ class MessageHandler
}
}
}
}
/**
* @param $message Message
......
......@@ -9,8 +9,6 @@
namespace Hanson\Vbot\Core;
use Hanson\Vbot\Collections\Contact;
class Myself
{
......@@ -26,7 +24,7 @@ class Myself
public static function getInstance()
{
if(!static::$instance){
if (!static::$instance) {
static::$instance = new Myself();
}
......@@ -39,7 +37,7 @@ class Myself
$this->nickname = $user['NickName'];
$this->username = $user['UserName'];
$this->sex = $user['Sex'];
$this->uin= $user['Uin'];
$this->uin = $user['Uin'];
}
}
\ No newline at end of file
......@@ -15,7 +15,6 @@ use Hanson\Vbot\Collections\Account;
use Hanson\Vbot\Collections\ContactFactory;
use Hanson\Vbot\Collections\Group;
use Hanson\Vbot\Support\Console;
use Hanson\Vbot\Support\ObjectAble;
use Symfony\Component\DomCrawler\Crawler;
class Server
......@@ -84,12 +83,12 @@ class Server
{
$this->prepare();
$this->init();
Console::log('[INFO] init success!');
Console::log('[INFO] 初始化成功');
$this->statusNotify();
Console::log('[INFO] begin to init contacts');
Console::log('[INFO] 开始初始化联系人');
$this->initContact();
Console::log('[INFO] init contacts success!');
Console::log('[INFO] 初始化联系人成功');
MessageHandler::getInstance()->listen();
}
......@@ -98,11 +97,11 @@ class Server
{
$this->getUuid();
$this->generateQrCode();
Console::log('[INFO] please scan qrcode to login');
Console::log('[INFO] 请扫描二维码登录');
$this->waitForLogin();
$this->login();
Console::log('[INFO] login success!');
Console::log('[INFO] 登录成功');
}
/**
......@@ -123,7 +122,7 @@ class Server
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
if(!$matches){
throw new \Exception('fail to get uuid');
throw new \Exception('[ERROR] 获取UUID失败');
}
$this->uuid = $matches[2];
......@@ -167,7 +166,7 @@ class Server
$code = $matches[1];
switch($code){
case '201':
Console::log('[INFO] please confirm to login');
Console::log('[INFO] 请点击确认登录微信');
$tip = 0;
break;
case '200':
......@@ -178,7 +177,6 @@ class Server
'wx2.qq.com' => ['file.wx2.qq.com', 'webpush.wx2.qq.com'],
'wx.qq.com' => ['file.wx.qq.com', 'webpush.wx.qq.com'],
'wx8.qq.com' => ['file.wx8.qq.com', 'webpush.wx8.qq.com'],
// 'qq.com' => ['file.wx.qq.com', 'webpush.wx.qq.com'],
'web2.wechat.com' => ['file.web2.wechat.com', 'webpushweb2.wechat.com'],
'wechat.com' => ['file.web.wechat.com', 'webpushweb.web.wechat.com'],
];
......@@ -195,13 +193,13 @@ class Server
Console::log('url is:'. $this->baseUri);
return;
case '408':
Console::log('[ERROR] login timeout. please try 1 second later.');
Console::log('[ERROR] 登录超时,请重试');
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
default:
Console::log("[ERROR] login fail. exception code:$code . please try 1 second later.");
Console::log("[ERROR] 登录失败,错误码:$code 。请重试");
$tip = 1;
$retryTime -= 1;
sleep(1);
......@@ -209,7 +207,7 @@ class Server
}
}
throw new \Exception('[ERROR] login fail!');
die('[ERROR] 登录超时,退出应用');
}
/**
......@@ -229,7 +227,7 @@ class Server
$this->passTicket = $data['pass_ticket'];
if(in_array('', [$this->skey, $this->sid, $this->uin, $this->passTicket])){
throw new \Exception('[ERROR] login fail!');
throw new \Exception('[ERROR] 登录失败');
}
$this->deviceId = 'e' .substr(mt_rand().mt_rand(), 1, 15);
......@@ -263,7 +261,7 @@ class Server
// print_r($this->baseRequest);
Console::log('init URL:'. $url);
throw new \Exception('[ERROR] init fail!');
throw new \Exception('[ERROR] 初始化失败');
}
}
......
......@@ -19,7 +19,7 @@ use Pimple\Container;
* @package Hanson\Vbot\Foundation
* @property Server $server
*/
class Robot extends Container
class Vbot extends Container
{
/**
......
......@@ -13,10 +13,9 @@ use Carbon\Carbon;
use Hanson\Vbot\Core\Server;
use Hanson\Vbot\Collections\Contact;
use Hanson\Vbot\Collections\Official;
use Hanson\Vbot\Collections\SpecialAccount;
use Hanson\Vbot\Collections\Special;
use Hanson\Vbot\Support\FileManager;
use Hanson\Vbot\Support\Console;
use Hanson\Vbot\Support\ObjectAble;
class Message
{
......@@ -81,11 +80,11 @@ class Message
$this->fromType = 'Self';
} elseif (substr($this->msg['FromUserName'], 0, 2) === '@@') { # group
$this->fromType = 'Group';
} elseif (contact()->getContactByUsername($this->msg['FromUserName'])) {
} elseif (contact()->get($this->msg['FromUserName'])) {
$this->fromType = 'Contact';
} elseif (Official::getInstance()->get($this->msg['FromUserName'])) {
$this->fromType = 'Official';
} elseif (SpecialAccount::getInstance()->get($this->msg['FromUserName'], false)) {
} elseif (Special::getInstance()->get($this->msg['FromUserName'], false)) {
$this->fromType = 'Special';
} else {
$this->fromType = 'Unknown';
......
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2017/1/13
* Time: 17:23
*/
namespace Hanson\Vbot\Support;
trait ObjectAble
{
public function toObject(Array $array)
{
return json_decode(json_encode($array));
}
}
\ 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!