spider-local-task.js
4.42 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const _ = require('lodash');
const spider = require('../libs/spider');
// const sknsData = require('../data/skns.json');
const legoData = require('../data/lego.json');
const { logger } = require('../libs/logger');
const { getUfoSpecialSizeProducts, getsSizeData, handleChannelSkuCompare, queryAllChannelProductRelations } = require('../libs/ufo-product-api');
const { getSize } = require('../utils');
const ddAlert = require('../libs/dingding-alert');
const moment = require('moment');
module.exports = async(time) => {
var beginTime = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
ddAlert(`监控提醒 : 比价开始 — ${beginTime} `, 'bjSpider');
let sizeData = {};
let specialSizeProducts = {};
let sizeRelation = {};
let productRelationsArr = {};
let sknsData = [];
let n = 0; // 接口请求次数的初始值
try {
// 获取所有要比价的商品对应关系
const productRelations = await queryAllChannelProductRelations();
if (productRelations) {
productRelationsArr = JSON.parse(productRelations).data;
}
// 获取特殊的商品
const resultSpecialSizeProducts = await getUfoSpecialSizeProducts();
if (resultSpecialSizeProducts) {
specialSizeProducts = JSON.parse(resultSpecialSizeProducts).data;
}
// 获取尺码
const resultSizeData = await getsSizeData();
if (resultSizeData) {
sizeData = JSON.parse(resultSizeData).data;
}
}catch(error) {
ddAlert(`监控提醒 : pop 服务接口异常,请查看! @15101660386 `, 'bjSpider');
return false;
}
//判断pop服务接口是否为空
if(sizeData.length < 1 || specialSizeProducts.length < 1 || productRelationsArr.length < 1) {
ddAlert(`监控提醒 : pop 接口获取数据为空,请查看! @15101660386 `, 'bjSpider');
return false;
}
try {
// 重新组织数据
productRelationsArr.forEach(relations => {
sknsData.push({'productId' : relations.channelProductId, 'yhId': relations.productId});
});
sizeData.forEach(size => {
sizeRelation[_.trim(size.sizeName)] = {
sizeId: size.id
};
});
} catch (error) {
ddAlert(`监控提醒 : pop 接口获取数据为空,请查看! @15101660386 `, 'bjSpider');
return false;
}
logger.info(`[spiderTask] time: ${beginTime}, product_number: ${sknsData && sknsData.length}`);
spider(sknsData.map(p => p.productId), '', {}, 3500)
.forEach(promise => promise.then(result => {
n++;
if (n === sknsData.length) {
const endTime = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
ddAlert(`监控提醒 : 比价结束 — ${endTime} `, 'bjSpider');
}
if (result.status !== 200 && !_.has(result, 'data.detail')) {
logger.warn(`[spider-task]爬取失败记录: ${JSON.stringify(result)}`);
return;
}
const {data: {detail, sizeList}} = result;
const yhProduct = sknsData.find(p => p.productId === detail.productId);
const isSpecialSize = specialSizeProducts.includes(yhProduct.yhId);
sizeList.forEach(s => {
if (_.isPlainObject(s.item) && s.item.price > 0) {
if (sizeList.length === 1) {
const isLego = legoData.some(lego => lego.productId === detail.productId);
if (isLego) {
s.size = 'F';
console.log(`is lego size = F, ${detail.productId}`);
}
}
const find = getSize(s.size, sizeRelation, isSpecialSize);
if (!find) {
return logger.error(`[spider-task]爬取尺码对应关系未找到: ${JSON.stringify(s)}`);
}
if (isSpecialSize) {
console.log(`=================>spider-task yhId: ${yhProduct.yhId}, duId: ${detail.productId} size: ${s.size} => ${find.relationSize || s.size}, ${find.sizeId}`);
}
const highRate = 0.02;
handleChannelSkuCompare({
productId: yhProduct.yhId,
sizeId: find.sizeId,
channelPrice: s.item.price / 100,
highRate
}).then((ret) => {
const res = JSON.parse(ret);
if( res.code === 200) {
return logger.info(`[spider-task]更新sku价格成功记录, duId:${detail.productId}, yhId: ${yhProduct.yhId}, sizeId: ${find.sizeId}, highRate: ${highRate}`);
}
return logger.error(res.message);
}).catch(error => {
logger.error(error.message);
});
}
});
}));
};