material.js 3.14 KB
'use strict';

const Model = require('../models/material');
const searchHandler = require('../../product/models/search-handler');

const index = (req, res, next) => {
    let uid = req.user.uid;
    let params = {
        limit: req.query.limit || 50,
        page: req.query.page || 1
    };

    if (req.query.union_type) {
        params.union_type = req.query.union_type;
    }

    Model.canLogin(uid).then(canLogin => {
        if (canLogin === 'N') {
            return next();
        } else {
            Model.getRecommendProductList({
                page: params.page
            }).then(result => {
                if (result && result.code === 200) {
                    res.render('material', Object.assign({
                        unionType: req.query.union_type,
                        module: '3party',
                        page: 'material',
                        layout: false,
                        footPager: Object.assign(searchHandler.handlePagerData(result.data.total, params), {tip: false})
                    }, result.data));
                } else {
                    return next();
                }
            }).catch(next);
        }
    }).catch(next);
};


const indexNew = (req, res, next) => {
    let unionType = req.query.union_type;
    let params = req.query;

    Model.canLogin(req.user.uid).then(canLogin => {
        if (canLogin === 'N') {
            return next();
        } else {
            Model.getApiRecommendProductList(params, unionType).then(result => {
                res.render('material-new', Object.assign(result, {
                    unionType: unionType,
                    module: '3party',
                    page: 'material-new',
                    layout: false
                }));
            }).catch(next);
        }
    }).catch(next);
};

const newBrandList = (req, res, next) => {
    Model.newBrandList(req).then(data => {
        res.send(data);
    }).catch(next);
};

const getCategory = (req, res, next) => {
    Model.getCategory().then(data => {
        res.send(data);
    }).catch(next);
};

const getList = (req, res, next) => {
    Model.getRecommendProductList(req.query).then(result => {
        res.send(result);
    }).catch(next);
};

const getRecommendlist = (req, res, next) => {
    Model.getRecommendlist().then(result => {
        res.send(result);
    }).catch(next);
};

// exports.getIndexGuide = (req, res, next) => {
//     channelModel.getIndexGuideData().then(data => {
//         if (data.code !== 200) {
//             const err = new Error('异常');

//             throw err;
//         }
//         return channelModel.formatIndexGuideData(data.data);
//     }).then(data => {
//         return channelModel.getResourceData(data);
//     }).then(data => {
//         let result = {list: data, layout: false};

//         res.render('guide', result);
//     }).catch(next);
// };

// exports.hasNewUserFloor = (req, res, next) => {

//     channelModel.hasNewUserFloor(req.yoho.channel, req.user.uid).then(data => {
//         res.send(data);
//     }).catch(next);
// };

module.exports = {
    index,
    indexNew,
    newBrandList,
    getCategory,
    getList,
    getRecommendlist
};