/** * 我的优惠券 controller * @author: weiqingting<qingting.wei@yoho.cn> * @date: 2016/05/16 */ 'use strict'; const Promise = require('bluebird'); const co = Promise.coroutine; const favoriteModel = require('../models/favorite-model'); 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; let favoriteService = req.ctx(favoriteModel); 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; let favoriteService = req.ctx(favoriteModel); 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; let favoriteService = req.ctx(favoriteModel); 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; let favoriteService = req.ctx(favoriteModel); 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; let favoriteService = req.ctx(favoriteModel); 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'; let group = req.query.group || {}; let favoriteService = req.ctx(favoriteModel); if (!uid || !id) { return res.json({ code: 400, message: '缺少参数' }); } favoriteService.cancelAsync(uid, id, group, type).then((result) => { return res.json(result); }).catch(next); }; module.exports = { index, newProduct, reduction, notice, cancelNotice, cancel };