common.yohosearch.js
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var yohoSearch = global.global.Register.yohoSearch;
module.exports = {
namespace: "common",
apis: {
brandlist: function (req, callback) {
var result = request.get(yohoSearch + "/brands.json?status=1");
return callback(null, result);
},
search: function (req, callback) {
var size = 50;//每页数据量
var params = [];
for (var key in req.body) {
params.push(key + "=" + encodeURI(req.body[key]));
}
var url = yohoSearch + "/search.json?viewNum=" + size + "&" + params.join('&');
console.info(url);
request({url: url}, function (error, response, body) {
if (error) {
return callback(null, {msg: "error", code: 500});
} else {
var data = {};
data.list = [];
body = JSON.parse(body);
var dataList = body.data.product_list;
var goodslist = [];
var goodslist1 = [];
for (var m = 0; m < dataList.length; m++) {
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: {}});
}
}
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;//记录每一页数据量
return callback(null, {data: data, code: 200});
}
});
}
}
};