Commit 08f33261 by HanSon Committed by GitHub

Merge pull request #15 from HanSon/qrcode

增加二维码显示
2 parents a7baacb4 11a8e84c
/vendor/
.idea
/tmp/
......@@ -16,7 +16,11 @@
"pimple/pimple": "^3.0",
"illuminate/support": "^5.3",
"nesbot/carbon": "^1.21",
"php": ">=7.0.0"
"php": ">=7.0.0",
"symfony/var-dumper": "^3.2",
"aferrandini/phpqrcode": "^1.0",
"symfony/process": "^3.2",
"symfony/console": "^3.2"
},
"minimum-stability": "dev",
"autoload": {
......
......@@ -18,9 +18,11 @@ $robot = new Vbot([
$flag = false;
$robot->server->setCustomerHandler(function() use (&$flag){
$contact = contact()->getUsernameById('matts8023');
// RemarkName,代表的改用户在你通讯录的名字
$contact = contact()->getUsernameByRemarkName('hanson');
if ($contact === false){
dd("找不到你要的联系人,请确认联系人姓名");
}
if(!$flag){
Text::send($contact, '来轰炸吧');
$flag = true;
......
......@@ -62,5 +62,20 @@ class Contact extends Collection
}
});
}
/**
* 根据通讯录中的名字获取通讯对象
*
* @param $id
* @return mixed
*/
public function getUsernameByRemarkName( $id)
{
return $this->search(function($item, $key) use ($id){
if($item['RemarkName'] === $id){
return true;
}
});
}
}
\ No newline at end of file
......@@ -10,12 +10,9 @@ namespace Hanson\Vbot\Core;
use Endroid\QrCode\QrCode;
use GuzzleHttp\Client;
use Hanson\Vbot\Collections\Account;
use Hanson\Vbot\Collections\ContactFactory;
use Hanson\Vbot\Collections\Group;
use Hanson\Vbot\Support\Console;
use Symfony\Component\DomCrawler\Crawler;
class Server
{
......@@ -60,7 +57,7 @@ class Server
{
$this->config = $config;
$this->config['debug'] = $this->config['debug'] ?? false;
$this->config['debug'] = isset($this->config['debug']) ? $this->config['debug'] : false;
}
/**
......@@ -97,6 +94,7 @@ class Server
{
$this->getUuid();
$this->generateQrCode();
Console::showQrCode('https://login.weixin.qq.com/l/' . $this->uuid);
Console::log('[INFO] 请扫描二维码登录');
$this->waitForLogin();
......@@ -144,10 +142,6 @@ class Server
$file = $this->config['tmp'] . 'qr.png';
$qrCode->save($file);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
system($file);
}
}
/**
......@@ -253,7 +247,7 @@ class Server
$content = http()->json($url, [
'BaseRequest' => $this->baseRequest
]);
$result = json_decode($content, true);
$this->generateSyncKey($result, $first);
......@@ -334,11 +328,4 @@ class Server
{
MessageHandler::getInstance()->setExceptionHandler($closure);
}
public function debug($debug = true)
{
$this->debug = $debug;
return $this;
}
}
\ No newline at end of file
......@@ -9,12 +9,66 @@
namespace Hanson\Vbot\Support;
use PHPQRCode\QRcode;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
class Console
{
/**
* 输出字符串
*
* @param $str
*/
public static function log($str)
{
echo $str . PHP_EOL;
}
/**
* 初始化二维码style
*
* @param OutputInterface $output
*/
private static function initQrcodeStyle(OutputInterface $output) {
$style = new OutputFormatterStyle('black', 'black', array('bold'));
$output->getFormatter()->setStyle('blackc', $style);
$style = new OutputFormatterStyle('white', 'white', array('bold'));
$output->getFormatter()->setStyle('whitec', $style);
}
/**
* 控制台显示二维码
*
* @param $text
*/
public static function showQrCode($text)
{
$output = new ConsoleOutput();
static::initQrcodeStyle($output);
if(System::isWin()){
$pxMap = ['<whitec>mm</whitec>', '<blackc> </blackc>'];
system('cls');
}else{
$pxMap = ['<whitec> </whitec>', '<blackc> </blackc>'];
system('clear');
}
$text = QRcode::text($text);
$length = strlen($text[0]);
foreach ($text as $line) {
$output->write($pxMap[0]);
for ($i = 0; $i < $length; $i++) {
$type = substr($line, $i, 1);
$output->write($pxMap[$type]);
}
$output->writeln($pxMap[0]);
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/20
* Time: 18:37
*/
namespace Hanson\Vbot\Support;
class System
{
/**
* 判断运行服务器是否windows
*
* @return bool
*/
public static function isWin()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
}
\ 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!