list.js 2.29 KB
'use strict';

const _ = require('lodash');
const Search = require('../models/search');
const camelCase = global.yoho.camelCase;
const DateHelper = require('../models/helpers');

const list = {
    index: (req, res, next) => {
        let q = req.query;

        q.page = q.page || 1;

        let retDate = {
            module: 'product',
            page: 'list',
            title: '列表'
        };

        Search.queryProduct(q).then(result => {

            if (result && result.code === 200 && result.data) {
                let data = camelCase(result.data);

                if (data.filter) {
                    retDate.filter = DateHelper.filterHandle(data.filter, q);
                }

                retDate.paginationData = {
                    page: q.page,
                    limit: data.limit || 45,
                    total: data.total,
                    pageTotal: data.pageTotal,
                    queryParams: q
                };
                res.display('list', _.assign(retDate, {
                    products: data.productList,
                    order: q.order
                }));
            } else {
                return Promise.reject('query product error');
            }
        }).catch(next);

    },

    newPage: (req, res, next) => {
        let q = req.query;

        q.page = q.page || 1;

        let retDate = {
            module: 'product',
            page: 'list',
            title: '列表'
        };

        Search.queryProduct(q).then(result => {

            if (result && result.code === 200 && result.data) {
                let data = camelCase(result.data);

                if (data.filter) {
                    retDate.filter = DateHelper.filterHandle(data.filter, q);
                }

                retDate.paginationData = {
                    page: q.page,
                    limit: data.limit || 45,
                    total: data.total,
                    pageTotal: data.pageTotal,
                    queryParams: q
                };
                res.display('newList', _.assign(retDate, {
                    products: data.productList,
                    order: q.order
                }));
            } else {
                return Promise.reject('query product error');
            }
        }).catch(next);
    }
};

module.exports = list;