Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

终端打印二维码 #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ node_modules
# vim temp file
*.swp
*.swo
<<<<<<< HEAD
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2333333


# log
data/pic/*
wechat-user-bot.log

# Editor backup
*~
=======
# gedit temp file
*~
# data dir
data/
>>>>>>> master
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ var webwxgetcontact = require('./lib/webwx.js').webwxgetcontact;
var robot = require('./lib/robot.js').robot;

// display, which is a stream
var child_process = require('child_process');
var display = child_process.spawn('display');
// var child_process = require('child_process');
// var display = child_process.spawn('display');
var display = require('qrcode-terminal');

getUUID
.then(checkAndParseUUID)
Expand Down
24 changes: 5 additions & 19 deletions lib/webwx.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,19 @@ function checkAndParseUUID(body) {
* @return {Promise} session对象
*/
function showQRImage(display) {

return (uuid) => {
console.log("请扫描二维码并确认登录,关闭二维码窗口继续...");
var QRUrl = 'https://login.weixin.qq.com/qrcode/' + uuid + '?';
var param = {
t: 'webwx',
'_': Date.now()
}

var checkLoginPromise = new Promise((resolve, reject)=> {
display.on('exit', wxSessionStop);
info("GET " + QRUrl)
var req = request(QRUrl, {qs: param});
req.on('response', ()=>{
display.generate('https://login.weixin.qq.com/l/' + uuid, function (qrcode) {
console.log(qrcode);
resolve({
uuid: uuid,
display: display, // 将display传递下去
tip: 1, //标识
});
})
req.pipe(display.stdin);
req.on('error', (err)=>{
return reject(err);
})
}, {small: true} ); // FIXME: not work now, waiting an update on qrcode-terminal
});

return checkLoginPromise;
Expand All @@ -115,7 +105,6 @@ function checkLogin(wxSession) {
var timestamp = ~Date.now();

var uuid = wxSession.uuid;
var display = wxSession.display;
// 检查登录和跳转
return new Promise((resolve, reject)=> {
var checkUrl = `https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=${wxSession.tip}&uuid=${uuid}&r=${timestamp}`
Expand All @@ -130,8 +119,6 @@ function checkLogin(wxSession) {
if (/window\.code=200/.test(body)) {
console.log("登录微信...");
// 删除退出子进程杀掉主进程的回调
display.removeListener('exit', wxSessionStop)
display.kill();
resolve(body);
} else if(/window\.code=201/.test(body)){
wxSession.tip = 0; // 第一次之后tip都为0,不然下一个请求不是长连接
Expand All @@ -144,8 +131,7 @@ function checkLogin(wxSession) {
resolve(checkLogin(wxSession));
} else {
console.log("验证码超时...")
display.kill();
wxSessionStop(1);
sessionStop(1);
}
});
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"homepage": "https://github.com/HalfdogStudio/wechat-user-bot#readme",
"dependencies": {
"qrcode-terminal": "^0.10.0",
"request": "^2.67.0",
"winston": "^2.1.1"
},
Expand Down