index.js 8.75 KB
/**
 * 编辑页
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2016/09/05
 */
'use strict';

const _ = require('lodash');
const mRoot = '../models';
const typeLib = require('../../../config/type-lib');
const IndexModel = require(`${mRoot}/index`);
const headerModel = require('../../../doraemon/models/header'); // 头部model
const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab
const guangProcess = require(`${global.utils}/guang-process`);
const stringProcess = require(`${global.utils}/string-process`);
const Promise = require('bluebird');
const qs = require('querystring');
const channels = {
    boys: 1,
    girl: 2,
    kids: 3,
    lifestyle: 4
};

/**
 * [编辑页面]
 */
const editor = (req, res, next) => {
    let uid = req.user.uid || req.query.uid,
        udid = req.sessionID,
        id = req.query.id || req.params[0] || 0,
        title = '编辑简介',
        parameter = {},
        isApp = req.yoho.isApp,
        gender = req.query.gender ||
            req.query.channel && typeLib.channels[req.query.channel] ||
            req.cookies._Channel && channels[req.cookies._Channel] ||
            1;

    if (!isApp) {
        parameter = {
            pageHeader: headerModel.setNav({
                navTitle: title
            })
        };
    }
    return Promise.all([
        req.ctx(IndexModel).getAuthor(id),
        req.ctx(IndexModel).getArticleList(gender, 0, uid, udid, 1, null, id)]
    ).then(datas => {
        let authorData = datas[0],
            articleListData = datas[1];
        let build = [];
        let name = authorData.data ? authorData.data.name : '';
        let total = articleListData.data ? articleListData.data.total : 0;

        if (articleListData.data && articleListData.data.list && articleListData.data.list.artList) {
            articleListData.data.list.artList.forEach(articleData => {
                articleData.colparam = {
                    urlpath: req.path,
                    param: `?id=${id}`
                };

                build.push(guangProcess.formatArticle(articleData, true, isApp, false, uid));
            });

            if (!build.length) {
                res.set('Cache-Control', 'no-cache');
            }

            res.render('index/list', Object.assign({
                page: 'index-editor',
                title: `潮流编辑${name}|YOHO!BUY有货`,
                keywords: `潮流编辑${name}`,
                description: `YOHO!BUY有货潮流编辑${name}!`,
                guangList: true,
                gender: gender,
                guang: {
                    infos: build,
                    isApp: isApp,
                    authorInfo: authorData.data,
                    total: total
                },
                localCss: true
            }, parameter));
        } else {
            return next();
        }
    }).catch(next);

};

// 301到新路由
const editorRedirect = (req, res, next) => {
    let id = req.query.id;

    if (id) {
        let redirectUrl = '/guang/author';

        if (req.yoho.channel !== req.cookies._Channel) {
            redirectUrl += `-${req.yoho.channel}`;
        }
        delete req.query.id;
        let param = qs.stringify(req.query);

        if (param) {
            param = '?' + param;
        }
        redirectUrl += `-${id}/${param}`;
        res.redirect(301, redirectUrl);
    } else {
        return next();
    }
};

/**
 * [逛列表页面的资讯分页]
 * @param  {[type]}   req  [description]
 * @param  {[type]}   res  [description]
 * @param  {Function} next [description]
 * @return {[type]}        [description]
 */
const pageData = (req, res, next) => {
    /* 判断是不是AJAX请求 */
    if (!req.xhr) {
        res.json({
            code: 400,
            message: '非法请求',
            data: ''
        });
        return;
    }

    /* 判断参数是否有效 */
    let tag = req.query.tag,
        sortId = req.query.type || 0,
        page = req.query.page,
        gender = req.query.gender,
        authorId = req.query.authorId,
        isApp = req.yoho.isApp || false,
        isTab = req.query.isTab || false,
        showAuthor = false;

    let uid = req.user.uid || req.query.uid,
        udid = req.sessionID;

    if (!stringProcess.isNumeric(sortId)) {
        res.json({
            code: 400,
            message: '参数错误',
            data: ''
        });
        return;
    }
    if (!page && !isNaN(page)) {
        res.json({
            code: 400,
            message: '参数错误',
            data: ''
        });
        return;
    }
    if (!authorId && isNaN(authorId)) {
        showAuthor = true;
    }
    return req.ctx(IndexModel).getPageData(gender,
        sortId,
        uid,
        udid,
        page,
        tag,
        authorId,
        isApp,
        showAuthor,
        isTab).then(data => {
        if (data) {
            res.render('index/page', Object.assign(data, {
                layout: false
            }));
        } else {
            res.json({
                code: 400,
                message: '',
                data: ''
            });
        }
    }).catch(next);

};

