Authored by yyq

Merge branch 'feature/keywords' of git.yoho.cn:OPENTECH/yoho-node-ci into feature/keywords

... ... @@ -8,65 +8,55 @@
const Router = require('koa-router');
const moment = require('moment');
const redisStore = require('koa-redis');
const redis = require("redis");
const client = redis.createClient();
const multi = client.multi();
client.on("error", function (err) {
console.log("Error " + err);
});
const pager = require('../utils/pager');
const r = new Router();
const multiAsync=()=>{
return new Promise(function (resolve, reject) {
multi.exec(async function(errors, results) {
if(errors)reject(errors);
let res = results;
resolve(res);
});
const multiAsync=(multi)=>{
return multi.execAsync().then(function(res) {
return res;
});
}
//查看
const getDataList = async(key, page)=>{
let start = (page-1)*10;
let end = (page)*10-1;
multi.lrange(key, start, end);
multi.LLEN(key);
return multiAsync();
const getDataList = async(ctx, key, page, limit)=>{
let multi = ctx.redis.multi();
let start = (page-1)*limit;
let end = (page)*limit-1;
multi.lrange(key, start, end).LLEN(key);
return multiAsync(multi);
}
const getDataValues = async(arr)=>{
const getDataValues = async(ctx, arr)=>{
let multi = ctx.redis.multi();
arr.forEach(item=>{
multi.get(item);
});
return multiAsync();
return multiAsync(multi);
}
//删除
const delDataValues = async(arr)=>{
const delDataValues = async(ctx, arr)=>{
let multi = ctx.redis.multi();
arr.forEach(item=>{
let key = `keywords_mana:${item}`;
multi.del(key);
multi.lrem('keywords_mana_list', 1, key);
multi.del(key).lrem('keywords_mana_list', 1, key);
});
return multiAsync();
return multiAsync(multi);
}
//添加
const addDataValues = async(value)=>{
const addDataValues = async(ctx, value)=>{
let multi = ctx.redis.multi();
let key=`keywords_mana:${value}`;
multi.set(key, value);
multi.lrem('keywords_mana_list', 1, key);
multi.lpush('keywords_mana_list', key);
multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
return multiAsync();
return multiAsync(multi);
}
//编辑
const editDataValues = async(value, oldValue)=>{
const editDataValues = async(ctx, value, oldValue)=>{
try{
await delDataValues([oldValue]);
await addDataValues(value);
await delDataValues(ctx, [oldValue]);
await addDataValues(ctx, value);
return true;
}catch(e){
return false;
... ... @@ -74,34 +64,32 @@ const editDataValues = async(value, oldValue)=>{
}
//搜索
const searchDataValues = async(value)=>{
const searchDataValues = async(ctx, value)=>{
let multi = ctx.redis.multi();
let key=`keywords_mana:*${value}*`;
multi.KEYS(key);
return multiAsync();
return multiAsync(multi);
}
r.get('/', async(ctx) => {
await ctx.render('action/keywords');
});
r.get('/getKeywords', async(ctx) => {
let q = ctx.request.query;
let r = await getDataList("keywords_mana_list", q.currentPage, q.pageCount);
let resData = {};
let q = ctx.request.query,
page = parseInt(`0${ctx.query.page}`, 10) || 1,
limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
let r = await getDataList(ctx, "keywords_mana_list", page, limit);
let result = [];
if(r[0].length)result = await getDataValues(r[0]);
ctx.body = {
code: 200,
message: 'success',
data: result,
total:r[1]
};
if(r[0].length)result = await getDataValues(ctx, r[0]);
let total = r[1];
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
resData.tabs = result;
await ctx.render('action/keywords', resData);
});
r.get('/delKeywords', async(ctx) => {
let q = ctx.request.query;
let keys = JSON.parse(q['keywords']);
try{
await delDataValues(keys);
await delDataValues(ctx, keys);
ctx.body = {
code: 200,
message: 'success',
... ... @@ -120,7 +108,7 @@ r.get('/addKeywords', async(ctx) => {
let q = ctx.request.query;
let key = q['keywords'];
try{
await addDataValues(key);
await addDataValues(ctx, key);
ctx.body = {
code: 200,
message: 'success',
... ... @@ -139,7 +127,7 @@ r.get('/editKeywords', async(ctx) => {
let q = ctx.request.query;
let key = JSON.parse(q['keywords']);
try{
await editDataValues(key[0], key[1]);
await editDataValues(ctx, key[0], key[1]);
ctx.body = {
code: 200,
message: 'success',
... ... @@ -157,9 +145,9 @@ r.get('/editKeywords', async(ctx) => {
r.get('/searchKeywords', async(ctx) => {
let q = ctx.request.query;
let key = q['association'];
let r = await searchDataValues(key);
let r = await searchDataValues(ctx, key);
let result = [];
if(r[0].length)result = await getDataValues(r[0]);
if(r[0].length)result = await getDataValues(ctx, r[0]);
ctx.body = {
code: 200,
message: 'success',
... ...
... ... @@ -45,15 +45,29 @@
<div class="panel panel-default">
<div class="panel-body">
<table id="table-oper-log" class="table table-striped table-bordered building-table">
<thead><tr><th>ID</th><th>关键词</th><th>操作</th></tr></thead>
{{# tabs}}
<tr><td><label><input type="checkbox">{{@index}}</label></td><td><input class="values" value="{{.}}" disabled></td><td><button class="btn btn-default nostyle edit">编辑</button><button class="btn btn-default nostyle delete">删除</button></td></tr>
{{/ tabs}}
</table>
</div>
</div>
</div>
<ul class="pager">
<li><a class="privious pages">上一页</a></li>
<li><a class="next pages">下一页</a></li>
{{# pager}}
<div class="text-right">
<ul class="pagination">
{{# prePage}}
<li><a href="{{url}}">上一页</a></li>
{{/ prePage}}
{{# pages}}
<li class="{{#unless url}}disabled {{/unless}}{{#if cur}}active{{/if}}"><a {{#if url}}href="{{url}}"{{^}}href="javascript:;"{{/if}}>{{num}}</a></li>
{{/ pages}}
{{# nextPage}}
<li><a href="{{url}}">下一页</a></li>
{{/ nextPage}}
</ul>
</div>
{{/ pager}}
<style>
.nostyle{
background: transparent;
... ... @@ -76,7 +90,7 @@
bottom: 0;
margin:auto;
}
.pager{
.text-right{
float: right;
margin-right: 20px;
margin-top:0;
... ... @@ -89,8 +103,8 @@
var currentPage=1,pageCount=10,pageTotal;
var tabHead='<thead><tr><th>ID</th><th>关键词</th><th>操作</th></tr></thead>';
//展示列表
$(document).on('ready pjax:success', function() {
$.get('/keywords/getKeywords?', {currentPage:currentPage, pageCount:pageCount}, function(data){
/**$(document).on('ready pjax:success', function() {
$.get('/keywords/getKeywords', {page:currentPage, limit:pageCount}, function(data){
var html='';
let datas=data.data;
if(datas){
... ... @@ -130,7 +144,7 @@
}
});
});
});**/
//删除
$(document).on('click', '.delete', function(){
var val=$($(this).parents('tr').find('input')[1]).val();
... ... @@ -206,7 +220,7 @@
html+=tableHtml(item, index);
});
$('#table-oper-log').html(tabHead+html);
pageTotal=Math.ceil(data.total/pageCount);
$('.text-right').hide();
}
});
});
... ...