detail.js 4.06 KB
/**
 *
 * @author: Aiden Xu<aiden.xu@yoho.cn>
 * @date: 2016/07/19
 */
'use strict';

// const _ = require('lodash');

// const helpers = global.yoho.helpers;
const api = global.yoho.API;
const _ = require('lodash');
const model = require('../models/detail');
const searchModel = require('../models/search');

/**
 * 商品详情
 */
const component = {
    index(req, res) {
        const pid = req.params[0],
            goodsId = req.params[1],
            cnAlphabet = req.params[2];

        res.render('detail', {
            module: 'product',
            page: 'detail',
            pid: pid,
            goodsId: goodsId,
            cnAlphabet: cnAlphabet
        });
    },
    product(req, res, next) {
        const pid = req.params[0]; // , goodsId = req.params[1];

        let params = {
            product_id: _.toString(pid),
            uid: req.user.uid || 0
        };

        model.product(params).then(result => {
            res.json(result);
        }).catch(next);
    },
    intro(req, res, next) {
        let params = {
            productskn: req.query.skn,
            udid: 'f528764d624db129b32c21fbca0cb8d6'
        };

        model.intro(params).then(result => {
            res.json(result);
        }).catch(next);
    },
    brand(req, res, next) {
        const brandId = req.params[0];

        model.brand({
            uid: req.user.uid || 0,
            brand_id: brandId,
            gender: req.query.gender || '1,2,3'
        }).then(result => {
            res.json(result);
        }).catch(next);
    },

    /**
     * 加入购物车接口
     *
     */
    addToCart(req, res, next) {
        let params = {
            product_sku: req.body.productSku, // 商品SKU
            buy_number: req.body.buyNumber, // 购买数量
            goods_type: req.body.goodsType || 0, // 商品类型,0表示普通商品,1表示加价购商品
            edit_product_sku: req.body.isEdit || 0, // 是否是编辑商品SKU,0表示不是编辑
            selected: 'Y',
            promotion_id: req.body.promotionId || null, // 促销id,默认null(加价购有关)
            uid: req.user.uid || 0,
            shopping_key: global.yoho.cookie.getShoppingKey(req)
        };

        model.addToCart(params).then(result => {
            if (result.code === 200) {
                // 将 shopping_key 写入Cookie
                res.cookie('_SPK', result.data.shopping_key, {
                    maxAge: 86400 * 360
                });
            }
            res.json(result);
        }).catch(next);
    },

    /**
     * 添加收藏/取消收藏
     *
     * @param req
     * @param res
     * @param next
     */
    favorite(req, res, next) {
        let params = {
            uid: req.user.uid || 0,
            type: 'product'
        };

        if (req.body.operation === 'add') {
            params.id = req.body.id;
            params.method = 'app.favorite.add';
        } else if (req.body.operation === 'remove') {
            params.fav_id = req.body.id;
            params.method = 'app.favorite.cancel';
        }

        if (!req.user.uid) {
            // 未登陆
            res.json({
                code: 403
            });

            return;
        }

        model.favorite(params).then(result => {
            res.json(result);
        }).catch(next);
    },

    /**
     * 获取购物车数量
     *
     * @param req
     * @param res
     * @param next
     */
    getCartCount: (req, res, next) => {
        let params = {
            method: 'app.Shopping.count',
            shopping_key: global.yoho.cookie.getShoppingKey(req),
            uid: req.user.uid || 0
        };

        api.get('', params).then(result => {
            res.json(result);
        }).catch(next);
    },

    /**
     * 搜索产品
     *
     * @param req
     * @param res
     * @param next
     */
    search: (req, res, next) => {
        let params = {
            query: req.query.ids,
            limit: 4
        };

        searchModel.products(params).then(result => {
            res.json(result);
        }).catch(next);
    }
};

module.exports = component;