favorite.js 3.31 KB
/**
 * 我的优惠券 controller
 * @author: weiqingting<qingting.wei@yoho.cn>
 * @date: 2016/05/16
 */
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;

const favoriteService = require('../models/favorite-service');

const TABS = {
    product: 'product',
    brand: 'brand',
    article: 'article'
};

const index = (req, res, next)=> {
    let uid = req.user.uid;
    let udid = req.yoho.udid;
    let page = +req.query.page || 1;
    let type = req.query.type || TABS.product;
    let selectedSort = +req.query.sort_id || 0;
    let reduction = req.query.is_reduction || 'N';
    let promotion = req.query.is_promotion || 'N';
    let limit = +req.query.limit || 10;

    co(function*() {
        let data = {};

        data.tabs = favoriteService.getFavoriteTabs(type);

        switch (type) {
            case TABS.brand:
                data.favBrands = yield favoriteService.favoriteBrandListAsync(uid, page, limit, type);
                break;
            case TABS.article:
                data.favArticles = yield favoriteService.favoriteArticleListAsync(uid, udid, page, limit, type);
                break;
            default:
                data.favProducts = yield favoriteService.favoriteProductListAsync(
                    uid, page, limit, selectedSort, 'N', reduction, promotion, req.query
                );
                break;
        }

        return data;
    })().then((result) => {
        return res.render('favorite', {
            meFavoritePage: true,
            meFavorite: result
        });
    }).catch(next);
};

const newProduct = (req, res, next) => {
    let uid = req.user.uid;
    let page = +req.query.page || 1;
    let limit = +req.query.limit || 10;
    let id = +req.query.id || 0;

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

    favoriteService.newProductAsync(uid, page, limit, id).then((result) => {
        return res.json({
            code: 200,
            data: result
        });
    }).catch(next);
};

const reduction = (req, res, next) => {
    let uid = req.user.uid;
    let page = +req.query.page || 1;
    let type = req.query.type || '';
    let limit = 10;

    favoriteService.reductionAsync(uid, page, limit, type, 0, 'Y').then((result) => {
        return res.render('home/favorite/reduction', result);
    }).catch(next);
};

const notice = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.query.id;
    let mobile = req.query.mobile;

    favoriteService.enableNoticeAsync(uid, mobile, id).then((result) => {
        return res.json(result);
    }).catch(next);
};

const cancelNotice = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.query.id;

    favoriteService.disableNoticeAsync(uid, id).then((result) => {
        return res.json(result);
    }).catch(next);
};

const cancel = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.query.id;
    let type = req.query.type || 'product';

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

    favoriteService.cancelAsync(uid, id, type).then((result) => {
        return res.json(result);
    }).catch(next);
};

module.exports = {
    index,
    newProduct,
    reduction,

    notice,
    cancelNotice,
    cancel
};