-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
53 lines (51 loc) · 1.73 KB
/
api.js
File metadata and controls
53 lines (51 loc) · 1.73 KB
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
import request from "request";
var cookie;
export async function main(kuaidi_number) {
let token = await getToken(kuaidi_number); //token可以多次使用的,但这里就不写多次使用了避免特殊情况发生
console.log("获取的token为:" + token, "要查询的订单号为:" + kuaidi_number);
let data = await kuaidi(token, kuaidi_number);
console.log(data);
return data;
}
//获取token和cookie的值
export function getToken(kuaidi_number) {
return new Promise((resolve) => {
let opts = {
url: "https://www.baidu.com/s?wd=" + kuaidi_number,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36",
},
};
request.get(opts, (_e, b, d) => {
cookie = b.headers["set-cookie"].toString(); //获取传回来的cookie值
let checkUrl = d.substring(d.indexOf("checkUrl")); //截取字符串获取tokenv2的值
checkUrl = checkUrl.substring(
checkUrl.indexOf("tokenV2=") + 8,
checkUrl.indexOf('",')
);
resolve(checkUrl);
});
});
}
//请求快递数据
export function kuaidi(token, kuaidi_number) {
return new Promise((resolve) => {
let opts = {
url:
"https://express.baidu.com/express/api/express?cb=&tokenV2=" +
token +
"&nu=" +
kuaidi_number,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36",
Connection: "keep-alive",
Cookie: cookie,
},
};
request.get(opts, (_e, _d, b) => {
resolve(JSON.parse(b));
});
});
}