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

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);
    }
  },

  configModify: async function(req, res) {

    try {
      let data = valid(req.body, {
        id: {type: 'number', empty: true},
        act_id: {type: 'string', empty: false},
        rule_btn_bg: {type: 'string', empty: true},
        rule_url: {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_bg: {type: 'string', empty: true},
        jump_btn_left_url: {type: 'string', empty: true},
        jump_btn_middle_url: {type: 'string', empty: true},
        jump_btn_right_url: {type: 'string', empty: true},
        jump_btn_left_bg: {type: 'string', empty: true},
        jump_btn_middle_bg: {type: 'string', empty: true},
        jump_btn_right_bg: {type: 'string', empty: true},
        bottomContentCode: {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).setActConf(data.act_id, 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 actInfo = await req.ctx(wheelSurfModel).actInfo(actId);
    let result = await req.ctx(wheelSurfModel).getActConf(actId);

    let actStatus = 2; // 1: 活动未开始 2: 活动进行中 3:活动已过期
    let now = parseInt(new Date().getTime() / 1000, 10);

    if (!actInfo) {
      return res.json({
        code: 404,
        message: '活动不存在'
      });
    }

    if (now < actInfo.startTime) {
        actStatus = 1;
    } else if (now > actInfo.endTime) {
      actStatus = 3;
    }

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

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

    try {
      if (param && param.length && param instanceof Array) {
        let arr = param.map((value) => {
          return valid(value, {
            act_id: {type: 'string', 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).createActPrize(arr[0].act_id, 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;

    try {
      if (param && param.length && param instanceof Array) {
        let arr = param.map((value) => {
          return valid(value, {
            id: {type: 'number', empty: true},
            act_id: {type: 'string', 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).updateActPrize(arr[0].act_id, 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).getActPrize(actId);

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

  userFind: async function(req, res) {
    let obj = req.body;
    let result = await req.ctx(wheelSurfModel).userFind(obj);

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

  exportRecords: async function(req, res) {
    let result = await req.ctx(wheelSurfModel).exportRecords(req.query.actId);

    let excelConf = {
      name: 'mysheet',
      cols: ['用户UID', '奖品类型', '奖品名称', '中奖时间'],
      rows: []
    };

    let temp = [];

    result.forEach(item => {
      temp = [];
      temp.push(item.uid);
      temp.push(item.type);
      temp.push(item.name);
      temp.push(moment(+item.time).format('YYYY-MM-DD HH:mm:ss'));
      excelConf.rows.push(temp);
    });

    return csvExport(res, {
      cols: excelConf.cols,
      rows: excelConf.rows,
      fileName: `ActivityUserList_${new Date().getTime()}`
    });

  }
};

module.exports = whSurfController;