Commit 35d89ec1 by HanSon

优化console输出

1 parent 08e604aa
......@@ -9,7 +9,6 @@
namespace Hanson\Vbot\Collections;
use Hanson\Vbot\Support\Console;
use Illuminate\Support\Collection;
class Message extends Collection
......
......@@ -9,8 +9,6 @@
namespace Hanson\Vbot\Core;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\FileCookieJar;
use Hanson\Vbot\Support\Console;
class Http
......@@ -92,10 +90,11 @@ class Http
$response = $this->getClient()->request($method, $url, $options);
return $response->getBody()->getContents();
}catch (\Exception $e){
Console::log('http链接失败:' . $e->getMessage());
Console::log('错误URL:' . $url);
Console::log('http链接失败:' . $e->getMessage(), Console::ERROR);
Console::log('错误URL:' . $url, Console::ERROR);
}
return null;
}
......
......@@ -17,7 +17,6 @@ use Hanson\Vbot\Message\Entity\Recall;
use Hanson\Vbot\Message\Entity\Recommend;
use Hanson\Vbot\Message\Entity\RedPacket;
use Hanson\Vbot\Message\Entity\RequestFriend;
use Hanson\Vbot\Message\Entity\Share;
use Hanson\Vbot\Message\Entity\ShareFactory;
use Hanson\Vbot\Message\Entity\Text;
use Hanson\Vbot\Message\Entity\Touch;
......@@ -28,7 +27,7 @@ use Hanson\Vbot\Message\Entity\Voice;
class MessageFactory
{
public function make($selector, $msg)
public function make($msg)
{
return $this->handleMessageByType($msg);
}
......@@ -91,5 +90,6 @@ class MessageFactory
//Unknown
break;
}
return null;
}
}
\ No newline at end of file
......@@ -9,7 +9,6 @@
namespace Hanson\Vbot\Core;
use Closure;
use Hanson\Vbot\Collections\Account;
use Hanson\Vbot\Message\Entity\Emoticon;
use Hanson\Vbot\Message\Entity\Image;
use Hanson\Vbot\Message\Entity\Message;
......@@ -130,7 +129,7 @@ class MessageHandler
list($retCode, $selector) = $this->sync->checkSync();
if(in_array($retCode, ['1100', '1101'])){ # 微信客户端上登出或者其他设备登录
Console::log('[INFO] 微信客户端正常退出');
Console::log('微信客户端正常退出');
if($this->exitHandler){
call_user_func_array($this->exitHandler, []);
}
......@@ -138,7 +137,7 @@ class MessageHandler
}elseif ($retCode == 0){
$this->handlerMessage($selector);
}else{
Console::log('[INFO] 微信客户端异常退出');
Console::log('微信客户端异常退出');
if($this->exceptionHandler){
call_user_func_array($this->exitHandler, []);
}
......@@ -147,7 +146,7 @@ class MessageHandler
$this->sync->checkTime($time);
}
Console::log('[INFO] 程序结束');
Console::log('程序结束');
}
/**
......@@ -165,7 +164,7 @@ class MessageHandler
if($message['AddMsgList']){
foreach ($message['AddMsgList'] as $msg) {
$content = $this->messageFactory->make($selector, $msg);
$content = $this->messageFactory->make($msg);
if($content){
$this->addToMessageCollection($content);
if($this->handler){
......
......@@ -80,12 +80,12 @@ class Server
{
$this->prepare();
$this->init();
Console::log('[INFO] 初始化成功');
Console::log('初始化成功');
$this->statusNotify();
Console::log('[INFO] 开始初始化联系人');
Console::log('开始初始化联系人');
$this->initContact();
Console::log('[INFO] 初始化联系人成功');
Console::log('初始化联系人成功');
MessageHandler::getInstance()->listen();
}
......@@ -95,11 +95,11 @@ class Server
$this->getUuid();
$this->generateQrCode();
Console::showQrCode('https://login.weixin.qq.com/l/' . $this->uuid);
Console::log('[INFO] 请扫描二维码登录');
Console::log('请扫描二维码登录');
$this->waitForLogin();
$this->login();
Console::log('[INFO] 登录成功');
Console::log('登录成功');
}
/**
......@@ -120,7 +120,8 @@ class Server
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
if(!$matches){
throw new \Exception('[ERROR] 获取UUID失败');
Console::log('获取UUID失败', Console::ERROR);
exit;
}
$this->uuid = $matches[2];
......@@ -164,7 +165,7 @@ class Server
$code = $matches[1];
switch($code){
case '201':
Console::log('[INFO] 请点击确认登录微信');
Console::log('请点击确认登录微信');
$tip = 0;
break;
case '200':
......@@ -189,13 +190,13 @@ class Server
}
return;
case '408':
Console::log('[ERROR] 登录超时,请重试');
Console::log('登录超时,请重试', Console::ERROR);
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
default:
Console::log("[ERROR] 登录失败,错误码:$code 。请重试");
Console::log("登录失败,错误码:$code 。请重试", Console::ERROR);
$tip = 1;
$retryTime -= 1;
sleep(1);
......@@ -203,7 +204,8 @@ class Server
}
}
die('[ERROR] 登录超时,退出应用');
Console::log('登录超时,退出应用', Console::ERROR);
exit;
}
/**
......@@ -223,7 +225,8 @@ class Server
$this->passTicket = $data['pass_ticket'];
if(in_array('', [$this->skey, $this->sid, $this->uin, $this->passTicket])){
throw new \Exception('[ERROR] 登录失败');
Console::log('登录失败', Console::ERROR);
exit;
}
$this->deviceId = 'e' .substr(mt_rand().mt_rand(), 1, 15);
......@@ -254,7 +257,8 @@ class Server
$this->initContactList($result['ContactList']);
if($result['BaseResponse']['Ret'] != 0){
throw new \Exception('[ERROR] 初始化失败,链接:' . $url);
Console::log('初始化失败,链接:' . $url, Console::ERROR);
exit;
}
}
......
......@@ -9,6 +9,7 @@
namespace Hanson\Vbot\Support;
use Carbon\Carbon;
use PHPQRCode\QRcode;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
......@@ -17,14 +18,19 @@ use Symfony\Component\Console\Output\OutputInterface;
class Console
{
const INFO = 'INFO';
const WARNING = 'WARNING';
const ERROR = 'ERROR';
/**
* 输出字符串
*
* @param $str
* @param string $level
*/
public static function log($str)
public static function log($str, $level = 'INFO')
{
echo $str . PHP_EOL;
echo '[' . Carbon::now()->toDateTimeString() . ']' . "[{$level}] " . $str . PHP_EOL;
}
/**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!