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

const mRoot = '../models';
const indexModel = require(`${mRoot}/index`);
const headerModel = require('../../../doraemon/models/header'); // 头部model
const guangProcess = require(`${global.utils}/guang-process`);
const stringProcess = require(`${global.utils}/string-process`);
const Promise = require('bluebird');
const channels = {
    boys: 1,
    girl: 2,
    kids: 3,
    lifestyle: 4
};

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

    if (isApp === false) {
        parameter = {
            pageHeader: headerModel.setNav({
                navTitle: title
            })
        };
    }

    return Promise.all([indexModel.getAuthor(id), indexModel.getArticleList(gender, 0, uid, udid, 1, null, id)]).then(datas => {
        let authorData = datas[0],
            articleListData = datas[1];
        let build = [];
        if (articleListData.data.list.artList) {
            articleListData.data.list.artList.forEach(articleData => {
                build.push(guangProcess.formatArticle(articleData, true, isApp, false, uid));
            });
            res.render('index/list', Object.assign({
                page: 'index-editor',
                title: title,
                guangList: true,
                gender: gender,
                guang: {
                    infos: build,
                    isApp: isApp,
                    authorInfo: authorData.data
                }
            }, parameter));
        } else {
            return next();
        }

    }).catch(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,
        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 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 headerData = headerModel.setNav({
        navTitle: '逛'
    });

    let responseData = {
        pageHeader: headerData,
        module: 'guang',
        page: 'index',
        title: '逛 | Yoho!Buy有货 | 潮流购物逛不停',
        pageFooter: true
    };

    let param = {
        uid: req.user.uid,

        udid: req.user.udid,

        type: req.query.type || '0',

        gender: req.query.gender || '1,3'
    };

    indexModel.getArticle(param).then(result => {
        res.render('index', Object.assign(responseData, result));
    }).catch(next);
};

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

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

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

    let param = {
        tag: req.query.query,
        isApp: req.query.app_version || req.query.appVersion || false,
        gender: req.query.gender || req.cookies._Channel && channels[req.cookies._Channel] || '1,3',
        uid: req.user.uid,
        udid: req.user.udid,
        type: req.query.type || 0

    };


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


    indexModel.getTagEditor(param).then(result => {
        res.render('index/list', Object.assign(responseData, result));
    }).catch(next);
};

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