shoes.js
18.5 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/**
* Created by qiujun on 2018/4/13.
*/
const ShoesModel = require('../models/shoes');
const _ = require('lodash');
const INVALID_PARAMS = '参数错误';
const http = require('http');
const shoes = {
/**
*检查并添加好友
* @param req
* @param res
* @param next
*/
async checkAddFriends(req, res) {
let {unionId, friendUnionId, actId = 27} = req.query;
if (!unionId || !friendUnionId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
if (unionId === friendUnionId) {
return res.json({
code: 401,
message: '自己不能挑战自己'
});
}
try {
let checkFriend = await req.ctx(ShoesModel).checkFriends(unionId, friendUnionId);
if (checkFriend && checkFriend.length > 0) {
return res.json({
code: 200,
data: checkFriend[0],
message: '查询好友成功'
});
} else {
let addFriend = await req.ctx(ShoesModel).addFriends(actId, unionId, friendUnionId);
// console.log(addFriend);
return res.json({
code: 200,
data: addFriend,
message: '添加好友成功'
});
}
} catch (err) {
return res.json(err);
}
},
/**
* 查询用户游戏记录
* @param req
* @param res
* @returns {Promise.<void>}
*/
async checkUserPlayed(req, res) {
let {actId = 27, unionId} = req.query;
try {
let userInfo = await req.ctx(ShoesModel).checkUserPlayed(actId, unionId);
let result = [];
// console.log(userInfo);
_.each(userInfo.data || [], (obj) => {
// console.log(obj);
result.push(obj);
});
return res.json({
code: 200,
count: result.length || 0,
data: result,
phone: userInfo.phone || 0,
message: '查询用户游戏信息成功'
});
} catch (err) {
return res.json(err);
}
},
/**
* 保存用户游戏记录
* @param req
* @param res
* @returns {Promise.<void>}
*/
async saveUserResult(req, res) {
let {actId = 29, unionId, list = [], question} = req.body;
if (list === []) {
return res.json({
code: 204,
actId: actId,
unionId: unionId,
message: INVALID_PARAMS
});
} else if (parseInt(question, 10) === 0) {
try {
list = JSON.parse(list);
let userInfo = await req.ctx(ShoesModel).getUserId(unionId);
let userId = 0;
let result = [];
if (userInfo.length > 0) {
userId = userInfo[0].id;
}
if (userId) {
let getUserPlayed = await req.ctx(ShoesModel).getUserPlayedInfo(actId, unionId);
// console.log(getUserPlayed);
if (getUserPlayed.code === 200 || (getUserPlayed.data && getUserPlayed.data.length === 0)) {
for (let i = 0; i < list.length; i++) {
let qsObj = list[i];
let qs = await req.ctx(ShoesModel).
saveResult(
actId,
userId,
qsObj.questionId,
qsObj.userSelectOption || 'F',
qsObj.isCorrect,
qsObj.time || 1000
);
result.push(qs);
}
return res.json({
code: 200,
userId,
actId,
unionId,
data: result,
message: '插入用户结果成功'
});
} else {
return res.json({
code: 205,
message: '请勿重复提交数据'
});
}
} else {
return res.json({
code: 204,
message: '插入用户结果失败'
});
}
} catch (err) {
return res.send(err);
}
} else {
res.json({
code: 201,
message: '用户已经进行过游戏'
});
}
},
/**
* 保存用户游戏记录
* @param req
* @param res
* @returns {Promise.<void>}
*/
async saveUserResultWithToken(req, res) {
let {actId = 29, unionId, list = [], question, token} = req.body;
if (list === [] || !token) {
return res.json({
code: 400,
actId: actId,
unionId: unionId,
message: INVALID_PARAMS
});
} else if (parseInt(question, 10) === 0) {
let session_token = req.session.shoesToken;
// console.log('save:', session_token, token);
if (session_token !== token) {
return res.json({
code: 400,
message: INVALID_PARAMS,
data: token + '_' + (session_token || '0')
});
}
try {
list = JSON.parse(list);
let userInfo = await req.ctx(ShoesModel).getUserId(unionId);
let userId = 0;
let result = [];
if (userInfo.length > 0) {
userId = userInfo[0].id;
}
if (userId) {
let getUserPlayed = await req.ctx(ShoesModel).getUserPlayedInfo(actId, unionId);
// console.log(getUserPlayed);
if (getUserPlayed.code === 200 || (getUserPlayed.data && getUserPlayed.data.length === 0)) {
for (let i = 0; i < list.length; i++) {
let qsObj = list[i];
let qs = await req.ctx(ShoesModel).
saveResult(
actId,
userId,
qsObj.questionId,
qsObj.userSelectOption || 'F',
qsObj.isCorrect,
qsObj.time || 1000,
qsObj.click || 0
);
result.push(qs);
}
return res.json({
code: 200,
userId,
actId,
unionId,
data: result,
message: '插入用户结果成功'
});
} else {
return res.json({
code: 205,
message: '请勿重复提交数据'
});
}
} else {
return res.json({
code: 204,
message: '插入用户结果失败'
});
}
} catch (err) {
return res.send(err);
}
} else {
res.json({
code: 201,
message: '用户已经进行过游戏'
});
}
},
/**
* 可以重复游戏的保存用户信息
* @param req
* @param res
* @returns {Promise.<void>}
*/
async saveUserResultWithReplay(req, res) {
let {actId = 37, unionId, list = [], token} = req.body;
if (list === [] || !token) {
return res.json({
code: 400,
actId: actId,
unionId: unionId,
message: INVALID_PARAMS
});
} else {
let session_token = req.session.shoesToken;
// console.log('save:', session_token, token);
if (session_token !== token) {
return res.json({
code: 400,
message: INVALID_PARAMS,
data: token + '_' + (session_token || '0')
});
}
try {
list = JSON.parse(list);
let userInfo = await req.ctx(ShoesModel).getUserId(unionId);
let userId = 0;
let result = [];
if (userInfo.length > 0) {
userId = userInfo[0].id;
}
if (userId) {
let getUserPlayed = await req.ctx(ShoesModel).getUserPlayedInfo(actId, unionId);
if (getUserPlayed.code === 200 || (getUserPlayed.data && getUserPlayed.data.length === 0)) {
for (let i = 0; i < list.length; i++) {
let qsObj = list[i];
let qs = await req.ctx(ShoesModel).
saveResult(
actId,
userId,
qsObj.questionId,
qsObj.userSelectOption || 'F',
qsObj.isCorrect,
qsObj.time || 1000,
qsObj.cts || 1
);
result.push(qs);
}
return res.json({
code: 200,
userId,
actId,
unionId,
data: result,
message: '插入用户结果成功'
});
} else {
let scoreInfo = await req.ctx(ShoesModel).getUserRankScore(unionId, actId, 10);
let newScoreInfo = {
score: 0,
playTime: 0,
clickTimes: 0
};
let canUpdate = false;
for (let i = 0; i < list.length; i++) {
newScoreInfo.score += parseInt(list[i].isCorrect, 10) ? 10 : 0;
newScoreInfo.playTime += list[i].time;
newScoreInfo.clickTimes += list[i].cts || 1;
}
if (newScoreInfo.score > scoreInfo.score) { // 分数比原来高的情况
canUpdate = true;
} else if (newScoreInfo.score === scoreInfo.score) { // 分数和原来相同时,看点击次数多少
if (newScoreInfo.clickTimes < scoreInfo.clickTimes) { // 点击次数少于原来
canUpdate = true;
} else if (newScoreInfo.clickTimes === scoreInfo.clickTimes) { // 点击次数等于原来,看时间长短
if (newScoreInfo.playTime < scoreInfo.playTime) {
canUpdate = true;
}
}
}
if (canUpdate) {
await req.ctx(ShoesModel).deleteUserPlayInfo(actId, userId);
for (let i = 0; i < list.length; i++) {
let qsObj = list[i];
let qs = await req.ctx(ShoesModel).
saveResult(
actId,
userId,
qsObj.questionId,
qsObj.userSelectOption || 'F',
qsObj.isCorrect,
qsObj.time || 1000,
qsObj.cts || 1
);
result.push(qs);
}
return res.json({
code: 200,
userId,
actId,
unionId,
data: newScoreInfo,
message: '插入用户结果成功'
});
} else {
return res.json({
code: 200,
userId,
actId,
unionId,
data: [],
message: '无需更新用户信息'
});
}
}
} else {
return res.json({
code: 204,
message: '插入用户结果失败'
});
}
} catch (err) {
return res.send(err);
}
}
},
/**
* 获取用户得分以及好友得分列表
* @param req
* @param res
* @returns {Promise.<void>}
*/
async getUserShareInfo(req, res) {
let {unionId, actId = 29, per = 10} = req.query;
if (!unionId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
let scoreInfo = await req.ctx(ShoesModel).getUserScore(unionId, actId, per);
let friendsInfo = await req.ctx(ShoesModel).getFriendsScoreList(scoreInfo.userid, actId, per, scoreInfo.score);
return res.json(
{
code: 200,
message: '好友得分列表',
data: {
currentUser: scoreInfo,
record: friendsInfo.record,
friendList: friendsInfo.friendList
}
}
);
} catch (err) {
return res.json(err);
}
},
/**
* 获取全球排名信息
* @param req
* @param res
* @returns {Promise.<void>}
*/
async getUserPlayInfo(req, res) {
let {unionId, actId = 29, per = 10} = req.query;
if (!unionId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
let scoreInfo = await req.ctx(ShoesModel).getUserRankScore(unionId, actId, per);
let friendsInfo = await req.ctx(ShoesModel).getAllUserScoreList(actId, per);
return res.json(
{
code: 200,
message: '全球排名列表',
data: {
currentUser: scoreInfo,
friendList: friendsInfo
}
}
);
} catch (err) {
return res.json(err);
}
},
/**
* 获取用户手机及排名列表
* @param req
* @param res
* @returns {Promise.<void>}
*/
async getUserPhoneList(req, res) {
let {actId = 37, per = 10, limit = 150} = req.query;
try {
let friendsInfo = await req.ctx(ShoesModel).getUserPhoneList(actId, per, limit);
let htmlHead = `<table cellpadding="0" cellspacing="0" border="1" width="100%">
<tr style="background-color: #00aeef;">
<td width="12%" height="30px">名次</td>
<td width="12%" height="30px">手机</td>
<td width="12%" height="30px">昵称</td>
<td width="12%" height="30px">得分</td>
<td width="12%" height="30px">时间</td>
<td width="12%" height="30px">点击</td>
<td width="12%" height="30px">userId</td>
<td width="12%" height="30px">日期</td>
</tr>`;
let innerHtml = '';
_.each(friendsInfo, item => {
innerHtml += `<tr><td>${item.rank}</td>
<td>${item.userPhone}</td>
<td>${item.nickname}</td>
<td>${item.score}</td>
<td>${item.playTime / 1000}s</td>
<td>${item.clickTimes}</td>
<td>${item.userId}</td>
<td>${item.createTime}</td></tr>`;
});
let htmlEnd = '</table>';
return res.send(htmlHead + innerHtml + htmlEnd);
} catch (err) {
return res.json(err);
}
},
/**
* 删除用户的游戏信息
*/
deleteUserPlayInfo(req, res) {
let {actId, userId = ''} = req.query;
if (!actId || !userId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
let userIds = userId.toString().replace(/,/g, ',').split(',');
return req.ctx(ShoesModel).deleteUserPlayInfo(actId, userIds).then(result => {
res.json({
code: 200,
userId: result.userId,
actId: result.actId,
message: `已成功删除${result.data}条数据`
});
});
},
async getImageData(req, res, next) {
let pic_url = req.query.image;
if (!pic_url) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
return new Promise((resolve, reject) => {
http.get(pic_url, (reqs) => {
let chunks = [];
let size = 0;
reqs.on('data', (data) => {
chunks.push(data);
size += data.length;
});
reqs.on('end', () => {
let data = Buffer.concat(chunks, size);
let base64Image = 'data:image/jpg;base64,' + data.toString('base64');
return resolve(base64Image);
});
reqs.on('error', () => {
return reject(pic_url);
});
});
}).then(result => {
res.json({
data: result
});
}).catch(err => {
next(err);
});
},
/**
* 获取答题的token
* @param req
* @param res
*/
getShoesToken(req, res) {
let referer = req.get('Referer');
let token = Math.random().toString(16).substring(2, 15);
referer = 'https://ad.yoho.cn'; // 用于测试,记得要删
if (referer && referer.indexOf('ad.yoho.cn') > 0) {
req.session.shoesToken = token;
// console.log('shoesToken:', req.session.shoesToken);
res.json({
data: token,
});
} else {
res.json({
data: '0'
});
}
}
};
module.exports = shoes;