wheel-surf.js 7.38 KB
/**
 * 大转盘活动controller
 * @author: leo <qi.li@yoho.cn>
 * @date: 26/09/2018
 */
const wheelSurfModel = require('../models/wheel-surf');
const valid = require('../../../utils/validator');

const whSurfController = {
    entry(req, res) {
        if (req.xhr) {
            return res.json({
                code: 404,
                message: 'invalid url'
            });
        }
        res.render('activity/wheel-surf', {
            bodyClass: 'nav-md',
            module: 'admin',
            page: 'wheel-surf'
        });
    },
    async actCreate(req, res) {
        const title = req.body.title;
        const startTime = req.body.startTime;
        const endTime = req.body.endTime;
        const type = req.body.type || 0;

        if (!title || !startTime || !endTime) {
            return res.json({
                code: 400,
                message: '参数缺失'
            });
        }

        const params = {
            title,
            startTime,
            endTime,
            type
        };

        try {
            const result = await req.ctx(wheelSurfModel).create(params);

            return res.json({ code: 200, data: result});
        } catch (err) {
            res.json(err);
        }

    },
    async actDelete(req, res) {
        const id = req.body.id;

        try {
            const result = await req.ctx(wheelSurfModel).actDelete(id);

            return res.json({ code: 200, data: result});

        } catch (err) {
            res.json(err);
        }

    },
    async actList(req, res) {
        try {
            const result = await req.ctx(wheelSurfModel).list();

            return res.json({
                code: 200,
                data: result
            });
        } catch (err) {
            res.json(err);
        }
    },

    getConfigList: async function(req, res) {
        let result = await req.ctx(wheelSurfModel).configList();

        return res.json({
            code: 200,
            data: result
        });
    },

    configModify: async function(req, res) {
        try {
            let data = valid(req.body, {
                id: {type: 'number', empty: true},
                act_id: {type: 'number', empty: false},
                rule_btn_bg: {type: 'string', empty: true},
                rule: {type: 'string', empty: true},
                share_btn_bg: {type: 'string', empty: true},
                share_url: {type: 'string', empty: true},
                main_bg: {type: 'string', empty: true},
                wheel_bg: {type: 'string', empty: true},
                prize_btn_bg: {type: 'string', empty: true},
                day_limit_times: {type: 'number', empty: true},
                act_total_times: {type: 'number', empty: true},
                day_over_limit_bg: {type: 'string', empty: true},
                btm_tip_one: {type: 'string', empty: true},
                btm_tip_two: {type: 'string', empty: true},
                my_prize_btn: {type: 'string', empty: true},
                jump_url: {type: 'string', empty: true},
                win_prize_bg: {type: 'string', empty: true},
                try_again_bg: {type: 'string', empty: true},
                status: {type: 'number', empty: true}
            });
            let result = await req.ctx(wheelSurfModel).configModify(data);

            return res.json({
                code: 200,
                data: result
            });
        } catch (e) {
            return res.json({
                code: 203,
                result: false,
                msg: e
            });
        }
    },

    configFindOne: async function(req, res) {
        let actId = req.query.act_id;

        if (!actId) {
            return res.json({
                code: 202,
                result: false,
                msg: '参数为空'
            });
        }
        let result = await req.ctx(wheelSurfModel).configFindOne(actId);

        return res.json({
            code: 200,
            data: result
        });
    },

    prizeCreate: async function(req, res) {
        let param = req.body && req.body.prizes;

        try {
            if (param && param.length && param instanceof Array) {
                let arr = param.map((value) => {
                    return valid(value, {
                        act_id: {type: 'number', empty: false},
                        name: {type: 'string', empty: true},
                        type: {type: 'number', empty: true},
                        value: {type: 'string', empty: true},
                        img: {type: 'string', empty: true},
                        total: {type: 'number', empty: true},
                        total_left: {type: 'number', empty: true},
                        chance: {type: 'number', empty: true},
                        prize_bg: {type: 'string', empty: true},
                        prize_idx: {type: 'number', empty: true},
                    });
                });
                let result = await req.ctx(wheelSurfModel).prizeCreate(arr);

                return res.json({
                    code: 200,
                    data: result
                });
            } else {
                return res.json({
                    code: 202,
                    result: false,
                    msg: '参数格式错误'
                });
            }
        } catch (e) {
            return res.json({
                code: 203,
                result: false,
                msg: e
            });
        }
    },

    prizeUpdate: async function(req, res) {
        let param = req.body && req.body.prizes;

        try {
            if (param && param.length && param instanceof Array) {
                let arr = param.map((value) => {
                    return valid(value, {
                        id: {type: 'number', empty: false},
                        act_id: {type: 'number', empty: false},
                        name: {type: 'string', empty: true},
                        type: {type: 'number', empty: true},
                        value: {type: 'string', empty: true},
                        img: {type: 'string', empty: true},
                        total: {type: 'number', empty: true},
                        total_left: {type: 'number', empty: true},
                        chance: {type: 'number', empty: true},
                        prize_bg: {type: 'string', empty: true},
                        prize_idx: {type: 'number', empty: true},
                    });
                });
                let result = await req.ctx(wheelSurfModel).prizeUpdate(arr);

                return res.json({
                    code: 200,
                    data: result
                });
            } else {
                return res.json({
                    code: 202,
                    result: false,
                    msg: '参数格式错误'
                });
            }
        } catch (e) {
            return res.json({
                code: 203,
                result: false,
                msg: e
            });
        }
    },

    prizeFindByActId: async function(req, res) {
        let actId = req.query.act_id;

        if (!actId) {
            return res.json({
                code: 202,
                result: false,
                msg: '参数为空'
            });
        }
        let result = await req.ctx(wheelSurfModel).prizeFindByActId(actId);

        return res.json({
            code: 200,
            data: result
        });
    }
};

module.exports = whSurfController;