...
|
...
|
@@ -7,6 +7,8 @@ |
|
|
import Model from './model';
|
|
|
import rp from 'request-promise';
|
|
|
import ws from '../../lib/ws';
|
|
|
import sign from '../../lib/sign'
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
class ProductCache extends Model{
|
|
|
constructor() {
|
...
|
...
|
@@ -34,6 +36,78 @@ class ProductCache extends Model{ |
|
|
self._broadcast(`开始提交:其它批量`);
|
|
|
await self._batchPost(list, url);
|
|
|
}
|
|
|
async generateProductUrl(list) {
|
|
|
let self = this;
|
|
|
let interval = 50;
|
|
|
let tick = parseInt(list.length / interval, 10) + (list.length % interval > 0 ? 1 : 0);
|
|
|
let result = [];
|
|
|
self._broadcast(`获取商品信息中:`);
|
|
|
for (var tickIndex = 0; tickIndex < tick; tickIndex++) {
|
|
|
let datas = list.slice(tickIndex * interval, (tickIndex + 1) * interval > list.length ? list.length : (tickIndex + 1) * interval);
|
|
|
try {
|
|
|
let sknList = [];
|
|
|
_.forEach(datas, data => {
|
|
|
sknList.push(data.skn)
|
|
|
})
|
|
|
let skns = sknList.join(',');
|
|
|
self._broadcast(`进度:${tickIndex+1}/${tick}`)
|
|
|
let res = await self._batchProductInfo(skns);
|
|
|
_.forEach(res.data.product_list, (product) => {
|
|
|
result.push(self._joinProductUrl(product));
|
|
|
});
|
|
|
} catch(exp) {
|
|
|
}
|
|
|
}
|
|
|
self._broadcast(`获取完成,正在生成文件`);
|
|
|
return result;
|
|
|
}
|
|
|
async _batchProductInfo(skns) {
|
|
|
let params = {
|
|
|
productSkn: skns,
|
|
|
method: 'h5.product.batch'
|
|
|
}
|
|
|
params = sign.apiSign(params, 'h5', '5.1.0', '');
|
|
|
return new Promise((resolve, reject) => {
|
|
|
rp({
|
|
|
url: 'http://api.yoho.cn/',
|
|
|
qs: params,
|
|
|
json: true,
|
|
|
timeout: 3000,
|
|
|
gzip: true
|
|
|
})
|
|
|
.then(function (res) {
|
|
|
res.code === 200 ? resolve(res) : reject(res);
|
|
|
})
|
|
|
.catch(function (err) {
|
|
|
if (err.response) {
|
|
|
console.log(err.response.body);
|
|
|
reject(err.response.body)
|
|
|
}
|
|
|
});
|
|
|
})
|
|
|
}
|
|
|
_joinProductUrl(product) {
|
|
|
if (product && product.goods_list && product.goods_list.length) {
|
|
|
return `/product/pro_${product.product_id}_${product.goods_list[0].goods_id}/${product.cn_alphabet}.html`;
|
|
|
}
|
|
|
return '';
|
|
|
}
|
|
|
rendExcel(sheets) {
|
|
|
if (sheets.length) {
|
|
|
let rows = sheets[0].data;
|
|
|
if (rows.length > 1) {
|
|
|
let data = [];
|
|
|
for (var i = 1; i < rows.length; i++) {
|
|
|
var row = rows[i];
|
|
|
if (row.length) {
|
|
|
data.push({skn: row[0]});
|
|
|
}
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
}
|
|
|
return [];
|
|
|
}
|
|
|
async _batchPost(list, url) {
|
|
|
let self = this;
|
|
|
let interval = 50;
|
...
|
...
|
@@ -44,7 +118,11 @@ class ProductCache extends Model{ |
|
|
let limit = list.length > (i + 1) * interval ? (i + 1) * interval : list.length;
|
|
|
let datas = list.slice(i * interval, limit);
|
|
|
try {
|
|
|
await self._postApi(datas, url);
|
|
|
let skns = [];
|
|
|
_.forEach(datas, data => {
|
|
|
skns.push(data.skn)
|
|
|
})
|
|
|
await self._postApi(skns, url);
|
|
|
self._broadcast(`进度:${limit}/${list.length}`)
|
|
|
} catch(err) {
|
|
|
self._broadcast(`错误:${err.message}`)
|
...
|
...
|
@@ -72,8 +150,10 @@ class ProductCache extends Model{ |
|
|
res.code === 200 ? resolve(res) : reject(res);
|
|
|
})
|
|
|
.catch(function (err) {
|
|
|
if (err.response) {
|
|
|
console.log(err.response.body);
|
|
|
reject(err.response.body)
|
|
|
}
|
|
|
});
|
|
|
})
|
|
|
|
...
|
...
|
|