Authored by 郝肖肖

'关键词随机关联'

... ... @@ -14,6 +14,7 @@ const Promise = require('bluebird');
const r = new Router();
const rp = require('request-promise');
const config = require('../../../config/config');
const ExpandModel = require('../models/expandModel');
const singleBrandKeyPre = config.singleBrandKeyPre;
const singleSortKeyPre = config.singleSortKeyPre;
const qs = require('querystring');
... ... @@ -252,7 +253,7 @@ r.get('/expand', async(ctx) => {
},
{
type: 'wordroot',
name: '词'
name: '词根ID'
},
{
type: 'goodnum',
... ... @@ -291,7 +292,7 @@ r.get('/expand', async(ctx) => {
total = d[0] && d[0].total || 0;
conditions.push((page - 1) * limit, limit);
d = await mysql.query(`SELECT * FROM seo_keywords WHERE status = 1 ${wheres} limit ?, ?`, conditions);
d = await mysql.query(`SELECT * FROM seo_keywords WHERE status = 1 ${wheres} ORDER BY id DESC limit ?, ?`, conditions);
let sortIds = [];
let brandIds = [];
... ... @@ -363,6 +364,24 @@ r.post('/expand/del', async(ctx) => {
};
});
});
/**
* [yohobuy-chanping关联词生成]
* @param {[type]} '/expand/randWords' [description]
* @param {[type]} async(ctx [description]
* @return {[type]} [description]
*/
r.post('/expand/randWords', async(ctx) => {
let expandModel = new ExpandModel(ctx);
return expandModel.getRanData().then(d => {
return ctx.body = {
code: 200,
message: 'success'
};
});
});
const baiduUrls = {
urls: 'http://data.zz.baidu.com/urls',
update: 'http://data.zz.baidu.com/update',
... ...
'use strict';
const _ = require('lodash');
const model = require('../../../lib/model');
const MysqlPromise = require('../../../lib/mysql-promise');
const Promise = require('bluebird');
const keywordsRedis = {
'keywordsAllIds': `golobal:yoho:seo:keywords:allIds`,// 关键词表符合条件所有id
'keywordsSortId': `golobal:yoho:seo:keywords:sortId`,// 分类下的所有关键词
'keywordsId': `golobal:yoho:seo:keywords:id`,// 每条关键词关联其它12条关键词
};
class expandModel extends model {
constructor(ctx) {
super(ctx);
this.mysql = new MysqlPromise();
this.redis = ctx.redis;
}
getRanData() {
this.dataIds = [];
return this.mysql.query(`SELECT sort_id FROM seo_keywords WHERE status = 1 GROUP BY sort_id;`).then(rdata => {
return Promise.each(rdata, (item) => this._setSortToRedis(item.sort_id));
}).then(d => {
return this.redis.setAsync(`${keywordsRedis.keywordsAllIds}`, JSON.stringify(this.dataIds));
}).then(d => {
this.dataIds = [];
return d;
});
}
_setSortToRedis(sortId) {
return this.mysql.query(`SELECT id, keyword, root_id, brand_id, sort_id FROM seo_keywords WHERE status = 1 AND yoho_goods_num > 3 AND sort_id=${sortId};`).then(dsort => {
let len = dsort.length;
let key;
let tdata;
if (len <= 0) {
return Promise.resolve(true);
}
return this.redis.setAsync(`${keywordsRedis.keywordsSortId}:${sortId}`, JSON.stringify(dsort)).then(() => {
return Promise.each(dsort, (el, index) => {
key = `${keywordsRedis.keywordsId}:${el.id}`;
this.dataIds.push(el.id);
tdata = {
name: el.keyword,
data: _.compact(_.map(this._getRandom(dsort, index), (mval) => {
return dsort[mval];
}))
};
console.log(`setSortToRedis, index: ${index}, len: ${len}, id: ${el.id}, keywords: ${el.keyword}, key: ${key}`);
return this.redis.setAsync(key, JSON.stringify(tdata));
});
}).then((d) => {
dsort = [];
return true;
});
});
}
_getRandom(dsort, index) {
return _.shuffle(_.difference(_.keys(dsort), [`${index}`])).slice(0, 12);
}
}
module.exports = expandModel;
... ...
... ... @@ -20,9 +20,10 @@
<label style="margin-right:20px;"><input id="allSelected" type="checkbox" style="margin-right:5px;">全选</label>
<a class="btn btn-default deleteAll">删除</a>
<a class="btn btn-default" href="javascript:sendUrl();">推送百度</a>
<a class="btn btn-default rand-words" href="javascript:void(0);">关键词随机关联</a>
<div class="input-append pull-right">
<form id="query-form" action="/keywords/expand" class="query-form" method="get">
<div class="btn-group">
<!-- <div class="btn-group">
<button class="btn" type="button">
品牌
</button>
... ... @@ -36,7 +37,7 @@
</button>
<input class="span2 query-key" type="text" name="sort" value="{{brand}}">
<ul class="dropdown-menu sort-menu"></ul>
</div>
</div> -->
<div class="btn-group">
<button class="btn tn-default dropdown-toggle" data-toggle="dropdown">
... ... @@ -179,6 +180,13 @@
right: 0px;
left: 59px;
}
.panel-body .rand-words.is-run {
background: #f5f5f5 url(/images/loaders/loader1.gif) no-repeat center;
border: 1px solid #ddd;
color: #aca899;
cursor: no-drop;
}
</style>
<script>
var currentPage=1,pageCount=10,pageTotal;
... ... @@ -223,6 +231,32 @@
}
});
//随机生成关联词
$('.rand-words').click(function() {
var that = $(this);
if (that.hasClass('is-run')) {
return true;
}
that.addClass('is-run');
layer.msg('关键词随机关联正在生成中...');
$.ajax({
  url: '/keywords/expand/randWords',
  timeout: 1000,
  type: 'post',
  data: {},
  dataType: 'json',
  success: function(data){
if (data.code === 200) {
layer.msg('关键词关联成功');
that.removeClass('is-run');
}
  }
});
});
// 推送百度
function sendUrl() {
confirm('确定推送?');
... ...