Skip to content

Commit f2200e0

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

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

run-core.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ 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+
* 尝试获取本地登录数据,免扫码
11+
*/
12+
try {
13+
bot = new Wechat(require('./sync-data.json'))
14+
} catch (e) {
15+
bot = new Wechat()
16+
}
917
/**
1018
* 启动机器人
1119
*/

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 = {} // 所有联系人
@@ -157,10 +157,21 @@ class Wechat extends WechatCore {
157157
}
158158

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

0 commit comments

Comments
 (0)