prize.js 3.88 KB
/**
 * 后台抽奖以及记录中奖用户信息
 * 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;