SearchData.js 1.99 KB
'use strict';
const querystring = require('querystring');


const getUrl = ($type)=>{
    var map = {
        sort: '/sortgroup.json',
        discount: '/discount.json',
        recent: '/recent.json',
        suggest: '/suggest.json',
        'new-shelve': '/new-shelve.json',
        shop: '/shops.json',
        brand: '/brand/list.json'
    };
    return map[$type] || '/search.json';
};

const getBrandListUrl = ()=>{
    return getUrl('brand');
};
const getProductUrl = ($condition, $type)=>{
    let $orderMaps = {
        s_t_desc: 'shelve_time:desc',
        s_t_asc: 'shelve_time:asc',
        s_p_asc: 'sales_price:asc',
        s_p_desc: 'sales_price:desc',
        p_d_desc: 'discount:desc',
        p_d_asc: 'discount:asc',
        skn_desc: 'product_skn:desc',
        skn_asc: 'product_skn:asc',
        activities_desc: 'activities.order_by:desc',
        activities_asc: 'activities.order_by:asc',
        s_n_asc: 'sales_num:asc',
        s_n_desc: 'sales_num:desc',
        activities_id_desc: 'activities.activity_id:desc',
        activities_id_asc: 'activities.activity_id:asc',
        brand_desc: 'brand_weight:desc'
    };
    let $param = {
        status: 1,
        sales: 'Y',
        outlets: 2,
        stocknumber: 1,
        attribute_not: 2
    };
    if (!$condition.order) {
        $param.order = $orderMaps.s_t_desc;
    } else {
        $param.order = $orderMaps[$condition.order] ? $orderMaps[$condition.order] : '';
    }
    if (!$condition.page) {
        $param.page = 1;
    }
    if ($condition.viewNum) {
        $param.viewNum = $condition.viewNum;
    } else if (!$condition.limit) {
        $param.viewNum = 60;
    } else {
        $param.viewNum = $condition.limit;
        delete $condition.limit;
    }
    if (!$condition) {
        $param += $condition;
    }
    console.log(JSON.stringify($param));
    return getUrl($type) + '?' + querystring.stringify($param);// searchApi.get(getUrl($type), $param,{cache:true});
};

module.exports = {
    getProductUrl,
    getBrandListUrl
};