message.js 2.44 KB
// 消息 by acgpiano
'use strict';
const headerModel = require('../../../doraemon/models/header'), // 头部model
    model = require('../models/message');

const _getList = (req, res, next) => {
    req.ctx(model).getList({
        page: req.body.page,
        size: 20,
        uid: req.user.uid,
    }).then(result => {
        let options = {
            module: 'home',
            page: 'message',
            pageHeader: headerModel.setNav({
                navTitle: '我的消息'
            }),
            title: '我的消息',
            pageFooter: true,
            list: result,
        };

        req.body.page && req.body.page !== '1' && (options.layout = false);
        res.render('message', options);
    }).catch(next);
};

const _msgDetail = (req, res, next) => {
    req.ctx(model).getList({
        page: req.body.page,
        size: 1000,
        msgid: req.query.id,
        uid: req.user.uid,
    }).then(result => {
        let options = {
            module: 'home',
            page: 'message',
            pageHeader: headerModel.setNav({
                navTitle: '我的消息'
            }),
            title: '我的消息',
            pageFooter: true,
            info: result,
        };

        if (result && result.isCollar && result.isCollar === true) {
            res.redirect('/home/message');
        } else {
            res.render('message-detail', options);
        }

    }).catch(next);
};

exports.index = (req, res, next) => {
    if (!req.query.id) {
        _getList(req, res, next);
    } else {
        _msgDetail(req, res, next);
    }
};

exports.ajaxDelMes = (req, res, next) => {
    req.ctx(model).delMsg({
        uid: req.user.uid,
        msgid: req.body.id,
    }).then(result => {
        res.send(result);
        res.end();
    }).catch(next);
};

exports.pickCoupon = (req, res, next) => {
    req.ctx(model).pickCoupon({
        uid: req.user.uid,
        couponId: req.body.id,
    }).then(result => {
        res.send(result);
        res.end();
    }).catch(next);
};

// 生日券
exports.birthCoupon = (req, res, next) => {
    let responseData = {
        module: 'home',
        page: 'message',
        pageHeader: headerModel.setNav({
            navTitle: '生日券'
        }),
        title: '生日券',
        pageFooter: true
    };

    req.ctx(model)._getBirthCouponById(req.user.uid).then(result => {
        res.render('birth-coupon', Object.assign(responseData, result));
    }).catch(next);
};