prize.js
8.26 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
/**
* 后台抽奖以及记录中奖用户信息
* Created by qiujun on 2018/1/30.
*/
const PrizeModel = require('../models/prize');
const moment = require('moment');
const INVALID_PARAMS = '参数错误';
const prize = {
/**
* 获取用户中奖信息
* @param req
* @param res
* @param next
*/
checkPrize(req, res, next) {
let query = req.query;
let actId = query.actId;
let userPhone = query.phone;
if (!actId || !userPhone || userPhone.length !== 11) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
req.ctx(PrizeModel).checkPrize(actId, userPhone).then(ret => {
res.json(ret);
}).catch(next);
},
/**
* 抽奖并先写入用户的手机号码防止重复抽奖
* @param req
* @param res
* @param next
* @returns {*|Promise.<T>}
*/
getPrize(req, res, next) {
let query = req.body;
let actId = query.actId;
let z = query.z || 5; // 中奖概率设置,z:中,
let bz = query.bz || 95;
let userPhone = query.phone;
console.log(actId, userPhone);
let baseArr = [];
let prizeArr = [];
if (!actId || !userPhone || userPhone.length !== 11) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
for (let i = 0; i < z; i++) {
baseArr.push(1);
}
for (let i = 0; i < bz; i++) {
prizeArr.push(0);
}
baseArr.forEach(item => { // 随机往prizeArr中插入一个中奖数字1
let rand = Math.floor(Math.random() * prizeArr.length);
prizeArr.splice(rand, 0, item);
});
let isGet = prizeArr[Math.floor(Math.random() * prizeArr.length)]; // 随机抽取一个,如果是1就中奖,0就不中
console.log('isGet=' + isGet);
return req.ctx(PrizeModel).addPrize(actId, userPhone, isGet).then(ret => {
res.json(Object.assign(ret, {prize: isGet}));
}).catch(next);
},
/**
* 抽奖并先写入用户的手机号码防止重复抽奖, 4月9日新抽奖规则
* @param req
* @param res
* @param next
*/
async getNewPrize(req, res, next) {
let query = req.body;
let actId = query.actId;
let z = query.z || 4;
let bz = query.bz || 196;
let userPhone = query.phone;
console.log(actId, userPhone);
let baseArr = [];
let prizeArr = [];
if (!actId || !userPhone || userPhone.length !== 11) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
for (let i = 0; i < z; i++) {
baseArr.push(1);
}
for (let i = 0; i < bz; i++) {
prizeArr.push(0);
}
baseArr.forEach(item => { // 随机往prizeArr中插入一个中奖数字1
let rand = Math.floor(Math.random() * prizeArr.length);
prizeArr.splice(rand, 0, item);
});
let isGet = prizeArr[Math.floor(Math.random() * prizeArr.length)]; // 随机抽取一个,如果是1就中奖,0就不中
const userPrizeCount = await req.ctx(PrizeModel).checkUserPrize(actId, userPhone);
console.log('isGet=' + isGet, 'userPrizeCount=' + userPrizeCount);
if (isGet) {
console.log(userPrizeCount);
if (userPrizeCount === 0) {
let result;
let prizeNum_3 = await req.ctx(PrizeModel).checkPrizeNums(actId, 3);
console.log('prizeNum_3', prizeNum_3);
if (prizeNum_3 < 20) { // 先判断三等奖是否被抽完
result = await req.ctx(PrizeModel).addNewPrize(actId, userPhone, 3);
res.json({
code: 200,
prize: 3,
data: result,
message: '恭喜您中了三等奖!'
}).catch(next);
} else {
let prizeNum_2 = await req.ctx(PrizeModel).checkPrizeNums(actId, 2);
console.log('prizeNum_2', prizeNum_2);
if (prizeNum_2 < 10) { // 然后判断二等奖是否被抽完
result = await req.ctx(PrizeModel).addNewPrize(actId, userPhone, 2);
res.json({
code: 200,
prize: 2,
data: result,
message: '恭喜您中了二等奖!'
}).catch(next);
} else {
let prizeNum_1 = await req.ctx(PrizeModel).checkPrizeNums(actId, 1);
console.log('prizeNum_1', prizeNum_1);
if (prizeNum_1 < 1) { // 最后判断一等奖是否被抽完
result = await req.ctx(PrizeModel).addNewPrize(actId, userPhone, 1);
res.json({
code: 200,
prize: 1,
data: result,
mesage: '恭喜您中了一等奖'
}).catch(next);
} else { // 奖品全部领完的情况
result = await req.ctx(PrizeModel).addNewPrize(actId, userPhone, 0);
res.json({
code: 204,
prize: 0,
data: result,
message: '奖品已领取完'
}).catch(next);
}
}
}
} else {
res.json({
code: 203,
message: '请勿重复抽奖'
});
}
} else {
if (userPrizeCount === 0) {
let result = await req.ctx(PrizeModel).addNewPrize(actId, userPhone, 0);
res.json({
code: 200,
prize: 0,
data: result,
message: '很抱歉,您没有中奖'
});
} else {
res.json({
code: 203,
message: '请勿重复抽奖'
});
}
}
},
/**
* 中奖用户更新自己的用户信息
* @param req
* @param res
* @param next
*/
updatePrize(req, res, next) {
let query = req.body;
let actId = query.actId;
let userPhone = query.phone;
let userAddress = decodeURIComponent(query.address).replace(/<.*?>/g, '');
let userName = decodeURIComponent(query.name);
let userSize = query.size;
console.log(req.body);
if (!actId || !userPhone || !userAddress || !userName || !userSize) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
return req.ctx(PrizeModel).updatePrize(actId, userPhone, userName, userAddress, userSize).then(ret => {
res.json(ret);
}).catch(next);
},
/**
* 保存用户信息
* @param req
* @param res
* @param next
*/
saveUserInfo(req, res, next) {
let query = req.body;
let actId = query.actId;
let name = query.name;
let birthday = query.birthday || moment().format('YYYY-MM-DD');
let gender = query.gender || 0;
let phone = query.phone;
let email = query.email;
let province = query.province;
let city = query.city;
console.log(actId, name, birthday, gender, phone, email, province, city);
if (!actId || !phone || !name || phone.length !== 11 || email.indexOf('@') < 0) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
return req.ctx(PrizeModel)
.saveUserInfo(actId, name, birthday, gender, phone, email, province, city)
.then(ret => {
res.json(ret);
}).catch(next);
}
};
module.exports = prize;