Skip to content

Commit 0d5b6c8

Browse files
committed
New Feature
可以获取和设置微信保持登录同步的属性 利用本特性可以做免扫码登录
1 parent 4b2f7bb commit 0d5b6c8

File tree

4 files changed

+87
-35
lines changed

4 files changed

+87
-35
lines changed

run-core.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ const qrcode = require('qrcode-terminal')
55
const fs = require('fs')
66
const request = require('request')
77

8-
let bot = new Wechat()
8+
let bot
9+
// 尝试获取本地登录数据,免扫码
10+
try {
11+
bot = new Wechat(require('./sync-data.json'))
12+
} catch (e) {
13+
bot = new Wechat()
14+
}
915

1016
bot.on('error', err => {
1117
console.log('错误:', err)
@@ -26,13 +32,12 @@ bot.on('user-avatar', avatar => {
2632

2733
bot.on('login', () => {
2834
console.log('登录成功')
29-
let ToUserName = bot.contacts['filehelper'].UserName || 'filehelper'
3035

36+
// 保存登录数据
37+
fs.writeFileSync('./sync-data.json', JSON.stringify(bot.botData))
38+
39+
let ToUserName = bot.contacts['filehelper'].UserName || 'filehelper'
3140
// 发送文本消息,可以包含emoji(😒)和QQ表情([坏笑])
32-
// bot.sendText('发送文本消息,可以包含emoji(😒)和QQ表情([坏笑])', ToUserName)
33-
// .catch(err => {
34-
// console.log(err)
35-
// })
3641
bot.sendMsg('发送文本消息,可以包含emoji(😒)和QQ表情([坏笑])', ToUserName)
3742
.catch(err => {
3843
console.log(err)
@@ -50,9 +55,9 @@ bot.on('login', () => {
5055
// console.log(err)
5156
// })
5257
bot.sendMsg({
53-
file: request('https://raw.githubusercontent.com/nodeWechat/wechat4u/master/bot-qrcode.jpg'),
54-
filename: 'bot-qrcode.jpg'
55-
}, ToUserName)
58+
file: request('https://raw.githubusercontent.com/nodeWechat/wechat4u/master/bot-qrcode.jpg'),
59+
filename: 'bot-qrcode.jpg'
60+
}, ToUserName)
5661
.catch(err => {
5762
console.log(err)
5863
})
@@ -72,8 +77,8 @@ bot.on('login', () => {
7277
// console.log(err)
7378
// })
7479
bot.sendMsg({
75-
file: fs.createReadStream('./media/test.gif')
76-
}, ToUserName)
80+
file: fs.createReadStream('./media/test.gif')
81+
}, ToUserName)
7782
.catch(err => {
7883
console.log(err)
7984
})
@@ -87,8 +92,8 @@ bot.on('login', () => {
8792
// console.log(err)
8893
// })
8994
bot.sendMsg({
90-
file: fs.createReadStream('./media/test.mp4')
91-
}, ToUserName)
95+
file: fs.createReadStream('./media/test.mp4')
96+
}, ToUserName)
9297
.catch(err => {
9398
console.log(err)
9499
})
@@ -102,8 +107,8 @@ bot.on('login', () => {
102107
// console.log(err)
103108
// })
104109
bot.sendMsg({
105-
file: fs.createReadStream('./media/test.txt')
106-
}, ToUserName)
110+
file: fs.createReadStream('./media/test.txt')
111+
}, ToUserName)
107112
.catch(err => {
108113
console.log(err)
109114
})
@@ -142,6 +147,13 @@ bot.on('message', msg => {
142147
}).catch(err => {
143148
console.log(err)
144149
})
150+
// 直接转发图片消息
151+
bot.sendPic({
152+
Content: msg.Content
153+
}, msg.FromUserName)
154+
.catch(err => {
155+
bot.emit('error', err)
156+
})
145157
break
146158
case bot.CONF.MSGTYPE_VOICE:
147159
// 语音消息
@@ -172,6 +184,13 @@ bot.on('message', msg => {
172184
}).catch(err => {
173185
console.log(err)
174186
})
187+
// 直接转发视频消息
188+
bot.sendVideo({
189+
Content: msg.Content
190+
}, msg.FromUserName)
191+
.catch(err => {
192+
bot.emit('error', err)
193+
})
175194
break
176195
case bot.CONF.MSGTYPE_MICROVIDEO:
177196
// 小视频消息

src/core.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const debug = _debug('core')
1616

1717
export default class WechatCore {
1818

19-
constructor () {
19+
constructor (data) {
2020
this.PROP = {
2121
uuid: '',
2222
uin: '',
@@ -29,10 +29,29 @@ export default class WechatCore {
2929
List: []
3030
}
3131
}
32-
3332
this.CONF = getCONF()
33+
this.COOKIE = {}
3434
this.user = {}
35-
this.request = new Request()
35+
if (data) {
36+
this.botData = data
37+
}
38+
39+
this.request = new Request({
40+
Cookie: this.COOKIE
41+
})
42+
}
43+
44+
get botData () {
45+
return {
46+
PROP: this.PROP,
47+
CONF: this.CONF,
48+
COOKIE: this.COOKIE,
49+
user: this.user
50+
}
51+
}
52+
53+
set botData (data) {
54+
Object.assign(this, data)
3655
}
3756

3857
getUUID () {

src/util/request.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import axios from 'axios'
2-
import CM from 'cookie-manager'
32
import {isStandardBrowserEnv} from './global'
43

54
const getPgv = c => {
@@ -19,20 +18,24 @@ export function Request (defaults) {
1918

2019
this.axios = axios.create(defaults)
2120
if (!isStandardBrowserEnv) {
22-
this.cm = new CM()
23-
this.cm.store('', ['pgv_pvi=' + getPgv() + '; Domain=.qq.com; Path=/', 'pgv_si=' + getPgv('s') + '; Domain=.qq.com; Path=/'])
24-
this.axios.interceptors.request.use(config => {
25-
config.headers['cookie'] = config.url ? decodeURIComponent(this.cm.prepare(config.url)) : ''
26-
return config
27-
}, err => {
28-
return Promise.reject(err)
29-
})
21+
this.Cookie = defaults.Cookie || {}
22+
this.Cookie['pgv_pvi'] = getPgv()
23+
this.Cookie['pgv_si'] = getPgv('s')
24+
this.axios.defaults.headers.common['cookie'] = Object.keys(this.Cookie).map(key => {
25+
return `${key}=${this.Cookie[key]}`
26+
}).join('; ')
3027
this.axios.interceptors.response.use(res => {
3128
let setCookie = res.headers['set-cookie']
3229
if (setCookie) {
33-
this.cm.store(res.config.url, setCookie.map(item => {
34-
return item.replace(/=\s*?(?=(\w+\.)*(wx\d?\.qq\.com|wechat\.com))/, '=.')
35-
}))
30+
setCookie.forEach(item => {
31+
let pm = item.match(/^(.+?)\s?\=\s?(.+?);/)
32+
if (pm) {
33+
this.Cookie[pm[1]] = pm[2]
34+
}
35+
})
36+
this.axios.defaults.headers.common['cookie'] = Object.keys(this.Cookie).map(key => {
37+
return `${key}=${this.Cookie[key]}`
38+
}).join('; ')
3639
}
3740
return res
3841
}, err => {

src/wechat.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ if (!isStandardBrowserEnv) {
2121

2222
class Wechat extends WechatCore {
2323

24-
constructor () {
25-
super()
24+
constructor (data) {
25+
super(data)
2626
_.extend(this, new EventEmitter())
2727
this.state = this.CONF.STATE.init
2828
this.contacts = {} // 所有联系人
@@ -155,10 +155,21 @@ class Wechat extends WechatCore {
155155
}
156156

157157
start () {
158-
return this.startLogin()
159-
.then(() => this.startInit())
160-
.then(() => this.startGetContact())
158+
Promise.resolve(this.PROP.uin ? Promise.resolve() : Promise.reject())
161159
.then(() => {
160+
return this.init()
161+
.then(() => this.notifyMobile())
162+
})
163+
.catch(() => {
164+
return this.startLogin()
165+
.then(() => this.startInit())
166+
})
167+
.then(() => {
168+
this.startGetContact()
169+
.catch(err => {
170+
debug(err)
171+
bot.emit('error', err)
172+
})
162173
this.emit('login')
163174
this.state = this.CONF.STATE.login
164175
this.lastSyncTime = Date.now()

0 commit comments

Comments
 (0)