...
|
...
|
@@ -5,13 +5,14 @@ |
|
|
*/
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
const Router = require('koa-router');
|
|
|
const moment = require('moment');
|
|
|
const pager = require('../utils/pager');
|
|
|
const Mysql = require('../../../lib/mysql-promise');
|
|
|
|
|
|
const Promise = require('bluebird');
|
|
|
const r = new Router();
|
|
|
const rp = require('request-promise');
|
|
|
|
|
|
const multiAsync=(multi)=>{
|
|
|
return multi.execAsync().then(function(res) {
|
...
|
...
|
@@ -157,16 +158,138 @@ r.get('/searchKeywords', async(ctx) => { |
|
|
};
|
|
|
});
|
|
|
|
|
|
const insertWord = (keyword, mysql) => {
|
|
|
return mysql.query(`SELECT id FROM seo_keywords WHERE keyword = ?`, [keyword]).then(d => {
|
|
|
console.log(keyword, d, '--+++++++++++++++++++++++-');
|
|
|
if (d.length <= 0) {
|
|
|
return mysql.query(
|
|
|
`INSERT INTO seo_keywords SET keyword = ?, is_push = ?, yoho_goods_num= ?, add_time = ?`,
|
|
|
[keyword, 1, -2, moment().unix()]
|
|
|
);
|
|
|
}
|
|
|
return d;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
//redis关键词同步到mysql
|
|
|
r.get('/syncWord', async(ctx) => {
|
|
|
let mysql = new Mysql();
|
|
|
|
|
|
let r = await mysql.query(`select * from seo_keywords`);
|
|
|
console.log(r[0].keyword, ' ===');
|
|
|
ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
|
|
data: r
|
|
|
};
|
|
|
return getDataList(ctx, "keywords_mana_list", 0, -1).then(d => {
|
|
|
return getDataValues(ctx, d[0]);
|
|
|
}).then(ddata => {
|
|
|
return Promise.map(ddata, (word) => insertWord(word, mysql), {concurrency: 15});
|
|
|
}).then(d => {
|
|
|
return ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
|
|
data: d
|
|
|
};
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
r.get('/expand', async(ctx) => {
|
|
|
let resData = {};
|
|
|
let q = ctx.request.query;
|
|
|
let query = q.query || '';
|
|
|
let page = parseInt(`0${ctx.query.page}`, 10) || 1;
|
|
|
let limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
|
|
|
let mysql = new Mysql();
|
|
|
let total = 0;
|
|
|
let typeList = [
|
|
|
{
|
|
|
type: 'keyword',
|
|
|
name: '关键词'
|
|
|
},
|
|
|
{
|
|
|
type: 'wordroot',
|
|
|
name: '词 根'
|
|
|
},
|
|
|
{
|
|
|
type: 'goodnum',
|
|
|
name: '商品数'
|
|
|
}
|
|
|
];
|
|
|
let type = q.type || typeList[0].type;
|
|
|
let typeName = _.result(_.find(typeList, { 'type': type}), 'name') || typeList[0].name;
|
|
|
let wheres = '';
|
|
|
let conditions = [];
|
|
|
|
|
|
switch (query && type) {
|
|
|
case 'keyword':
|
|
|
wheres = ` AND keyword like '%%${query}%'`;
|
|
|
break;
|
|
|
case 'wordroot':
|
|
|
wheres += ' AND root_id = ?'
|
|
|
conditions.push(query);
|
|
|
break;
|
|
|
case 'brand':
|
|
|
wheres += ' AND brand_id = ?'
|
|
|
conditions.push(query);
|
|
|
break;
|
|
|
case 'sort':
|
|
|
wheres += ' AND sort_id = ?'
|
|
|
conditions.push(query);
|
|
|
break;
|
|
|
case 'goodnum':
|
|
|
wheres += ' AND yoho_goods_num >= ?'
|
|
|
conditions.push(query);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
return mysql.query(`SELECT COUNT(*) as total FROM seo_keywords WHERE 1 = 1 ${wheres}`, conditions).then(d => {
|
|
|
total = d[0] && d[0].total || 0;
|
|
|
conditions.push((page - 1) * limit, limit);
|
|
|
return mysql.query(`SELECT * FROM seo_keywords WHERE 1 = 1 ${wheres} limit ?, ?`, conditions);
|
|
|
}).then(d => {
|
|
|
|
|
|
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
|
|
|
|
|
|
resData.tabs = _.map(d, (elem) => {
|
|
|
return Object.assign({}, elem, {
|
|
|
is_push: elem.is_push ? '是' : '否',
|
|
|
add_time: elem.add_time && moment(elem.add_time * 1000).format('YYYY-MM-DD HH:mm'),
|
|
|
});
|
|
|
});
|
|
|
|
|
|
resData.typeList = typeList;
|
|
|
resData.typeName = typeName;
|
|
|
resData.type = type;
|
|
|
resData.query = query;
|
|
|
|
|
|
return ctx.render('action/keywords/expand', resData);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
r.post('/expand/del', async(ctx) => {
|
|
|
let q = ctx.request.body;
|
|
|
let ids = q.ids && JSON.parse(q.ids) || [];
|
|
|
|
|
|
let len = ids.length;
|
|
|
let marks = [];
|
|
|
|
|
|
if (len <= 0) {
|
|
|
return ctx.body = {
|
|
|
code: 400,
|
|
|
message: 'ids is empty',
|
|
|
data: ''
|
|
|
};
|
|
|
}
|
|
|
|
|
|
let mysql = new Mysql();
|
|
|
for(let i = 0; i < len; i++) {
|
|
|
marks.push('?');
|
|
|
}
|
|
|
|
|
|
return mysql.query(`DELETE FROM seo_keywords WHERE id IN (${marks.join(',')})`, ids).then(d => {
|
|
|
return ctx.body = {
|
|
|
code: 200,
|
|
|
message: 'success',
|
|
|
data: d
|
|
|
};
|
|
|
});
|
|
|
});
|
|
|
|
|
|
module.exports = r; |
...
|
...
|
|