common.yohosearch.js 3.14 KB
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var yohoSearch = global.Register.yohoSearch;

module.exports = {
    namespace: "common",
    apis: {
        brandlist: function (req, callback) {
            var result = request.get(yohoSearch + "/brands.json?status=1&contain_global=Y");
            return callback(null, result);
        },
        aggStyle:function(req,callback){
            request.get(yohoSearch + "/productindex/aggStyle.json",function(err,res,body){
                return callback(null, body);
            });
        },
        search: function (req, callback) {
            var size = 50;//每页数据量
            var params = [];
            for (var key in req.body) {
                if (key != "size") {
                    params.push(key + "=" + encodeURI(req.body[key]));
                }

            }
            var url = yohoSearch + "/productindex/productList.json?viewNum=" + size + "&" + params.join('&');
            console.info("搜索接口请求地址:"+url);
            request({url: url}, function (error, response, body) {
                if (error) {
                    console.info(error);
                    return callback(null, {msg: "error", code: 500});
                } else {
                    console.info("调用接口成功。");
                    var data = {};
                    data.list = [];
                    try{
                        console.info(body);
                        body = JSON.parse(body);
                        var dataList = body.data.product_list;
                        var goodslist = [];
                        var goodslist1 = [];
                        for (var m = 0; m < dataList.length; m++) {
                            //逛关联商品不需要平铺skn下skc商品
                            /*if (dataList[m].goods_list && dataList[m].goods_list.length != 0) {
                                for (var n = 0; n < dataList[m].goods_list.length; n++) {
                                    goodslist.push({a: dataList[m], b: dataList[m].goods_list[n]})
                                }
                            } else {
                                goodslist.push({a: dataList[m], b: {}});
                            }*/
                            goodslist.push({a: dataList[m], b: {}});
                        }
                        for (var i = 0; i < goodslist.length; i++) {
                            goodslist1[i] = _.assign({}, goodslist[i].a, goodslist[i].b);
                        }
                        data.list = goodslist1.concat();//显示数据
                        data.page = body.data.page;//页码
                        data.totalPage = 1;//body.data.page_total;
                        data.nums = data.list.length;//记录每一页数据量
                        console.info("---接口返回:");
                        console.info(data);
                    }catch(ex){
                        return callback(null, {data: data, code: 500});
                    }
                    
                    return callback(null, {data: data, code: 200});
                }
            });
        }
    }
};