Authored by 郝肖肖

'co'

... ... @@ -8,6 +8,7 @@ const yohoLib = require('yoho-node-lib');
// set root path
global.ROOT_PATH = path.join(__dirname, '');
global.Promise = require('bluebird');
// 全局注册library
yohoLib.global(config);
... ...
... ... @@ -36,14 +36,12 @@ const autoGoodsXml = (req, res) => {
const goodsUrl = (req, res) => {
return req.ctx(BaiduLinkSubmit).goodsUrl().then(result => {
console.log(result);
return res.json(result);
});
};
const guangUrl = (req, res) => {
return req.ctx(BaiduLinkSubmit).guangUrl().then(result => {
console.log(result);
return res.json(result);
});
};
... ...
... ... @@ -9,8 +9,7 @@ const XzhIndexModel = require('../../models/xzh/index');
const index = (req, res) => {
req.ctx(XzhIndexModel).index().then(result => {
console.log(result);
return req.ctx(XzhIndexModel).index().then(result => {
return res.json(result);
});
... ...
... ... @@ -4,7 +4,7 @@ const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const redis = require(`${ROOT_PATH}/libs/redis`);
const YohoApiModel = require('./../interface/yoho-api');
const co = Promise.coroutine;
const rp = require('request-promise');
const senUrl = 'http://data.zz.baidu.com/urls?token=0lSAO4ZxEKsYopMG&site=';
... ... @@ -46,17 +46,9 @@ class BaiduLinkSubmit extends global.yoho.BaseModel {
// 提交商品详情URL
goodsUrl() {
let ids = [];
let message = '';
return this.yohoApiModel.bdPromotion().then(rdata => {
rdata = _.get(rdata, 'data', []);
ids = _.map(rdata, item => {
return `${item.erpProductId}`;
});
let that = this;
return redis.hmgetAsync('global:yoho:seo:linksubmit:goods', ids);
}).then(rdata => {
return co(function* () {
let products = {
pc: [],
... ... @@ -64,14 +56,21 @@ class BaiduLinkSubmit extends global.yoho.BaseModel {
pcUrl: 'https://www.yohobuy.com',
wapUrl: 'https://m.yohobuy.com'
};
let tids = [];
let rdata = _.get(yield that.yohoApiModel.bdPromotion(), 'data', []);
let ids = _.map(rdata, item => {
return `${item.erpProductId}`;
});
let artice = {
total: ids.length,
notAlready: 0
};
ids = _.difference(ids, rdata);// 去除已经推送的
ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:linksubmit:goods', ids));// 去除已经推送的
artice.notAlready = ids.length;
message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;
let message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;
if (artice.notAlready <= 0) {
return [
... ... @@ -81,50 +80,40 @@ class BaiduLinkSubmit extends global.yoho.BaseModel {
}
_.each(ids, val => {
tids.push(val, val);// mset做准备
products.pc.push(`${products.pcUrl}/product/${val}.html`);
products.wap.push(`${products.wapUrl}/product/${val}.html`);
});
return Promise.all([
this.sendUrl(products.pc, products.pcUrl),
this.sendUrl(products.wap, products.wapUrl)
rdata = yield Promise.all([
that.sendUrl(products.pc, products.pcUrl),
that.sendUrl(products.wap, products.wapUrl)
]);
}).then(rdata => {
if (rdata[0].code !== 200 || rdata[1].code !== 200) {
return rdata;
}
let tids = [];
yield redis.hmsetAsync('global:yoho:seo:linksubmit:goods', tids);
_.each(ids, id => {
tids.push(id, id);
});
ids = [];
tids = [];
return redis.hmsetAsync('global:yoho:seo:linksubmit:goods', tids).then(() => {
ids = [];
tids = [];
Object.assign(rdata[0], {message: message});
Object.assign(rdata[1], {message: message});
Object.assign(rdata[0], {message: message});
Object.assign(rdata[1], {message: message});
return rdata;
});
});
return rdata;
})();
}
// 提交逛详情URL
guangUrl() {
let ids = [];
let message = '';
return this.yohoApiModel.getLastArticleList().then(rdata => {
rdata = _.get(rdata, 'data.artList', []);
ids = _.map(rdata, item => {
return `${item.articleId}`;
});
let that = this;
return redis.hmgetAsync('global:yoho:seo:linksubmit:guang', ids);
}).then(rdata => {
return co(function* () {
let guangs = {
pc: [],
... ... @@ -132,14 +121,20 @@ class BaiduLinkSubmit extends global.yoho.BaseModel {
pcUrl: 'https://www.yohobuy.com',
wapUrl: 'https://m.yohobuy.com'
};
let tids = [];
let rdata = _.get(yield that.yohoApiModel.getLastArticleList(), 'data.artList', []);
let ids = _.map(rdata, item => {
return `${item.articleId}`;
});
let artice = {
total: ids.length,
notAlready: 0
};
ids = _.difference(ids, rdata);// 去除已经推送的
ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:linksubmit:guang', ids));// 去除已经推送的
artice.notAlready = ids.length;
message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;
let message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;
if (artice.notAlready <= 0) {
return [
... ... @@ -148,38 +143,33 @@ class BaiduLinkSubmit extends global.yoho.BaseModel {
];
}
_.each(ids, val => {
tids.push(val, val);// mset做准备
guangs.pc.push(`${guangs.pcUrl}/guang/${val}.html`);
guangs.wap.push(`${guangs.wapUrl}/guang/${val}.html`);
});
return Promise.all([
this.sendUrl(guangs.pc, guangs.pcUrl),
this.sendUrl(guangs.wap, guangs.wapUrl)
rdata = yield Promise.all([
that.sendUrl(guangs.pc, guangs.pcUrl),
that.sendUrl(guangs.wap, guangs.wapUrl)
]);
}).then(rdata => {
if (rdata[0].code !== 200 || rdata[1].code !== 200) {
return rdata;
}
let tids = [];
_.each(ids, id => {
tids.push(id, id);
});
yield redis.hmsetAsync('global:yoho:seo:linksubmit:guang', tids);
return redis.hmsetAsync('global:yoho:seo:linksubmit:guang', tids).then(() => {
ids = [];
tids = [];
ids = [];
tids = [];
Object.assign(rdata[0], {message: message});
Object.assign(rdata[1], {message: message});
Object.assign(rdata[0], {message: message});
Object.assign(rdata[1], {message: message});
return rdata;
});
});
return rdata;
})();
}
}
... ...
... ... @@ -2,8 +2,10 @@
const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const co = Promise.coroutine;
const rp = require('request-promise');
const redis = require(`${ROOT_PATH}/libs/redis`);
const YohoApiModel = require('./../interface/yoho-api');
const senUrl = 'http://data.zz.baidu.com/urls?appid=1583402501013173&token=K0L5PUhk1XOko81r&type=';
// http://ziyuan.baidu.com/xzh/commit/push?appid=1583402501013173&qq-pf-to=pcqq.c2c
... ... @@ -11,30 +13,31 @@ const senUrl = 'http://data.zz.baidu.com/urls?appid=1583402501013173&token=K0L5P
class XzhIndexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.yohoApiModel = new YohoApiModel(ctx);
}
index() {
let that = this;
let ids = [];
let message = '';
return co(function* () {
let ids = [];
let message = '';
// 新增
return this.getLastArticleList({page: 1, limit: 100}).then(rdata => {
// 新增
let rdata = yield that.yohoApiModel.getLastArticleList({page: 1, limit: 100});
rdata = _.get(rdata, 'data.artList', []);
ids = _.map(rdata, item => {
return `${item.articleId}`;
});
return redis.hmgetAsync('global:yoho:seo:xzh:guang', ids);
}).then(rdata => {
let artice = {
total: ids.length,
notAlready: 0
};
ids = _.difference(ids, rdata);// 去除已经推送的
ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:xzh:guang', ids));// 去除已经推送的
artice.notAlready = ids.length;
message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;
... ... @@ -42,8 +45,7 @@ class XzhIndexModel extends global.yoho.BaseModel {
return {code: 201, data: {}, message: message};
}
return this.sendData(ids, 'realtime');
}).then(rdata => {
rdata = yield that.sendData(ids, 'realtime');
if (rdata.code !== 200) {
return rdata;
... ... @@ -55,13 +57,13 @@ class XzhIndexModel extends global.yoho.BaseModel {
tids.push(id, id);
});
return redis.hmsetAsync('global:yoho:seo:xzh:guang', tids).then(() => {
ids = [];
tids = [];
yield redis.hmsetAsync('global:yoho:seo:xzh:guang', tids);
return Object.assign(rdata, {message: message});
});
});
ids = [];
tids = [];
return Object.assign(rdata, {message: message});
})();
}
// 向百度发送数据
... ... @@ -89,22 +91,6 @@ class XzhIndexModel extends global.yoho.BaseModel {
return {code: 400, data: {}, message: e.message};
});
}
getLastArticleList(params) {
params = _.assign({
limit: 100,
udid: 'seo_format_data'
}, params);
return this.get({
url: '/guang/api/v2/article/getLastArticleList',
data: params,
api: global.yoho.ServiceAPI
}).catch(e => {
console.log(e.message);
return {code: 400, data: {}, message: e.message};
});
}
}
module.exports = XzhIndexModel;
... ...
... ... @@ -38,6 +38,9 @@ const SeoTask = {
resolveWithFullResponse: false, // header、statusCode
json: true,
gzip: true
}).then(rdata => {
console.log(`url: ${url}, result: ${JSON.stringify(rdata)}`);
return rdata;
}).catch(e => {
console.error(`execTask err: ${e.message}`);
return {code: 400, message: e.message};
... ...