-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalipay.js
299 lines (245 loc) · 9.47 KB
/
alipay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const puppeteer = require('puppeteer');
const keyDefinitions = require('./USKeyboardLayout');
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
var config = require('./config/config.js');
async function getAlipayOrders() {
console.log("启动浏览器")
const browser = await puppeteer.launch({
headless: false, //不想显示浏览器,可以去掉这一行
// args: [
// '--no-sandbox',
// '--disable-setuid-sandbox',
// '--disable-dev-shm-usage',
// ]
});
const login_page = await browser.newPage()
if (config.qrLogin) {
await alipayQrLogin(login_page)
} else {
await alipayPwdLogin(login_page, config)
}
refreshAlipayHome(login_page)
const order_page = await browser.newPage()
var orders = {}
for (; ;) {
await watchOrder(order_page, orders)
await delay(3000)
}
// browser.close();
}
async function alipayQrLogin(page) {
var data = {}
await gotoUrl(page, "https://auth.alipay.com/login/index.htm");
await page.waitFor(".barcode")
console.log("你选择了扫码二维码支付宝登录, 已保存为alipay_login_qrcode.png")
await screenshotDOMElement(page, {
path: 'alipay_login_qrcode.png',
selector: '.barcode',
padding: 5
});
for (; ;) {
try {
await page.waitFor(".userName", { timeout: 3000 })
var welcome = await page.evaluate(() => {
var welcome = document.querySelector('#container > div.i-banner > div.i-banner-content.fn-clear > div.i-banner-main > div.i-banner-main-hello.fn-clear > p');
if (welcome != null) {
return welcome.innerText
}
});
if (welcome != {}) {
console.log(welcome)
var cookies = await page.cookies()
var cookieStr = ''
cookies.forEach(c => {
cookieStr += c.name + '=' + c.value + '; '
});
data['cookie'] = cookieStr
break
}
} catch (e) {
console.log("等待支付宝扫码登录结果~")
}
}
return data
}
async function alipayPwdLogin(page, config) {
var data = {}
await gotoUrl(page, "https://auth.alipay.com/login/index.htm");
await page.waitFor("#J-loginMethod-tabs")
await page.click("#J-loginMethod-tabs > li:nth-child(2)")
await page.focus("#J-input-user")
for (var i = 0; i < config.username.length; i++) {
var letter = config.username.charAt(i)
await page.keyboard.press(keyDefinitions[letter].code, { delay: 200 })
}
await page.focus("#password_rsainput")
for (var i = 0; i < config.pwd.length; i++) {
var letter = config.pwd.charAt(i)
await page.keyboard.press(keyDefinitions[letter].code, { delay: 200 })
}
var need_captcha = await page.evaluate(() => {
var code = document.querySelector('#J-checkcode');
if (code != null && code.className.indexOf("fn-hide") == -1) {
console.log(code)
return true
}
return false
});
if (need_captcha) {
console.log("需要验证码")
await screenshotDOMElement(page, {
path: 'alipay_login_captcha.png',
selector: '#J-checkcode-img',
padding: 5
});
const wait_aptcha = (query) => new Promise(resolve => rl.question(query, (captcha) => resolve(captcha)));
let captcha = await wait_aptcha('请在控制台输入验证码, 图片保存在alipay_login_captcha.png \n');
console.log(`真的是${captcha}吗QAQ……`);
await page.focus("#J-input-checkcode")
for (var i = 0; i < captcha.length; i++) {
var letter = captcha.charAt(i)
await page.keyboard.press(keyDefinitions[letter].code, { delay: 200 })
}
} else {
console.log('恭喜!本次登录没有验证码~');
}
await page.click("#J-login-btn")
try {
await page.waitFor(".userName")
var welcome = await page.evaluate(() => {
var welcome = document.querySelector('#container > div.i-banner > div.i-banner-content.fn-clear > div.i-banner-main > div.i-banner-main-hello.fn-clear > p');
if (welcome != null) {
return welcome.innerText
}
});
if (welcome != {}) {
console.log(welcome)
}
} catch (e) {
console.log("登录貌似没成功~", e)
}
return data
}
async function watchOrder(page, orders) {
var url = "https://consumeprod.alipay.com/record/advanced.htm"
await gotoUrl(page, url);
await page.waitFor("#globalContainer")
var has_risk_qrcode = await page.evaluate(() => {
var risk_qrcode = document.querySelector('#risk_qrcode_cnt');
if (risk_qrcode != null) {
return true
}
return false
});
if (has_risk_qrcode) {
console.log("此页面需要扫描二维码验证")
await screenshotDOMElement(page, {
path: 'alipay_risk_qrcode.png',
selector: '#risk_qrcode_cnt',
padding: 16
});
}
for (; ;) {
try {
await page.waitFor(".J-item ", { timeout: 3000 })
var order_items = await page.evaluate(() => {
var data = {}
var order_items = document.querySelectorAll('.J-item ');
for (var i = 0; i < order_items.length; i++) {
var order_info = {}
var order_item = order_items[i]
var order_no = order_item.querySelector(".tradeNo")
if (order_no != null && order_no.innerText.indexOf("订单号:") !== -1) {
if (order_item.querySelector(".time") != null) {
order_info["time"] = order_item.querySelector(".time").innerText
}
if (order_item.querySelector(".name") != null) {
order_info["name"] = order_item.querySelector(".name").innerText
}
if (order_item.querySelector(".time") != null) {
order_info["memo"] = order_item.querySelector(".memo").innerText
}
if (order_item.querySelector(".tradeNo") != null) {
order_info["order"] = order_item.querySelector(".tradeNo").innerText
}
if (order_item.querySelector(".other") != null) {
order_info["sender"] = order_item.querySelector(".other").innerText
}
if (order_item.querySelector(".amount") != null) {
order_info["amount"] = order_item.querySelector(".amount").innerText
}
if (order_item.querySelector(".detail") != null) {
order_info["detail"] = order_item.querySelector(".detail").innerText
}
if (order_item.querySelector(".status") != null) {
order_info["status"] = order_item.querySelector(".status").innerText
}
data[order_no.innerText] = order_info
}
}
return data
});
for (var key in order_items) {
value = order_items[key]
if (order_items.length > 0 && !orders.hasOwnProperty(key)) {
console.log("新订单:", value)
//todo anything you want
}
orders[key] = value
}
break
} catch (e) {
console.log("获取订单信息出错,需要扫码验证(二维码已存为alipay_risk_qrcode.png):")
await delay(3000)
}
}
return
}
async function refreshAlipayHome(page) {
for (; ;) {
await delay(30 * 1000)
await gotoUrl(page, "https://my.alipay.com/portal/i.htm?referer=https%3A%2F%2Fauth.alipay.com%2Flogin%2Findex.htm")
}
}
async function gotoUrl(page, url) {
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, "webdriver", {
get: () => false
});
});
await page.goto(url)
}
function delay(timeout) {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
async function screenshotDOMElement(page, opts = {}) {
const padding = 'padding' in opts ? opts.padding : 0;
const path = 'path' in opts ? opts.path : null;
const selector = opts.selector;
if (!selector)
throw Error('Please provide a selector.');
const rect = await page.evaluate(selector => {
const element = document.querySelector(selector);
if (!element)
return null;
const { x, y, width, height } = element.getBoundingClientRect();
return { left: x, top: y, width, height, id: element.id };
}, selector);
if (!rect)
throw Error(`Could not find element that matches selector: ${selector}.`);
return await page.screenshot({
path,
clip: {
x: rect.left - padding,
y: rect.top - padding,
width: rect.width + padding * 2,
height: rect.height + padding * 2
}
});
}
getAlipayOrders()