brand.js 4.35 KB
/**
 * 品牌页
 * <jing.li@yoho.cn>
 * 2016/09/19
 */

'use strict';

const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab
const indexModel = require('../models/brand');
const typeLib = require('../../../config/type-lib');
const qs = require('querystring');

/**
 * 从 path 获取 channel
 */
const _getChannelFromPath = (req) => {
    let pathWithoutParams = _.trimEnd(_.first(_.split(req.path, '?')), '/');
    let pathLast = _.last(_.split(pathWithoutParams, '/'));
    let pathChannel = _.first(_.split(pathLast, '-'));

    return pathChannel;
};

/**
 * 品牌一览
 *
 * @param string gender 老版本中使用的参数, 做兼容判断
 * @param int channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
 */
exports.index = (req, res, next) => {
    let responseData = {
        module: 'channel',
        page: 'brand',
        localCss: true,

        // title: '品牌一览 | Yoho!Buy有货 | 潮流购物逛不停',
        showFooterTab: footerModel.getUrlData('category')
    };
    let channel;

    if (!req.query.channel) {
        req.query.channel = _getChannelFromPath(req);
    }

    // 唤起 APP 的路径
    res.locals.appPath = 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.attention","params":{"actiontype":"1"}}';

    if (!req.query.channel) {
        channel = '1';
    } else if (!typeLib.channels[req.query.channel]) {
        channel = req.query.channel;
    } else {
        channel = typeLib.channels[req.query.channel] + '';
    }

    let param = {
        channel
    };

    req.ctx(indexModel).getBrandByChannel(param.channel).then(result => {
        res.render('brand/index', Object.assign(responseData, result, {
            params: {
                yhChannel: channel
            }
        }));
    }).catch(next);
};

// 301到新路由
exports.indexRedirect = (req, res) => {
    let param = qs.stringify(req.query);

    if (param) {
        param = '?' + param;
    }
    res.redirect(301, `/${req.yoho.channel}-brands/${param}`);
};

exports.brandList = (req, res, next) => {
    var channel = req.query.channel || '1';

    req.ctx(indexModel).getBrandListByChannel(channel).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 品牌一览搜索页
 *
 * @param string gender 老版本中使用的参数, 做兼容判断
 * @param int channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
 */
exports.search = (req, res, next) => {
    let headerData = headerModel.setNav({
        navTitle: '品牌一览'
    });

    let responseData = {
        pageHeader: headerData,
        module: 'channel',
        page: 'brand',
        localCss: true,
        isWechat: req.yoho.isWechat,
        title: '品牌一览 | Yoho!Buy有货 | 潮流购物逛不停',
        pageFooter: true
    };

    let param = {
        uid: req.user.uid,
        channel: req.query.channel || '1'
    };

    req.ctx(indexModel).branchSearch(param).then(result => {
        res.render('brand/search', Object.assign(responseData, result));
    }).catch(next);

};

/**
 * 添加品牌搜索记录
 */
exports.addBrandSearch = (req, res, next) => {

    let uid = req.user.uid;

    let brandName = req.body.brandName;

    let timestamp = Date.parse(new Date());

    timestamp = timestamp / 1000;

    let records = timestamp + '_' + brandName;

    let param = {
        uid,
        records
    };

    req.ctx(indexModel).addSearchHistory(param).then((result) => {
        res.json(result);
    }).catch(next);
};

/**
 * 删除品牌搜索记录
 */
exports.delBrandHistory = (req, res, next) => {

    let param = {

        uid: req.user.uid,

    };

    req.ctx(indexModel).delBrandSearchHistory(param).then((result) => {
        res.json(result);
    }).catch(next);
};

/**
 * [品牌搜索异步数据]
 */
exports.searchAsync = (req, res, next) => {

    let uid = req.user.uid;

    if (!req.xhr) {
        return next();
    }

    if (!uid) {
        return res.json({
            code: 200,
            data: {}
        });
    }

    return req.ctx(indexModel).branchSearchHistoryAsync(uid).then((result) => {
        return res.json({
            code: 200,
            data: {
                history: result
            }
        });
    });
};