forked from vv314/actions-mtz-coupons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
81 lines (62 loc) · 1.85 KB
/
user.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
import fetch, { createCookieJar } from './fetch.js'
import { ECODE } from './coupons/const.js'
function tokenFormat(token, index = 0) {
const defToken = {
token: '',
alias: '',
index: index + 1,
tgUid: '',
qywxUid: '',
barkKey: '',
larkWebhook: '',
qq: ''
}
if (typeof token == 'string') {
token = { token }
}
return Object.assign({}, defToken, token)
}
function parseToken(token) {
if (!token) throw '请配置 TOKEN'
const likeJson = ['{', '['].includes(token.trim()[0])
if (!likeJson) return [tokenFormat(token)]
try {
token = JSON.parse(token)
} catch (e) {
throw `TOKEN 解析错误: ${e.message}`
}
return [].concat(token).map(tokenFormat)
}
async function getMTUerId() {
const rep = await fetch('https://h5.waimai.meituan.com/waimai/mindex/home')
const repCookie = rep.headers.get('set-cookie') || ''
const matchArr = repCookie.match(/userId=(\w+)/) || []
return matchArr[1] || ''
}
async function getUserInfo(cookie, guard) {
const res = await fetch.post(
'https://mediacps.meituan.com/gundam/gundamLogin',
null,
{
cookie: cookie,
guard: guard
}
)
if (res.code == 0) return res.data
if (res.code == 3) {
throw { code: ECODE.AUTH, api: 'gundamLogin', msg: res.msg || res.message }
}
throw { code: ECODE.API, api: 'gundamLogin', msg: res.msg || res.message }
}
function createMTCookie(token) {
const cookieJar = createCookieJar(token)
const domain = 'Domain=.meituan.com'
const path = 'Path=/'
const http = 'HttpOnly'
const expire = 'Max-Age=3600'
const content = token.startsWith('token=') ? token : `token=${token}`
const cookieStr = [content, domain, path, http, expire].join(';')
cookieJar.setCookie(cookieStr, 'https://mediacps.meituan.com')
return cookieJar
}
export { createMTCookie, getUserInfo, getMTUerId, parseToken }