...
|
...
|
@@ -41,7 +41,7 @@ const prize = { |
|
|
getPrize(req, res, next) {
|
|
|
let query = req.body;
|
|
|
let actId = query.actId;
|
|
|
let z = query.z || 5; // 中奖概率设置,
|
|
|
let z = query.z || 5; // 中奖概率设置,z:中,
|
|
|
let bz = query.bz || 95;
|
|
|
let userPhone = query.phone;
|
|
|
|
...
|
...
|
@@ -80,6 +80,130 @@ const prize = { |
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 抽奖并先写入用户的手机号码防止重复抽奖, 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: '恭喜您中了三等奖!'
|
|
|
});
|
|
|
} 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: '恭喜您中了二等奖!'
|
|
|
});
|
|
|
} 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: '恭喜您中了一等奖'
|
|
|
});
|
|
|
} else { // 奖品全部领完的情况
|
|
|
result = await req.ctx(PrizeModel).addNewPrize(actId, userPhone, 0);
|
|
|
|
|
|
res.json({
|
|
|
code: 204,
|
|
|
prize: 0,
|
|
|
data: result,
|
|
|
message: '奖品已领取完'
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} 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
|
...
|
...
|
@@ -93,6 +217,8 @@ const prize = { |
|
|
let userName = decodeURIComponent(query.name);
|
|
|
let userSize = query.size;
|
|
|
|
|
|
console.log(req.body);
|
|
|
|
|
|
if (!actId || !userPhone || !userAddress || !userName || !userSize) {
|
|
|
return res.json({
|
|
|
code: 400,
|
...
|
...
|
|