/**
 * 逛列表页
 * @param req
 * @param res
 * @param next
 */
const index = (req, res, next) => {
    let uid = req.user.uid || req.query.uid;
    let responseData = {
        module: 'guang',
        page: 'index',
        title: '逛|逛潮流,逛购物,官方授权正品潮流购物中心|YOHO!BUY有货',
        keywords: '逛,逛潮流,逛购物',
        description: 'YOHO!BUY有货逛频道,来YOHO!玩潮流!潮搭大解析!年轻人潮流购物中心,中国潮流购物风向标,吴亦凡重磅代言!YOHO!BUY有货100%正品保证,支持货到付款。',
        showFooterTab: footerModel.getUrlData('guang')
    };

    let param = {
        uid: uid,
        udid: req.user.udid,
        type: req.query.type || _.parseInt(stringProcess.paramsFilter(req.query.id)) || '0',
        gender: req.query.gender || req.query.channel && typeLib.gender[req.query.channel] || '1,3'
    };

    req.ctx(IndexModel).getArticle(param).then(result => {
        if (result && result.guang && result.guang.infos) {
            if (!result.guang.infos.length) {
                res.set('Cache-Control', 'no-cache');
            }
        }

        // 唤起 APP 的路径
        res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy= {"action":"go.guangchannel","params":{"id":${param.type}}}`;

        res.render('guang', Object.assign(responseData, result, {
            localCss: true
        }));
    }).catch(next);
};

/**
 * 逛标签页
 * @param req
 * @param res
 * @param next
 */
const tag = (req, res, next) => {
    let uid = req.user.uid || req.query.uid || 0;
    let headerData = headerModel.setNav({
        navTitle: '逛标签'
    });

    let tagTitle = req.query.query || '';

    let responseData = {
        pageHeader: headerData,
        module: 'guang',
        page: 'index-editor',
        title: tagTitle + ' | Yoho!Buy有货 | 潮流购物逛不停',
        keywords: tagTitle,
        description: 'YOHO!BUY有货潮流' + tagTitle + '!'
    };

    let param = {
        tag: req.query.query,
        isApp: req.query.app_version || req.query.appVersion || false,
        gender: req.query.gender || '1,3',
        uid: uid,
        udid: req.sessionID,
        type: req.query.type || 0,
        path: req.path
    };

    responseData.pageHeader.navTitle = param.tag || '标签';


    req.ctx(IndexModel).getTagEditor(param).then(result => {
        res.render('index/list', Object.assign(responseData, result, {
            localCss: true
        }));
    }).catch(next);
};

/**
 * 列表页(列表首页、标签列表页、作者列表页)动态数据,如:查看数,点赞数,评论数,是否点赞,是否回复
 * @param req
 * @param res
 */

const listDynamicData = (req, res) => {
    let uid = req.user.uid || req.query.uid;
    let ids = req.query.ids;

    let udid = req.sessionID;

    let other = {};
    let query = req.query.query,
        type = req.query.type;

    other.uid = uid;

    if (query) {
        other.query = query;
    }

    if (type) {
        other.type = type;
    }

    req.ctx(IndexModel).getDynamicDataByIds(ids, udid, other).then(ret => {
        res.send(ret);
    });
};

/**
 * 详情页动态数据,如:评论数,回复数,是否点赞,是否收藏
 * @param req
 * @param res
 */
const detailDynamicData = (req, res) => {

    let id = req.query.id,
        uid = req.user.uid || req.query.uid,
        udid = req.sessionID;

    req.ctx(IndexModel).getDynamicDataById(id, uid, udid).then((ret) => {
        res.status(200).send(ret);
    }).catch(() => {
        res.status(400);
    });
};

module.exports = {
    editor,
    pageData,
    index,
    tag,
    listDynamicData,
    detailDynamicData,
    editorRedirect
};