Commit 35d89ec1 by HanSon

优化console输出

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