forked from shufflewzc/faker2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jd_aliyunautoSignin.js
207 lines (183 loc) · 5.59 KB
/
jd_aliyunautoSignin.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
/*
cron "0 9 * * *" jd_aliyunautoSignin.js, tag=阿里云盘签到
*/
const axios = require('axios')
const { initInstance, getEnv, updateCkEnv } = require('./function/qlApi.js')
const notify = require('./sendNotify')
const updateAccesssTokenURL = 'https://auth.aliyundrive.com/v2/account/token'
const signinURL =
'https://member.aliyundrive.com/v1/activity/sign_in_list?_rx-s=mobile'
const rewardURL =
'https://member.aliyundrive.com/v1/activity/sign_in_reward?_rx-s=mobile'
// 使用 refresh_token 更新 access_token
function updateAccesssToken(queryBody, remarks) {
const errorMessage = [remarks, '更新 access_token 失败']
return axios(updateAccesssTokenURL, {
method: 'POST',
data: queryBody,
headers: { 'Content-Type': 'application/json' }
})
.then(d => d.data)
.then(d => {
const { code, message, nick_name, refresh_token, access_token } = d
if (code) {
if (
code === 'RefreshTokenExpired' ||
code === 'InvalidParameter.RefreshToken'
)
errorMessage.push('refresh_token 已过期或无效')
else errorMessage.push(message)
return Promise.reject(errorMessage.join(', '))
}
return { nick_name, refresh_token, access_token }
})
.catch(e => {
errorMessage.push(e.message)
return Promise.reject(errorMessage.join(', '))
})
}
//签到列表
function sign_in(access_token, remarks) {
const sendMessage = [remarks]
return axios(signinURL, {
method: 'POST',
data: {
isReward: false
},
headers: {
Authorization: access_token,
'Content-Type': 'application/json'
}
})
.then(d => d.data)
.then(async json => {
if (!json.success) {
sendMessage.push('签到失败', json.message)
return Promise.reject(sendMessage.join(', '))
}
sendMessage.push('签到成功')
const { signInLogs, signInCount } = json.result
const currentSignInfo = signInLogs[signInCount - 1] // 当天签到信息
sendMessage.push(`本月累计签到 ${signInCount} 天`)
// 未领取奖励列表
const rewards = signInLogs.filter(
v => v.status === 'normal' && !v.isReward
)
if (rewards.length) {
for await (reward of rewards) {
const signInDay = reward.day
try {
const rewardInfo = await getReward(access_token, signInDay)
sendMessage.push(
`第${signInDay}天奖励领取成功: 获得${rewardInfo.name || ''}${
rewardInfo.description || ''
}`
)
} catch (e) {
sendMessage.push(`第${signInDay}天奖励领取失败:`, e)
}
}
} else if (currentSignInfo.isReward) {
sendMessage.push(
`今日签到获得${currentSignInfo.reward.name || ''}${
currentSignInfo.reward.description || ''
}`
)
}
return sendMessage.join(', ')
})
.catch(e => {
sendMessage.push('签到失败')
sendMessage.push(e.message)
return Promise.reject(sendMessage.join(', '))
})
}
// 领取奖励
function getReward(access_token, signInDay) {
return axios(rewardURL, {
method: 'POST',
data: { signInDay },
headers: {
authorization: access_token,
'Content-Type': 'application/json'
}
})
.then(d => d.data)
.then(json => {
if (!json.success) {
return Promise.reject(json.message)
}
return json.result
})
}
// 获取环境变量
async function getRefreshToken() {
let instance = null
try {
instance = await initInstance()
} catch (e) {}
let refreshToken = process.env.refreshToken || []
try {
if (instance) refreshToken = await getEnv(instance, 'refreshToken')
} catch (e) {}
let refreshTokenArray = []
if (Array.isArray(refreshToken)) refreshTokenArray = refreshToken
else if (refreshToken.indexOf('&') > -1)
refreshTokenArray = refreshToken.split('&')
else if (refreshToken.indexOf('\n') > -1)
refreshTokenArray = refreshToken.split('\n')
else refreshTokenArray = [refreshToken]
if (!refreshTokenArray.length) {
console.log('未获取到refreshToken, 程序终止')
process.exit(1)
}
return {
instance,
refreshTokenArray
}
}
!(async () => {
const { instance, refreshTokenArray } = await getRefreshToken()
const message = []
let index = 1
for await (refreshToken of refreshTokenArray) {
let remarks = refreshToken.remarks || `账号${index}`
const queryBody = {
grant_type: 'refresh_token',
refresh_token: refreshToken.value || refreshToken
}
try {
const { nick_name, refresh_token, access_token } =
await updateAccesssToken(queryBody, remarks)
if (nick_name && nick_name !== remarks)
remarks = `${nick_name}(${remarks})`
// 更新环境变量
if (instance) {
let params = {
name: refreshToken.name,
value: refresh_token,
remarks: refreshToken.remarks || nick_name // 优先存储原有备注信息
}
// 新版青龙api
if (refreshToken.id) {
params.id = refreshToken.id
}
// 旧版青龙api
if (refreshToken._id) {
params._id = refreshToken._id
}
await updateCkEnv(instance, params)
}
const sendMessage = await sign_in(access_token, remarks)
console.log(sendMessage)
console.log('\n')
message.push(sendMessage)
} catch (e) {
console.log(e)
console.log('\n')
message.push(e)
}
index++
}
await notify.sendNotify(`阿里云盘签到`, message.join('\n'))
})()