yoho-activitys.js 3.86 KB
/**
 * 一些普通的活动
 * Created by qiujun on 2019/5/16.
 */
const moment = require('moment');
const actYohoActivitysModel = require('../models/yoho-activitys');

const actYohoActivitysController = {
  /**
   * 创建活动
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async actCreate(req, res) {
    const title = req.query.title;
    const desc = req.query.desc || '';
    const voteTimes = req.query.vote_times || 999;
    const repeatVote = req.query.repeat || 1;
    const startTime = req.query.startTime || (new Date()).getTime();
    const endTime = req.query.endTime || (new Date()).getTime() + 1000 * 60 * 60 * 24 * 30;

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

    const params = {
      title,
      startTime,
      endTime,
      desc,
      voteTimes,
      repeatVote
    };

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

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

  /**
   * 取得活动列表或者单个活动信息
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async getActInfo(req, res) {
    let id = req.query.act_id || 0;

    try {
      const result = await req.ctx(actYohoActivitysModel).actList(id);

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

  /**
   * 创建文章
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async createArticle(req, res) {
    let actId = req.query.actId;
    let itemLength = req.query.itemLength || 0; // 一次创建多个数量的文章

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

    try {
      const result = await req.ctx(actYohoActivitysModel).createArticles({actId, itemLength});

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

  /**
   * 获取文章列表
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async getArticleList(req, res) {
    let actId = req.query.actId;
    let order = req.query.order || 'desc';
    let start = req.query.limit_start || 0;
    let end = req.query.limit_end || -1;

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

    try {
      const result = await req.ctx(actYohoActivitysModel).getArtilceList({actId, order, start, end});

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

  /**
   * 投票点赞
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async addLike(req, res) {
    let actId = req.body.actId;
    let ids = req.body.ids;



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

    try {
      const result = await req.ctx(actYohoActivitysModel).addLike({actId, ids});

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

  /**
   * 移除给定排名区间内的数据
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async removeRangeByRand(req, res) {
    let actId = req.query.actId;
    let start = parseInt(req.query.start, 10) || 0;
    let end = parseInt(req.query.end, 10) || 0;

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

    try {
      const result = await req.ctx(actYohoActivitysModel).removeRangeByRand({actId, start, end});

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

module.exports = actYohoActivitysController;