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

const dailyCheckInController = {
  /**
   * 普通签到
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async checkInNormal(req, res) {
    let {actId, uid, sessionKey, appVersion, appSessionType} = req.body;

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


    if (!parseInt(uid, 10) || !sessionKey || !appVersion || !appSessionType) {
      return res.json({
        code: 401,
        message: '未登录'
      });
    }

    let params = {
      actId,
      uid: parseInt(uid, 10),
      sessionKey,
      appVersion,
      appSessionType
    };

    try {
      let result = await req.ctx(dailyCheckInModel).checkInNormal(params);

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

  async checkInCumulative(req, res) {
    let {actId, uid, sessionKey, appVersion, appSessionType} = req.body;

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

    if (!parseInt(uid, 10) || !sessionKey || !appVersion || !appSessionType) {
      return res.json({
        code: 401,
        message: '未登录'
      });
    }

    let params = {
      actId,
      uid: parseInt(uid, 10),
      sessionKey,
      appVersion,
      appSessionType
    };

    try {
      let result = await req.ctx(dailyCheckInModel).checkInCumulative(params);

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

  /**
   * 获取用户签到累计信息
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async getUserCumulativeData(req, res) {
    let {actId, uid} = req.query;

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

    let params = {
      actId,
      uid: parseInt(uid, 10)
    };

    try {
      req.ctx(dailyCheckInModel).getUserCumulativeData(params).then(result => {
        if (result.code === 200) {
          let cumDaysResult = [];
          let cumDaysArr = [];
          let cumPrizeResult = result.cumPrize;

          result.cumDays.map(ret => {
            if (ret.createTime) {
              cumDaysArr.push(ret.createTime);// 把签到时间拿出来,用于签到日期列表
              cumDaysResult.push({ // 每次签到获得的奖品
                prizeId: ret.prizeId,
                prizeImg: ret.prizeImg,
                prizeDesc: ret.prizeDesc,
                prizeType: ret.prizeType,
                createTime: ret.createTime
              });
            }
          });


          return res.json({
            code: 200,
            actId: actId,
            uid: uid,
            data: {
              signDays: cumDaysArr,
              cumDays: cumDaysResult,
              cumPrize: cumPrizeResult
            }
          });
        } else {
          return res.json(result);
        }
      });
    } catch (err) {
      return res.json(err);
    }

  },

  /**
   * 保存用户信息
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async saveUserInfo(req, res) {
    let {uid, actId, phone} = req.body;
    let regExp = /^1[34578]\d{9}$/;

    if (!actId || !uid || !phone || phone.toString().length !== 11) {
      return res.json({
        code: 400,
        message: '缺少参数'
      });
    }

    if (!regExp.test(phone)) {
      return res.json({
        code: 400,
        message: '错误的手机号码格式'
      });
    }


    let params = {
      actId,
      uid,
      phone
    };

    try {
      let result = await req.ctx(dailyCheckInModel).saveUserInfo(params);

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

  /**
   * 获取用户是否填写过手机号的信息
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async getUserInfo(req, res) {
    let {uid, actId} = req.query;

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

    try {
      let result = await req.ctx(dailyCheckInModel).getUserInfo({actId, uid});

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

  /**
   * 获取所有实物中奖人员列表
   * @param req
   * @param res
   * @returns {Promise.<void>}
   */
  async getWinnerList(req, res) {
    let {actId, showPhone} = req.query;

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

    try {
      let result = await req.ctx(dailyCheckInModel).getWinnerList({actId: actId});

      if (!showPhone) {
        result.map(ret => {
          delete ret.fid;
        });
      }

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

module.exports = dailyCheckInController;