Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
hfpp2012
/
vbot
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 992b2a82
authored
Jan 20, 2017
by
zhuanxuhit
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
将产生QRCode的代码放入Console中
1 parent
cb5f6d4d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
83 deletions
example/custom.php
src/Collections/Contact.php
src/Core/Server.php
src/Support/Console.php
example/custom.php
View file @
992b2a8
...
...
@@ -19,7 +19,7 @@ $flag = false;
$robot
->
server
->
setCustomerHandler
(
function
()
use
(
&
$flag
){
// RemarkName,代表的改用户在你通讯录的名字
$contact
=
contact
()
->
getUsernameBy
NickRemarkName
(
'hanson'
);
$contact
=
contact
()
->
getUsernameBy
RemarkName
(
'hanson'
);
if
(
$contact
===
false
){
dd
(
"找不到你要的联系人,请确认联系人姓名"
);
}
...
...
src/Collections/Contact.php
View file @
992b2a8
...
...
@@ -68,7 +68,7 @@ class Contact extends Collection
* @param $id
* @return mixed
*/
public
function
getUsernameBy
NickRemarkName
(
$id
)
public
function
getUsernameBy
RemarkName
(
$id
)
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$id
){
if
(
$item
[
'RemarkName'
]
===
$id
){
...
...
src/Core/Server.php
View file @
992b2a8
...
...
@@ -10,19 +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
PHPQRCode\QRencode
;
use
Symfony\Component\Console\Formatter\OutputFormatterStyle
;
use
Symfony\Component\Console\Output\ConsoleOutput
;
use
Symfony\Component\Console\Output\OutputInterface
;
use
Symfony\Component\DomCrawler\Crawler
;
use
PHPQRCode\QRcode
as
CQRcode
;
use
Symfony\Component\Process\Exception\ProcessFailedException
;
use
Symfony\Component\Process\ProcessBuilder
;
class
Server
{
...
...
@@ -103,8 +93,8 @@ class Server
public
function
prepare
()
{
$this
->
getUuid
();
//
$this->generateQrCode();
$this
->
generateConsoleQrCode
(
);
$this
->
generateQrCode
();
Console
::
showQrCode
(
'https://login.weixin.qq.com/l/'
.
$this
->
uuid
);
Console
::
log
(
'[INFO] 请扫描二维码登录'
);
$this
->
waitForLogin
();
...
...
@@ -158,75 +148,6 @@ class Server
}
}
private
function
initStyle
(
OutputInterface
$output
)
{
$style
=
new
OutputFormatterStyle
(
'black'
,
'black'
);
$output
->
getFormatter
()
->
setStyle
(
'blackc'
,
$style
);
$style
=
new
OutputFormatterStyle
(
'white'
,
'white'
);
$output
->
getFormatter
()
->
setStyle
(
'whitec'
,
$style
);
}
/**
*
*/
public
function
generateConsoleQrCode
()
{
$text
=
'https://login.weixin.qq.com/l/'
.
$this
->
uuid
;
$output
=
new
ConsoleOutput
();
$this
->
initStyle
(
$output
);
$map
=
array
(
0
=>
'<whitec> </whitec>'
,
1
=>
'<blackc> </blackc>'
,
);
$lrPadding
=
1
;
$tbPadding
=
0
;
$text
=
CQRcode
::
text
(
$text
);
$length
=
strlen
(
$text
[
0
]);
$screenSize
=
$this
->
getTTYSize
();
if
(
!
$screenSize
)
{
$output
->
getErrorOutput
()
->
writeln
(
'<comment>Get Screen Size Failed</comment>'
);
}
else
{
list
(
$maxLines
,
$maxCols
)
=
$screenSize
;
$qrCols
=
2
*
(
$length
+
$lrPadding
*
2
);
$qrLines
=
count
(
$text
)
+
$tbPadding
*
2
;
if
(
$qrCols
>
$maxCols
||
$qrLines
>
$maxLines
){
$output
->
getErrorOutput
()
->
writeln
(
'<error>Max Lines/Columns Reached:请缩小控制台字体大小</error>'
);
return
;
}
}
$paddingLine
=
str_repeat
(
$map
[
0
],
$length
+
$lrPadding
*
2
)
.
"
\n
"
;
$after
=
$before
=
str_repeat
(
$paddingLine
,
$tbPadding
);
$output
->
write
(
$before
);
foreach
(
$text
as
$line
)
{
$output
->
write
(
str_repeat
(
$map
[
0
],
$lrPadding
));
for
(
$i
=
0
;
$i
<
$length
;
$i
++
)
{
$type
=
substr
(
$line
,
$i
,
1
);
$output
->
write
(
$map
[
$type
]);
}
$output
->
writeln
(
str_repeat
(
$map
[
0
],
$lrPadding
));
}
$output
->
write
(
$after
);
}
private
function
getTTYSize
()
{
if
(
!
posix_isatty
(
STDOUT
)){
return
false
;
}
$ttyName
=
posix_ttyname
(
STDOUT
);
$builder
=
new
ProcessBuilder
();
$process
=
$builder
->
setPrefix
(
'stty'
)
->
setArguments
(
array
(
'-f'
,
$ttyName
,
'size'
))
->
getProcess
();
try
{
$process
->
mustRun
();
}
catch
(
ProcessFailedException
$e
)
{
return
false
;
}
$output
=
$process
->
getOutput
();
if
(
!
preg_match
(
'~^(\d+)\s+(\d+)$~'
,
$output
,
$match
))
{
return
false
;
}
return
array
(
$match
[
1
],
$match
[
2
]);
}
/**
* waiting user to login
*
...
...
src/Support/Console.php
View file @
992b2a8
...
...
@@ -9,6 +9,13 @@
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
;
use
Symfony\Component\Process\Exception\ProcessFailedException
;
use
Symfony\Component\Process\ProcessBuilder
;
class
Console
{
...
...
@@ -17,4 +24,69 @@ class Console
echo
$str
.
PHP_EOL
;
}
private
static
function
initStyle
(
OutputInterface
$output
)
{
$style
=
new
OutputFormatterStyle
(
'black'
,
'black'
);
$output
->
getFormatter
()
->
setStyle
(
'blackc'
,
$style
);
$style
=
new
OutputFormatterStyle
(
'white'
,
'white'
);
$output
->
getFormatter
()
->
setStyle
(
'whitec'
,
$style
);
}
private
static
function
getTTYSize
()
{
if
(
!
posix_isatty
(
STDOUT
)){
return
false
;
}
$ttyName
=
posix_ttyname
(
STDOUT
);
$builder
=
new
ProcessBuilder
();
$process
=
$builder
->
setPrefix
(
'stty'
)
->
setArguments
(
array
(
'-f'
,
$ttyName
,
'size'
))
->
getProcess
();
try
{
$process
->
mustRun
();
}
catch
(
ProcessFailedException
$e
)
{
return
false
;
}
$output
=
$process
->
getOutput
();
if
(
!
preg_match
(
'~^(\d+)\s+(\d+)$~'
,
$output
,
$match
))
{
return
false
;
}
return
array
(
$match
[
1
],
$match
[
2
]);
}
public
static
function
showQrCode
(
$text
)
{
$output
=
new
ConsoleOutput
();
static
::
initStyle
(
$output
);
$map
=
array
(
0
=>
'<whitec> </whitec>'
,
1
=>
'<blackc> </blackc>'
,
);
$lrPadding
=
1
;
$tbPadding
=
0
;
$text
=
QRcode
::
text
(
$text
);
$length
=
strlen
(
$text
[
0
]);
$screenSize
=
static
::
getTTYSize
();
if
(
!
$screenSize
)
{
$output
->
getErrorOutput
()
->
writeln
(
'<comment>Get Screen Size Failed</comment>'
);
}
else
{
list
(
$maxLines
,
$maxCols
)
=
$screenSize
;
$qrCols
=
2
*
(
$length
+
$lrPadding
*
2
);
$qrLines
=
count
(
$text
)
+
$tbPadding
*
2
;
if
(
$qrCols
>
$maxCols
||
$qrLines
>
$maxLines
){
$output
->
getErrorOutput
()
->
writeln
(
'<error>Max Lines/Columns Reached:请缩小控制台字体大小</error>'
);
return
;
}
}
$paddingLine
=
str_repeat
(
$map
[
0
],
$length
+
$lrPadding
*
2
)
.
"
\n
"
;
$after
=
$before
=
str_repeat
(
$paddingLine
,
$tbPadding
);
$output
->
write
(
$before
);
foreach
(
$text
as
$line
)
{
$output
->
write
(
str_repeat
(
$map
[
0
],
$lrPadding
));
for
(
$i
=
0
;
$i
<
$length
;
$i
++
)
{
$type
=
substr
(
$line
,
$i
,
1
);
$output
->
write
(
$map
[
$type
]);
}
$output
->
writeln
(
str_repeat
(
$map
[
0
],
$lrPadding
));
}
$output
->
write
(
$after
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment