Authored by 邱骏

球鞋抽奖及用户信息收集

/**
* 后台抽奖以及记录中奖用户信息
* 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; // 中奖概率设置,
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);
},
/**
* 中奖用户更新自己的用户信息
* @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;
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;
... ...
/**
* 抽奖model
* @author: qiujun <jun.qiu@yoho.cn>
* @date: 30/01/2018
*/
const mySqlCli = global.yoho.utils.mysqlCli;
const TABLE_PRIZE = 'act_prize_user_log';
const TABLE_USER_DETAIL = 'act_user_detail_info';
class PrizeModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 检查用户的获奖信息
* @param actId
* @param userPhone
*/
checkPrize(actId, userPhone) {
let strSql = `SELECT id, user_phone, is_get, user_name, user_address, user_shoes_size FROM ${TABLE_PRIZE}
WHERE act_id = :actId
AND user_phone = :userPhone`;
return mySqlCli.query(strSql, {
actId,
userPhone
}).then(ret => {
let result = {};
console.log(ret);
if (ret.length <= 0) {
result = {
code: 201,
message: '未找到获奖信息'
};
} else if (ret.length > 0) {
result = {
code: 200,
data: ret
};
}
return Promise.resolve(result);
});
}
/**
* 检查并添加中奖者基础信息
* @param actId
* @param userPhone
* @param isGet
*/
addPrize(actId, userPhone, isGet) {
let strFindSql = `SELECT COUNT(*) as user_prize_count FROM ${TABLE_PRIZE}
WHERE act_id = :actId
AND user_phone = :userPhone`;
let strCountSql = `SELECT COUNT(*) AS total_prize_count FROM ${TABLE_PRIZE}
WHERE act_id = :actId AND is_get = 1`;
let strInsertSql = `INSERT INTO ${TABLE_PRIZE} (act_id, user_phone, is_get)
VALUES(:actId, :userPhone, :isGet)`;
return mySqlCli.query(strFindSql, {
actId,
userPhone
}).then(ret => {
console.log(ret);
if (ret[0] && ret[0].user_prize_count <= 0) {
return mySqlCli.query(strCountSql, {
actId
}).then(ret2 => {
console.log(ret2);
let result = {};
if (ret2[0].total_prize_count >= 20) {
isGet = 0;
result = {
code: 204,
message: '奖品已领取完'
};
} else {
result = {
code: 200,
message: '抽奖信息添加成功!'
};
}
return mySqlCli.insert(strInsertSql, {
actId,
userPhone,
isGet
}).then(ret3 => {
return Promise.resolve(Object.assign(result, {data: ret3}));
});
});
} else {
return Promise.resolve({
code: 203,
message: '请勿重复领奖'
});
}
});
}
/**
* 更新中奖者的详细信息
* @param actId
* @param userPhone
* @param name
* @param address
* @param shoesSize
*/
updatePrize(actId, userPhone, name, address, shoesSize) {
let strSql = `UPDATE ${TABLE_PRIZE} SET
user_name=:name,
user_address=:address,
user_shoes_size=:shoesSize
WHERE user_phone=:userPhone
AND is_get=1
AND act_id=:actId`;
return mySqlCli.update(strSql, {
actId,
userPhone,
name,
address,
shoesSize
}).then(ret => {
return Promise.resolve({
code: 200,
data: ret
});
});
}
saveUserInfo(actId, name, birthday, gender, phone, email, province, city) {
let strFind = `SELECT COUNT(*) AS user_count
FROM ${TABLE_USER_DETAIL}
WHERE act_id=:actId
AND user_phone=:phone`;
let strInsert = `INSERT INTO ${TABLE_USER_DETAIL}
(act_id, user_name, user_birthday, user_gender,
user_phone, user_email, user_province, user_city)
VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;
return mySqlCli.query(strFind, {
actId,
phone
}).then(ret => {
if (ret && ret[0].user_count <= 0) {
return mySqlCli.insert(strInsert, {
actId,
name,
birthday,
gender,
phone,
email,
province,
city
}).then(ret2 => {
return Promise.resolve({
code: 200,
message: '用户信息添加成功',
data: ret2
});
});
} else {
return Promise.resolve({
code: 203,
message: '已存在的用户信息'
});
}
});
}
}
module.exports = PrizeModel;
... ...
... ... @@ -7,11 +7,16 @@ const express = require('express');
const router = express.Router(); // eslint-disable-line
const qiniu = require('./controllers/qiniu');
const wechat = require('./controllers/wechat');
const prize = require('./controllers/prize');
// const excel = require('./controllers/excel');
router.get('/gettoken', qiniu.getToken);
router.get('/share/getSignPackage', wechat.getSignPackage);
router.get('/prize/checkPrize', prize.checkPrize);
router.post('/prize/getPrize', prize.getPrize);
router.post('/prize/updatePrize', prize.updatePrize);
router.post('/info/saveUserInfo', prize.saveUserInfo);
// router.get('/excel/xml', excel.excelToXml);
// router.get('/excel/sendMail', excel.sendMail);
... ...
/*
true
@author: qiuj <jun.qiu@yoho.cn>
@date: 2018-01-30 10:31:47
@is_get: 是否中奖 0:未中 1:中奖
*/
#注意:GO;分割执行块
#中奖用户信息表
CREATE TABLE IF NOT EXISTS act_prize_user_log (
`id` int(8) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`act_id` int(8) NOT NULL DEFAULT 0,
`user_id` int(8) NOT NULL DEFAULT 0,
`is_get` TINYINT NOT NULL DEFAULT 0,
`user_name` varchar(50) DEFAULT '',
`user_phone` varchar(50) DEFAULT '',
`user_address` varchar(50) DEFAULT '',
`user_shoes_size` float DEFAULT 0,
`user_info` varchar(200) DEFAULT '',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8;
GO;
#用户信息收集表
CREATE TABLE IF NOT EXISTS act_user_detail_info (
`id` int(8) NOT NULL DEFAULT 0,
`act_id` int(8) NOT NULL DEFAULT 0,
`user_name` varchar(50) DEFAULT '',
`user_birthday` timestamp DEFAULT CURRENT_TIMESTAMP,
`user_gender` TINYINT NOT NULL DEFAULT 0,
`user_phone` varchar(20) DEFAULT '',
`user_email` varchar(50) DEFAULT '',
`user_province` varchar(50) DEFAULT '',
`user_city` varchar(50) DEFAULT '',
`user_desc` varchar(200) DEFAULT '',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8;
GO;
... ...
module.exports = ['_migration', '20170913102356', '20170927092926', '20171025145423',
'20171102104558', '20171115144710'];
'20171102104558', '20171115144710', '20180131104311'];
... ...