Authored by 邱骏

球鞋抽奖及用户信息收集

  1 +/**
  2 + * 后台抽奖以及记录中奖用户信息
  3 + * Created by qiujun on 2018/1/30.
  4 + */
  5 +
  6 +const PrizeModel = require('../models/prize');
  7 +const moment = require('moment');
  8 +const INVALID_PARAMS = '参数错误';
  9 +
  10 +const prize = {
  11 + /**
  12 + * 获取用户中奖信息
  13 + * @param req
  14 + * @param res
  15 + * @param next
  16 + */
  17 + checkPrize(req, res, next) {
  18 + let query = req.query;
  19 + let actId = query.actId;
  20 + let userPhone = query.phone;
  21 +
  22 + if (!actId || !userPhone || userPhone.length !== 11) {
  23 + return res.json({
  24 + code: 400,
  25 + message: INVALID_PARAMS
  26 + });
  27 + }
  28 +
  29 + req.ctx(PrizeModel).checkPrize(actId, userPhone).then(ret => {
  30 + res.json(ret);
  31 + }).catch(next);
  32 + },
  33 +
  34 + /**
  35 + * 抽奖并先写入用户的手机号码防止重复抽奖
  36 + * @param req
  37 + * @param res
  38 + * @param next
  39 + * @returns {*|Promise.<T>}
  40 + */
  41 + getPrize(req, res, next) {
  42 + let query = req.body;
  43 + let actId = query.actId;
  44 + let z = query.z || 5; // 中奖概率设置,
  45 + let bz = query.bz || 95;
  46 + let userPhone = query.phone;
  47 +
  48 + console.log(actId, userPhone);
  49 +
  50 + let baseArr = [];
  51 + let prizeArr = [];
  52 +
  53 + if (!actId || !userPhone || userPhone.length !== 11) {
  54 + return res.json({
  55 + code: 400,
  56 + message: INVALID_PARAMS
  57 + });
  58 + }
  59 +
  60 + for (let i = 0; i < z; i++) {
  61 + baseArr.push(1);
  62 + }
  63 + for (let i = 0; i < bz; i++) {
  64 + prizeArr.push(0);
  65 + }
  66 +
  67 + baseArr.forEach(item => { // 随机往prizeArr中插入一个中奖数字1
  68 + let rand = Math.floor(Math.random() * prizeArr.length);
  69 +
  70 + prizeArr.splice(rand, 0, item);
  71 + });
  72 +
  73 +
  74 + let isGet = prizeArr[Math.floor(Math.random() * prizeArr.length)]; // 随机抽取一个,如果是1就中奖,0就不中
  75 +
  76 + console.log('isGet=' + isGet);
  77 + return req.ctx(PrizeModel).addPrize(actId, userPhone, isGet).then(ret => {
  78 + res.json(Object.assign(ret, {prize: isGet}));
  79 + }).catch(next);
  80 + },
  81 +
  82 + /**
  83 + * 中奖用户更新自己的用户信息
  84 + * @param req
  85 + * @param res
  86 + * @param next
  87 + */
  88 + updatePrize(req, res, next) {
  89 + let query = req.body;
  90 + let actId = query.actId;
  91 + let userPhone = query.phone;
  92 + let userAddress = decodeURIComponent(query.address).replace(/<.*?>/g, '');
  93 + let userName = decodeURIComponent(query.name);
  94 + let userSize = query.size;
  95 +
  96 + if (!actId || !userPhone || !userAddress || !userName || !userSize) {
  97 + return res.json({
  98 + code: 400,
  99 + message: INVALID_PARAMS
  100 + });
  101 + }
  102 +
  103 + return req.ctx(PrizeModel).updatePrize(actId, userPhone, userName, userAddress, userSize).then(ret => {
  104 + res.json(ret);
  105 + }).catch(next);
  106 + },
  107 +
  108 + /**
  109 + * 保存用户信息
  110 + * @param req
  111 + * @param res
  112 + * @param next
  113 + */
  114 + saveUserInfo(req, res, next) {
  115 + let query = req.body;
  116 + let actId = query.actId;
  117 + let name = query.name;
  118 + let birthday = query.birthday || moment().format('YYYY-MM-DD');
  119 + let gender = query.gender || 0;
  120 + let phone = query.phone;
  121 + let email = query.email;
  122 + let province = query.province;
  123 + let city = query.city;
  124 +
  125 + console.log(actId, name, birthday, gender, phone, email, province, city);
  126 +
  127 + if (!actId || !phone || !name || phone.length !== 11 || email.indexOf('@') < 0) {
  128 + return res.json({
  129 + code: 400,
  130 + message: INVALID_PARAMS
  131 + });
  132 + }
  133 +
  134 + return req.ctx(PrizeModel)
  135 + .saveUserInfo(actId, name, birthday, gender, phone, email, province, city)
  136 + .then(ret => {
  137 + res.json(ret);
  138 + }).catch(next);
  139 + }
  140 +};
  141 +
  142 +module.exports = prize;
  143 +
  1 +/**
  2 + * 抽奖model
  3 + * @author: qiujun <jun.qiu@yoho.cn>
  4 + * @date: 30/01/2018
  5 + */
  6 +
  7 +const mySqlCli = global.yoho.utils.mysqlCli;
  8 +const TABLE_PRIZE = 'act_prize_user_log';
  9 +const TABLE_USER_DETAIL = 'act_user_detail_info';
  10 +
  11 +class PrizeModel extends global.yoho.BaseModel {
  12 + constructor(ctx) {
  13 + super(ctx);
  14 + }
  15 +
  16 + /**
  17 + * 检查用户的获奖信息
  18 + * @param actId
  19 + * @param userPhone
  20 + */
  21 + checkPrize(actId, userPhone) {
  22 + let strSql = `SELECT id, user_phone, is_get, user_name, user_address, user_shoes_size FROM ${TABLE_PRIZE}
  23 + WHERE act_id = :actId
  24 + AND user_phone = :userPhone`;
  25 +
  26 + return mySqlCli.query(strSql, {
  27 + actId,
  28 + userPhone
  29 + }).then(ret => {
  30 + let result = {};
  31 +
  32 + console.log(ret);
  33 + if (ret.length <= 0) {
  34 + result = {
  35 + code: 201,
  36 + message: '未找到获奖信息'
  37 + };
  38 + } else if (ret.length > 0) {
  39 + result = {
  40 + code: 200,
  41 + data: ret
  42 + };
  43 + }
  44 +
  45 + return Promise.resolve(result);
  46 + });
  47 + }
  48 +
  49 + /**
  50 + * 检查并添加中奖者基础信息
  51 + * @param actId
  52 + * @param userPhone
  53 + * @param isGet
  54 + */
  55 + addPrize(actId, userPhone, isGet) {
  56 + let strFindSql = `SELECT COUNT(*) as user_prize_count FROM ${TABLE_PRIZE}
  57 + WHERE act_id = :actId
  58 + AND user_phone = :userPhone`;
  59 +
  60 + let strCountSql = `SELECT COUNT(*) AS total_prize_count FROM ${TABLE_PRIZE}
  61 + WHERE act_id = :actId AND is_get = 1`;
  62 +
  63 + let strInsertSql = `INSERT INTO ${TABLE_PRIZE} (act_id, user_phone, is_get)
  64 + VALUES(:actId, :userPhone, :isGet)`;
  65 +
  66 + return mySqlCli.query(strFindSql, {
  67 + actId,
  68 + userPhone
  69 + }).then(ret => {
  70 + console.log(ret);
  71 + if (ret[0] && ret[0].user_prize_count <= 0) {
  72 + return mySqlCli.query(strCountSql, {
  73 + actId
  74 + }).then(ret2 => {
  75 + console.log(ret2);
  76 + let result = {};
  77 +
  78 + if (ret2[0].total_prize_count >= 20) {
  79 + isGet = 0;
  80 + result = {
  81 + code: 204,
  82 + message: '奖品已领取完'
  83 + };
  84 + } else {
  85 + result = {
  86 + code: 200,
  87 + message: '抽奖信息添加成功!'
  88 + };
  89 + }
  90 +
  91 + return mySqlCli.insert(strInsertSql, {
  92 + actId,
  93 + userPhone,
  94 + isGet
  95 + }).then(ret3 => {
  96 + return Promise.resolve(Object.assign(result, {data: ret3}));
  97 + });
  98 + });
  99 + } else {
  100 + return Promise.resolve({
  101 + code: 203,
  102 + message: '请勿重复领奖'
  103 + });
  104 + }
  105 + });
  106 + }
  107 +
  108 + /**
  109 + * 更新中奖者的详细信息
  110 + * @param actId
  111 + * @param userPhone
  112 + * @param name
  113 + * @param address
  114 + * @param shoesSize
  115 + */
  116 + updatePrize(actId, userPhone, name, address, shoesSize) {
  117 + let strSql = `UPDATE ${TABLE_PRIZE} SET
  118 + user_name=:name,
  119 + user_address=:address,
  120 + user_shoes_size=:shoesSize
  121 + WHERE user_phone=:userPhone
  122 + AND is_get=1
  123 + AND act_id=:actId`;
  124 +
  125 + return mySqlCli.update(strSql, {
  126 + actId,
  127 + userPhone,
  128 + name,
  129 + address,
  130 + shoesSize
  131 + }).then(ret => {
  132 + return Promise.resolve({
  133 + code: 200,
  134 + data: ret
  135 + });
  136 + });
  137 + }
  138 +
  139 + saveUserInfo(actId, name, birthday, gender, phone, email, province, city) {
  140 + let strFind = `SELECT COUNT(*) AS user_count
  141 + FROM ${TABLE_USER_DETAIL}
  142 + WHERE act_id=:actId
  143 + AND user_phone=:phone`;
  144 +
  145 + let strInsert = `INSERT INTO ${TABLE_USER_DETAIL}
  146 + (act_id, user_name, user_birthday, user_gender,
  147 + user_phone, user_email, user_province, user_city)
  148 + VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;
  149 +
  150 + return mySqlCli.query(strFind, {
  151 + actId,
  152 + phone
  153 + }).then(ret => {
  154 + if (ret && ret[0].user_count <= 0) {
  155 + return mySqlCli.insert(strInsert, {
  156 + actId,
  157 + name,
  158 + birthday,
  159 + gender,
  160 + phone,
  161 + email,
  162 + province,
  163 + city
  164 + }).then(ret2 => {
  165 + return Promise.resolve({
  166 + code: 200,
  167 + message: '用户信息添加成功',
  168 + data: ret2
  169 + });
  170 + });
  171 + } else {
  172 + return Promise.resolve({
  173 + code: 203,
  174 + message: '已存在的用户信息'
  175 + });
  176 + }
  177 + });
  178 + }
  179 +}
  180 +
  181 +module.exports = PrizeModel;
@@ -7,11 +7,16 @@ const express = require('express'); @@ -7,11 +7,16 @@ const express = require('express');
7 const router = express.Router(); // eslint-disable-line 7 const router = express.Router(); // eslint-disable-line
8 const qiniu = require('./controllers/qiniu'); 8 const qiniu = require('./controllers/qiniu');
9 const wechat = require('./controllers/wechat'); 9 const wechat = require('./controllers/wechat');
  10 +const prize = require('./controllers/prize');
10 11
11 // const excel = require('./controllers/excel'); 12 // const excel = require('./controllers/excel');
12 13
13 router.get('/gettoken', qiniu.getToken); 14 router.get('/gettoken', qiniu.getToken);
14 router.get('/share/getSignPackage', wechat.getSignPackage); 15 router.get('/share/getSignPackage', wechat.getSignPackage);
  16 +router.get('/prize/checkPrize', prize.checkPrize);
  17 +router.post('/prize/getPrize', prize.getPrize);
  18 +router.post('/prize/updatePrize', prize.updatePrize);
  19 +router.post('/info/saveUserInfo', prize.saveUserInfo);
15 20
16 // router.get('/excel/xml', excel.excelToXml); 21 // router.get('/excel/xml', excel.excelToXml);
17 // router.get('/excel/sendMail', excel.sendMail); 22 // router.get('/excel/sendMail', excel.sendMail);
  1 +/*
  2 + true
  3 + @author: qiuj <jun.qiu@yoho.cn>
  4 + @date: 2018-01-30 10:31:47
  5 + @is_get: 是否中奖 0:未中 1:中奖
  6 +*/
  7 +
  8 +#注意:GO;分割执行块
  9 +#中奖用户信息表
  10 +CREATE TABLE IF NOT EXISTS act_prize_user_log (
  11 + `id` int(8) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  12 + `act_id` int(8) NOT NULL DEFAULT 0,
  13 + `user_id` int(8) NOT NULL DEFAULT 0,
  14 + `is_get` TINYINT NOT NULL DEFAULT 0,
  15 + `user_name` varchar(50) DEFAULT '',
  16 + `user_phone` varchar(50) DEFAULT '',
  17 + `user_address` varchar(50) DEFAULT '',
  18 + `user_shoes_size` float DEFAULT 0,
  19 + `user_info` varchar(200) DEFAULT '',
  20 + `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  21 +) DEFAULT CHARSET=utf8;
  22 +GO;
  23 +
  24 +#用户信息收集表
  25 +CREATE TABLE IF NOT EXISTS act_user_detail_info (
  26 + `id` int(8) NOT NULL DEFAULT 0,
  27 + `act_id` int(8) NOT NULL DEFAULT 0,
  28 + `user_name` varchar(50) DEFAULT '',
  29 + `user_birthday` timestamp DEFAULT CURRENT_TIMESTAMP,
  30 + `user_gender` TINYINT NOT NULL DEFAULT 0,
  31 + `user_phone` varchar(20) DEFAULT '',
  32 + `user_email` varchar(50) DEFAULT '',
  33 + `user_province` varchar(50) DEFAULT '',
  34 + `user_city` varchar(50) DEFAULT '',
  35 + `user_desc` varchar(200) DEFAULT '',
  36 + `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  37 +) DEFAULT CHARSET=utf8;
  38 +GO;
1 module.exports = ['_migration', '20170913102356', '20170927092926', '20171025145423', 1 module.exports = ['_migration', '20170913102356', '20170927092926', '20171025145423',
2 - '20171102104558', '20171115144710']; 2 + '20171102104558', '20171115144710', '20180131104311'];