daily-check-in.js 2.59 KB
/**
 * 签到活动controller
 * Created by qiujun on 2019/5/7.
 */
const moment = require('moment');
const dailyCheckInModel = require('../models/daily-check-in');

const dailyCheckInController = {
  entry(req, res) {
    if (req.xhr) {
      return res.json({
        code: 404,
        message: 'invalid url'
      });
    }

    res.render('activity/daily-check-in', {
      bodyClass: 'nav-md',
      module: 'admin',
      page: 'daily-check-in'
    });
  },

  /**
   * 创建活动
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async actCreate(req, res) {

    const title = req.body.title;
    const startTime = req.body.startTime;
    const endTime = req.body.endTime;
    const normalWheelId = req.body.normalWheelId || '';
    let prizeDayArr = req.body.prizeDayArr ? JSON.parse(req.body.prizeDayArr) : [];

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


    const params = {
      title,
      startTime,
      endTime,
      normalWheelId,
      prizeDayArr
    };

    try {
      const result = await req.ctx(dailyCheckInModel).createActivity(params);

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

  /**
   * 获取活动列表获取通过id获取单个活动信息
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async actList(req, res) {
    try {
      let result = await req.ctx(dailyCheckInModel).activityList();

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

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

  /**
   * 通过id获取单个活动信息
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async actInfo(req, res) {
    let actId = req.query.id || 0;

    if (!actId) {
      return res.json({
        code: 400,
        message: '缺少参数'
      });
    }

    let result = await req.ctx(dailyCheckInModel).actInfo(actId);

    if (result.code !== 201) {
      return res.json({
        code: 200,
        data: result
      });
    } else {
      return res.json(result);
    }
  },

  /**
   * 通过id删除一个活动
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async actDelete(req, res) {
    const id = req.body.id;

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

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

};

module.exports = dailyCheckInController;