global.js 2.97 KB
'use strict';

const headerModel = require('../../../doraemon/models/header');
const model = require('../models/global');

const list = (req, res, next) => {
    let brand = req.query.brand;
    let sort = req.query.sort;
    let title = req.query.title;

    if (!brand && !sort) {
        return res.redirect('//m.yohobuy.com');
    }

    let param = {
        limit: 12,
        order: 's_t_desc',
        page: 1,
        uid: req.user.uid || 0
    };

    if (brand) {
        param.brand = brand;
    } else if (sort) {
        param.sort = sort;
    }

    let arrs = [model.list(param)];

    if (brand) {
        arrs.push(model.getBrand({
            brandId: brand,
            uid: req.user.uid || 0,
            client_type: 'iphone' // todo
        }));
    }

    Promise.all(arrs).then((result) => {
        let t = (result[1] || {}).brand_name || title;

        param.title = t;
        res.render('global/list', {
            module: 'product',
            page: 'global-list',
            pageHeader: headerModel.setNav({
                navTitle: t
            }),
            list: result[0].list,
            filter: result[0].filter,
            pageFooter: true,
            localCss: true,
            appPath: `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.globalpurchase","params":${JSON.stringify(param)}}`
        });
    }).catch(next);
};

const search = (req, res, next) => {
    let query = req.query;

    if (query.type === 'new') {
        query.order = 's_t_desc';
    } else if (query.type === 'price') {
        query.order = query.order === '1' ? 's_p_asc' : 's_p_desc';
    }

    model.list(query).then((result) => {
        res.render('global/search', {
            list: result.list,
            layout: false
        });
    }).catch(next);
};

const detail = (req, res, next) => {
    let skn = req.params[0];
    let params = {
        product_skn: skn,
        uid: req.user.uid || 0,
        client_type: 'iphone' // todo
    };

    model.detail(params).then((result) => {
        let appParams = {
            skn: skn
        };
        let appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.globalpurchase","params":${JSON.stringify(appParams)}}`;

        res.render('global/detail', {
            module: 'product',
            page: 'global-detail',
            pageHeader: headerModel.setNav({
                navTitle: '商品详情'
            }),
            result: result,
            cartUrl: '//m.yohobuy.com/cart/index/index',
            pageFooter: true,
            localCss: true,
            appPath: appPath
        });
    }).catch(next);
};

const gethtml = (req, res, next) => {
    let skn = req.query.skn;

    if (!skn) {
        return next();
    }

    let params = {
        product_skn: skn,
        uid: req.user.uid || 0,
        client_type: 'iphone' // todo
    };

    model.gethtml(params).then((html) => {
        res.send(html);
    }).catch(next);
};

module.exports = {
    list,
    search,
    detail,
    gethtml
};