-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
227 lines (203 loc) · 5.94 KB
/
worker.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
addEventListener("fetch", event => {
event.respondWith(fetchAndStream(event.request))
});
const kv = await Deno.openKv();
async function fetchAndStream(req) {
const { pathname, searchParams } = new URL(req.url)
let ret = {
msg: 'hello clicli'
}
if (pathname === '/qiandao') {
ret = await qianDao(searchParams.get('uid'), req)
} else if (pathname.indexOf('vip') > -1) {
ret = await vipOp(pathname, searchParams.get('uid'),searchParams.get('price'))
} else if (pathname.indexOf('action') > -1) {
ret = await actionOp(pathname, searchParams.get('uid'), searchParams.get('type'), searchParams.get('pid'))
} else if (pathname.indexOf('danmu') > -1) {
ret = await danmuOp(pathname, req, searchParams.get('pid'), searchParams.get('p'))
} else if (pathname === '/delete') {
// 预留删除接口
const k = searchParams.get('k').split('.')
await kv.delete(k)
return new Response('删除成功')
}
return new Response(JSON.stringify(ret), {
headers: {
"content-type": 'application/json;charset=utf-8',
"Access-Control-Allow-Origin": "*"
}
})
}
async function actionOp(pathname, uid, type, pid) {
let ret = {}
let some = false
let prefix = ['action', pid, type]
let k = ['action', pid, type, uid]
console.log(pathname)
if (pathname === '/action/replace') {
let vv = { uid, type, pid }
const v = await kv.get(k)
if (v.value) {
await kv.delete(k)
} else {
await kv.set(k, vv)
}
ret = {
code: 0,
k,
count: await actionOp('/action/count', uid, type, pid)
}
} else if (pathname === '/action/count') {
const iter = kv.list({ prefix })
const list = []
for await (const res of iter) {
if (res.key.includes(uid)) {
some = true
}
let data = {
id: res.key.join('.'),
...res.value
}
list.push(data)
}
ret = {
code: 0,
count: list.length,
some,
list
}
}
return ret
}
async function danmuOp(pathname, req, pid, p) {
let ret = {}
if (pathname === '/danmu/add') {
const body = await req.json()
if (body.time) {
// 预留更新接口
const kk = ['danmu', body.pid.toString(), (body.p || 0).toString(), body.time]
await kv.set(kk, body)
} else {
// 增加弹幕
const now = new Date().getTime()
const kk = ['danmu', body.pid.toString(), (body.p || 0).toString(), now.toString()]
body.time = now
await kv.set(kk, body)
await peaOp(body.uid.toString(), '/pea/add')
}
ret = {
code: 0,
}
} else if (pathname === '/danmu/list') {
const prefix = ['danmu', pid, p]
const iter = kv.list({ prefix })
const list = []
for await (const res of iter) {
let data = {
id: res.key.join('.'),
...res.value
}
list.push(data)
}
ret = {
code: 0,
list,
}
}
return ret
}
async function vipOp(pathname, uid, price) {
const list = {'0.5':24,'10':24*30,'25':24*30*3,'90':24*30*12}
const k = ['vip', uid]
let expireTime = await kv.get(k)
let ret = {}
if (pathname === '/vip/add') {
expireTime.value = expireTime.value + 3600 * 1000 * (list[price]||1)
await kv.set(k, expireTime.value)
ret = {
code: 0,
expire: expireTime.value
}
} else {
if (!expireTime.value) {
expireTime.value = Date.now()
await kv.set(k, expireTime.value)
}
ret = { code: 0, expire: expireTime.value }
}
return ret
}
async function peaOp(uid, pathname) {
const k = ['pea', uid]
let currentPea = await kv.get(k)
let ret = {}
if (pathname === '/pea/add') {
if (currentPea.value) {
currentPea.value = currentPea.value + 1
await kv.set(k, currentPea.value)
} else {
currentPea.value = 1
await kv.set(k, currentPea.value)
}
ret = {
code: 0,
pea: currentPea.value
}
} else if (pathname === '/pea/cut') {
if (currentPea.value) {
currentPea.value = currentPea.value - 1
await kv.set(k, currentPea.value)
ret = {
code: 0,
pea: currentPea.value
}
} else {
currentPea.value = 0
await kv.set(k, 0)
ret = {
code: 1,
pea: currentPea.value
}
}
} else {
ret = { code: 0, pea: currentPea.value }
}
return ret
}
async function qianDao(uid, req) {
const now = new Date().getTime()
const nexttime = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1
const k = ['qiandao', uid]
let ret = {}
const qiandaoTime = await kv.get(k)
if (req.method === "GET") { // 检查状态
// 没超时,处于签到状态
if (now < qiandaoTime.value) {
ret = {
code: 1,
msg: "已签到"
}
} else {
ret = {
code: 0,
msg: "签到"
}
}
} else {
// 如果没超时,什么也不做
if (now < qiandaoTime.value) {
ret = {
code: 1,
msg: "重复签到"
}
} else { // 超时了
await kv.set(k, nexttime)
await vipOp('/vip/add',uid)
ret = {
code: 1,
msg: "签到成功"
}
}
}
return ret
}