FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript201708/vote/BackRun.js at master · code-brewer/JavaScript201708 · GitHub
code-brewer
JavaScript201708
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript201708
/
vote
/
BackRun.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
393 lines (359 loc) · 12.7 KB
Breadcrumbs
JavaScript201708
/
vote
/
BackRun.js
Copy path
File metadata and controls
393 lines (359 loc) · 12.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*--require built module--*/
let
http
=
require
(
'http'
)
,
fs
=
require
(
'fs'
)
,
url
=
require
(
'url'
)
;
/*--require custom module--*/
let
$
=
require
(
'./BACK/TOOL'
)
;
/*--create server--*/
http
.
createServer
(
(
req
,
res
)
=>
{
let
{
pathname
,
query
}
=
url
.
parse
(
req
.
url
,
true
)
;
//->resource file request
let
suffixReg
=
/
\.
(
[
a
-
z
A
-
Z
0
-
9
]
+
)
$
/
i
;
if
(
suffixReg
.
test
(
pathname
)
)
{
let
suffix
=
suffixReg
.
exec
(
pathname
)
[
1
]
,
suffixMIME
=
$
.
queryMIME
(
suffix
)
;
try
{
$
.
responseData
(
res
,
fs
.
readFileSync
(
`.
${
pathname
}
`
)
,
200
,
suffixMIME
)
;
}
catch
(
e
)
{
$
.
responseData
(
res
,
'NOT FOUND!'
,
404
,
'text/plain'
)
;
}
return
;
}
//->getMatchList
if
(
pathname
===
'/getMatchList'
)
{
let
{
limit
,
page
,
search
,
userId
}
=
query
;
limit
=
parseFloat
(
limit
)
;
page
=
parseFloat
(
page
)
;
userId
=
parseFloat
(
userId
)
;
let
userAry
=
$
.
queryAllUser
(
)
.
reverse
(
)
;
//=>筛选出所有参加比赛的
//=>如果用户传递了搜索记录,还需要在次基础上把与搜索信息匹配的获取到(不能是自己)
userAry
=
userAry
.
filter
(
(
item
)
=>
{
if
(
search
.
length
>
0
)
{
return
item
[
'name'
]
.
indexOf
(
search
)
>
-
1
&&
parseFloat
(
item
[
'isMatch'
]
)
===
1
&&
item
[
'id'
]
!==
userId
;
}
return
parseFloat
(
item
[
'isMatch'
]
)
===
1
&&
item
[
'id'
]
!==
userId
;
}
)
;
//=>如果用户已登录,记录一下当前筛选出来的参赛者是否已经被当前登录用户投递过
if
(
userId
!==
0
)
{
let
voteAry
=
$
.
queryAllVote
(
)
;
userAry
.
forEach
(
(
item
)
=>
{
let
id
=
parseFloat
(
item
[
'id'
]
)
,
flag
=
0
;
voteAry
.
forEach
(
(
voteItem
)
=>
{
if
(
parseFloat
(
voteItem
[
'voterId'
]
)
===
userId
&&
parseFloat
(
voteItem
[
'participantId'
]
)
===
id
)
{
flag
=
1
;
}
}
)
;
item
[
'isVote'
]
=
flag
;
}
)
;
}
//=>计算总页数,获取出当前页面对应的数据
let
total
=
userAry
.
length
,
pageNum
=
Math
.
ceil
(
total
/
limit
)
,
result
=
[
]
;
if
(
page
<=
pageNum
)
{
for
(
let
i
=
(
page
-
1
)
*
limit
;
i
<=
(
page
*
limit
-
1
)
;
i
++
)
{
let
item
=
userAry
[
i
]
;
if
(
!
item
)
break
;
result
.
push
(
{
id
:
parseFloat
(
item
[
'id'
]
)
,
name
:
item
[
'name'
]
,
picture
:
item
[
'picture'
]
,
sex
:
parseFloat
(
item
[
'sex'
]
)
,
matchId
:
parseFloat
(
item
[
'matchId'
]
)
,
slogan
:
item
[
'slogan'
]
,
voteNum
:
parseFloat
(
item
[
'voteNum'
]
)
,
isVote
:
parseFloat
(
item
[
'isVote'
]
)
||
0
}
)
;
}
}
$
.
responseData
(
res
,
JSON
.
stringify
(
{
code
:
result
.
length
===
0
?
1
:
0
,
message
:
result
.
length
===
0
?
'无匹配的信息'
:
'OK'
,
limit
:
limit
,
page
:
page
,
pageNum
:
pageNum
,
total
:
total
,
list
:
result
}
)
)
;
return
;
}
//->vote
if
(
pathname
===
'/vote'
)
{
let
{
userId
,
participantId
}
=
query
;
userId
=
parseFloat
(
userId
)
;
participantId
=
parseFloat
(
participantId
)
;
let
voteAry
=
$
.
queryAllVote
(
)
,
userAry
=
$
.
queryAllUser
(
)
,
result
=
{
code
:
0
,
message
:
'ok'
}
;
if
(
userId
!==
0
)
{
//=>向投票表中存放内容
voteAry
.
push
(
{
id
:
voteAry
.
length
===
0
?
1
:
parseFloat
(
voteAry
[
voteAry
.
length
-
1
]
[
'id'
]
)
+
1
,
voterId
:
userId
,
participantId
:
participantId
,
time
:
new
Date
(
)
.
getTime
(
)
}
)
;
fs
.
writeFileSync
(
'./BACK/JSON/VOTE.JSON'
,
JSON
.
stringify
(
voteAry
)
,
'utf-8'
)
;
//=>更改USER表中的数据:被投人的投票数需要累加1
userAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'id'
]
)
===
participantId
)
{
item
[
'voteNum'
]
=
parseFloat
(
item
[
'voteNum'
]
)
+
1
;
}
}
)
;
fs
.
writeFileSync
(
'./BACK/JSON/USER.JSON'
,
JSON
.
stringify
(
userAry
)
,
'utf-8'
)
;
}
else
{
result
=
{
code
:
1
,
message
:
'投票之前需要先登录'
}
;
}
$
.
responseData
(
res
,
JSON
.
stringify
(
result
)
)
;
return
;
}
//->getUser
if
(
pathname
===
'/getUser'
)
{
let
{
userId
}
=
query
;
userId
=
parseFloat
(
userId
)
;
let
userAry
=
$
.
queryAllUser
(
)
,
result
=
{
code
:
1
,
message
:
'没有找到该用户信息'
,
data
:
null
}
;
userAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'id'
]
)
===
userId
)
{
result
=
{
code
:
0
,
message
:
'ok'
,
data
:
item
}
;
}
}
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
result
)
)
;
return
;
}
//->checkUser
if
(
pathname
===
'/checkUser'
)
{
let
{
userId
,
checkId
}
=
query
;
userId
=
parseFloat
(
userId
)
;
checkId
=
parseFloat
(
checkId
)
;
let
voteAry
=
$
.
queryAllVote
(
)
,
result
=
{
code
:
0
,
message
:
'ok'
,
isVote
:
0
}
;
voteAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'voterId'
]
)
===
userId
&&
parseFloat
(
item
[
'participantId'
]
)
===
checkId
)
{
result
[
'isVote'
]
=
1
;
}
}
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
result
)
)
;
return
;
}
//->getMyVote
if
(
pathname
===
'/getMyVote'
)
{
let
{
userId
}
=
query
;
userId
=
parseFloat
(
userId
)
;
let
voteAry
=
$
.
queryAllVote
(
)
,
userAry
=
$
.
queryAllUser
(
)
,
result
=
[
]
;
voteAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'voterId'
]
)
===
userId
)
{
userAry
.
forEach
(
(
userItem
)
=>
{
if
(
parseFloat
(
userItem
[
'id'
]
)
===
parseFloat
(
item
[
'participantId'
]
)
)
{
result
.
push
(
{
id
:
userItem
[
'id'
]
,
name
:
userItem
[
'name'
]
,
picture
:
userItem
[
'picture'
]
,
sex
:
userItem
[
'sex'
]
,
matchId
:
userItem
[
'matchId'
]
,
slogan
:
userItem
[
'slogan'
]
,
voteNum
:
userItem
[
'voteNum'
]
,
isVote
:
1
}
)
;
}
}
)
;
}
}
)
;
result
.
reverse
(
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
{
code
:
result
.
length
>
0
?
0
:
1
,
message
:
''
,
total
:
result
.
length
,
list
:
result
}
)
)
;
return
;
}
//->getVoteMy
if
(
pathname
===
'/getVoteMy'
)
{
let
{
userId
}
=
query
;
userId
=
parseFloat
(
userId
)
;
let
voteAry
=
$
.
queryAllVote
(
)
,
userAry
=
$
.
queryAllUser
(
)
,
result
=
[
]
;
voteAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'participantId'
]
)
===
userId
)
{
userAry
.
forEach
(
(
userItem
)
=>
{
if
(
parseFloat
(
userItem
[
'id'
]
)
===
parseFloat
(
item
[
'voterId'
]
)
)
{
result
.
push
(
{
id
:
userItem
[
'id'
]
,
name
:
userItem
[
'name'
]
,
picture
:
userItem
[
'picture'
]
,
sex
:
userItem
[
'sex'
]
,
matchId
:
userItem
[
'matchId'
]
,
slogan
:
userItem
[
'slogan'
]
,
voteNum
:
userItem
[
'voteNum'
]
,
isVote
:
0
}
)
;
}
}
)
;
}
}
)
;
result
.
forEach
(
(
item
)
=>
{
let
id
=
parseFloat
(
item
[
'id'
]
)
;
voteAry
.
forEach
(
(
voteItem
)
=>
{
if
(
parseFloat
(
voteItem
[
'voterId'
]
)
===
userId
&&
parseFloat
(
voteItem
[
'participantId'
]
)
===
id
)
{
item
[
'isVote'
]
=
1
;
}
}
)
;
}
)
;
result
.
reverse
(
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
{
code
:
result
.
length
>
0
?
0
:
1
,
message
:
''
,
total
:
result
.
length
,
list
:
result
}
)
)
;
return
;
}
//->checkPhone
if
(
pathname
===
'/checkPhone'
)
{
let
phone
=
query
[
'phone'
]
,
userAry
=
$
.
queryAllUser
(
)
,
flag
=
0
;
userAry
.
forEach
(
(
item
)
=>
{
if
(
item
[
'phone'
]
===
phone
)
{
flag
=
1
;
}
}
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
{
code
:
flag
,
message
:
flag
===
0
?
'当前手机号未被注册'
:
'当前手机号已经被注册'
}
)
)
;
return
;
}
//->register [POST]
if
(
pathname
===
'/register'
)
{
let
userAry
=
$
.
queryAllUser
(
)
,
passData
=
''
;
req
.
on
(
'data'
,
(
chunk
)
=>
{
passData
+=
chunk
;
}
)
;
req
.
on
(
'end'
,
(
)
=>
{
passData
=
$
.
queryURLParameter
(
passData
)
;
let
newInfo
=
{
id
:
userAry
.
length
===
0
?
1
:
parseFloat
(
userAry
[
userAry
.
length
-
1
]
[
'id'
]
)
+
1
,
name
:
passData
[
'name'
]
,
picture
:
passData
[
'sex'
]
==
0
?
'img/man.png'
:
'img/woman.png'
,
phone
:
passData
[
'phone'
]
,
sex
:
passData
[
'sex'
]
,
password
:
$
.
handMD5
(
passData
[
'password'
]
)
,
bio
:
passData
[
'bio'
]
||
''
,
time
:
new
Date
(
)
.
getTime
(
)
,
isMatch
:
0
,
matchId
:
'000'
,
slogan
:
''
,
voteNum
:
0
}
;
userAry
.
push
(
newInfo
)
;
fs
.
writeFileSync
(
'./BACK/JSON/USER.JSON'
,
JSON
.
stringify
(
userAry
)
,
'utf-8'
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
{
code
:
0
,
message
:
'ok'
,
data
:
{
id
:
newInfo
[
'id'
]
,
name
:
newInfo
[
'name'
]
,
password
:
passData
[
'password'
]
}
}
)
)
;
}
)
;
return
;
}
//->login [POST]
if
(
pathname
===
'/login'
)
{
let
userAry
=
$
.
queryAllUser
(
)
,
passData
=
''
;
req
.
on
(
'data'
,
(
chunk
)
=>
{
passData
+=
chunk
;
}
)
;
req
.
on
(
'end'
,
(
)
=>
{
passData
=
$
.
queryURLParameter
(
passData
)
;
let
name
=
passData
[
'name'
]
,
password
=
$
.
handMD5
(
passData
[
'password'
]
)
,
obj
=
null
;
userAry
.
forEach
(
(
item
,
index
)
=>
{
if
(
(
item
[
'name'
]
===
name
||
item
[
'phone'
]
===
name
)
&&
item
[
'password'
]
===
password
)
{
obj
=
{
id
:
item
[
'id'
]
,
name
:
item
[
'name'
]
,
password
:
password
}
;
}
}
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
{
code
:
obj
===
null
?
1
:
0
,
message
:
obj
===
null
?
'用户名密码输入错误'
:
'OK'
,
data
:
obj
}
)
)
;
}
)
;
return
;
}
//->match
if
(
pathname
===
'/match'
)
{
let
userAry
=
$
.
queryAllUser
(
)
,
{
userId
,
slogan
}
=
query
,
result
=
{
code
:
1
,
message
:
'no'
}
;
userId
=
parseFloat
(
userId
)
;
let
maxMatchId
=
0
;
userAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'isMatch'
]
)
===
1
)
{
maxMatchId
=
parseFloat
(
item
[
'matchId'
]
)
>
maxMatchId
?
parseFloat
(
item
[
'matchId'
]
)
:
maxMatchId
;
}
}
)
;
maxMatchId
++
;
if
(
maxMatchId
<
10
)
{
maxMatchId
=
'00'
+
maxMatchId
;
}
else
if
(
maxMatchId
<
100
)
{
maxMatchId
=
'0'
+
maxMatchId
;
}
else
{
maxMatchId
=
''
+
maxMatchId
;
}
userAry
.
forEach
(
(
item
)
=>
{
if
(
parseFloat
(
item
[
'id'
]
)
===
userId
)
{
item
[
'isMatch'
]
=
1
;
item
[
'matchId'
]
=
maxMatchId
;
item
[
'slogan'
]
=
slogan
;
result
=
{
code
:
0
,
message
:
'ok'
}
;
}
}
)
;
fs
.
writeFileSync
(
'./BACK/JSON/USER.JSON'
,
JSON
.
stringify
(
userAry
)
,
'utf-8'
)
;
$
.
responseData
(
res
,
JSON
.
stringify
(
result
)
)
;
return
;
}
//->404
$
.
responseData
(
res
,
'NOT FOUND!'
,
404
,
'text/plain'
)
;
}
)
.
listen
(
8888
,
function
(
)
{
console
.
log
(
'在客户端浏览器中输入 http://localhost:8888/index.html 访问页面!'
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL