shoes.js
7.23 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
/**
* 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 = [];
_.each(userInfo, (obj) => {
// console.log(obj);
result.push(obj);
});
return res.json({
code: 200,
count: result.length || 0,
data: result,
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) {
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: 204,
message: '插入用户结果失败'
});
}
} catch (err) {
return res.send(err);
}
} else {
res.json({
code: 201,
message: '用户已经进行过游戏'
});
}
},
/**
* 获取用户得分以及好友得分列表
* @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);
}
},
async getImageData(req, res) {
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
});
});
},
};
module.exports = shoes;