Authored by yyq

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

@@ -8,65 +8,55 @@ @@ -8,65 +8,55 @@
8 8
9 const Router = require('koa-router'); 9 const Router = require('koa-router');
10 const moment = require('moment'); 10 const moment = require('moment');
11 -const redisStore = require('koa-redis');  
12 -const redis = require("redis");  
13 -const client = redis.createClient();  
14 -const multi = client.multi();  
15 -  
16 -client.on("error", function (err) {  
17 - console.log("Error " + err);  
18 -}); 11 +const pager = require('../utils/pager');
19 12
20 const r = new Router(); 13 const r = new Router();
21 14
22 -const multiAsync=()=>{  
23 - return new Promise(function (resolve, reject) {  
24 - multi.exec(async function(errors, results) {  
25 - if(errors)reject(errors);  
26 - let res = results;  
27 - resolve(res);  
28 - }); 15 +const multiAsync=(multi)=>{
  16 + return multi.execAsync().then(function(res) {
  17 + return res;
29 }); 18 });
30 } 19 }
31 20
32 //查看 21 //查看
33 -const getDataList = async(key, page)=>{  
34 - let start = (page-1)*10;  
35 - let end = (page)*10-1;  
36 - multi.lrange(key, start, end);  
37 - multi.LLEN(key);  
38 - return multiAsync(); 22 +const getDataList = async(ctx, key, page, limit)=>{
  23 + let multi = ctx.redis.multi();
  24 + let start = (page-1)*limit;
  25 + let end = (page)*limit-1;
  26 + multi.lrange(key, start, end).LLEN(key);
  27 + return multiAsync(multi);
39 } 28 }
40 29
41 -const getDataValues = async(arr)=>{ 30 +const getDataValues = async(ctx, arr)=>{
  31 + let multi = ctx.redis.multi();
42 arr.forEach(item=>{ 32 arr.forEach(item=>{
43 multi.get(item); 33 multi.get(item);
44 }); 34 });
45 - return multiAsync(); 35 + return multiAsync(multi);
46 } 36 }
47 //删除 37 //删除
48 -const delDataValues = async(arr)=>{ 38 +const delDataValues = async(ctx, arr)=>{
  39 + let multi = ctx.redis.multi();
49 arr.forEach(item=>{ 40 arr.forEach(item=>{
50 let key = `keywords_mana:${item}`; 41 let key = `keywords_mana:${item}`;
51 - multi.del(key);  
52 - multi.lrem('keywords_mana_list', 1, key); 42 + multi.del(key).lrem('keywords_mana_list', 1, key);
53 }); 43 });
54 - return multiAsync(); 44 + return multiAsync(multi);
55 } 45 }
56 //添加 46 //添加
57 -const addDataValues = async(value)=>{ 47 +const addDataValues = async(ctx, value)=>{
  48 + let multi = ctx.redis.multi();
58 let key=`keywords_mana:${value}`; 49 let key=`keywords_mana:${value}`;
59 multi.set(key, value); 50 multi.set(key, value);
60 - multi.lrem('keywords_mana_list', 1, key);  
61 - multi.lpush('keywords_mana_list', key); 51 + multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
62 52
63 - return multiAsync(); 53 + return multiAsync(multi);
64 } 54 }
65 //编辑 55 //编辑
66 -const editDataValues = async(value, oldValue)=>{ 56 +const editDataValues = async(ctx, value, oldValue)=>{
67 try{ 57 try{
68 - await delDataValues([oldValue]);  
69 - await addDataValues(value); 58 + await delDataValues(ctx, [oldValue]);
  59 + await addDataValues(ctx, value);
70 return true; 60 return true;
71 }catch(e){ 61 }catch(e){
72 return false; 62 return false;
@@ -74,34 +64,32 @@ const editDataValues = async(value, oldValue)=>{ @@ -74,34 +64,32 @@ const editDataValues = async(value, oldValue)=>{
74 64
75 } 65 }
76 //搜索 66 //搜索
77 -const searchDataValues = async(value)=>{ 67 +const searchDataValues = async(ctx, value)=>{
  68 + let multi = ctx.redis.multi();
78 let key=`keywords_mana:*${value}*`; 69 let key=`keywords_mana:*${value}*`;
79 multi.KEYS(key); 70 multi.KEYS(key);
80 - return multiAsync(); 71 + return multiAsync(multi);
81 } 72 }
82 73
83 r.get('/', async(ctx) => { 74 r.get('/', async(ctx) => {
84 - await ctx.render('action/keywords');  
85 -});  
86 -  
87 -r.get('/getKeywords', async(ctx) => {  
88 - let q = ctx.request.query;  
89 - let r = await getDataList("keywords_mana_list", q.currentPage, q.pageCount); 75 + let resData = {};
  76 + let q = ctx.request.query,
  77 + page = parseInt(`0${ctx.query.page}`, 10) || 1,
  78 + limit = parseInt(`0${ctx.query.limit}`, 10) || 10;
  79 + let r = await getDataList(ctx, "keywords_mana_list", page, limit);
90 let result = []; 80 let result = [];
91 - if(r[0].length)result = await getDataValues(r[0]);  
92 - ctx.body = {  
93 - code: 200,  
94 - message: 'success',  
95 - data: result,  
96 - total:r[1]  
97 - }; 81 + if(r[0].length)result = await getDataValues(ctx, r[0]);
  82 + let total = r[1];
  83 + resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
  84 + resData.tabs = result;
  85 + await ctx.render('action/keywords', resData);
98 }); 86 });
99 87
100 r.get('/delKeywords', async(ctx) => { 88 r.get('/delKeywords', async(ctx) => {
101 let q = ctx.request.query; 89 let q = ctx.request.query;
102 let keys = JSON.parse(q['keywords']); 90 let keys = JSON.parse(q['keywords']);
103 try{ 91 try{
104 - await delDataValues(keys); 92 + await delDataValues(ctx, keys);
105 ctx.body = { 93 ctx.body = {
106 code: 200, 94 code: 200,
107 message: 'success', 95 message: 'success',
@@ -120,7 +108,7 @@ r.get('/addKeywords', async(ctx) => { @@ -120,7 +108,7 @@ r.get('/addKeywords', async(ctx) => {
120 let q = ctx.request.query; 108 let q = ctx.request.query;
121 let key = q['keywords']; 109 let key = q['keywords'];
122 try{ 110 try{
123 - await addDataValues(key); 111 + await addDataValues(ctx, key);
124 ctx.body = { 112 ctx.body = {
125 code: 200, 113 code: 200,
126 message: 'success', 114 message: 'success',
@@ -139,7 +127,7 @@ r.get('/editKeywords', async(ctx) => { @@ -139,7 +127,7 @@ r.get('/editKeywords', async(ctx) => {
139 let q = ctx.request.query; 127 let q = ctx.request.query;
140 let key = JSON.parse(q['keywords']); 128 let key = JSON.parse(q['keywords']);
141 try{ 129 try{
142 - await editDataValues(key[0], key[1]); 130 + await editDataValues(ctx, key[0], key[1]);
143 ctx.body = { 131 ctx.body = {
144 code: 200, 132 code: 200,
145 message: 'success', 133 message: 'success',
@@ -157,9 +145,9 @@ r.get('/editKeywords', async(ctx) => { @@ -157,9 +145,9 @@ r.get('/editKeywords', async(ctx) => {
157 r.get('/searchKeywords', async(ctx) => { 145 r.get('/searchKeywords', async(ctx) => {
158 let q = ctx.request.query; 146 let q = ctx.request.query;
159 let key = q['association']; 147 let key = q['association'];
160 - let r = await searchDataValues(key); 148 + let r = await searchDataValues(ctx, key);
161 let result = []; 149 let result = [];
162 - if(r[0].length)result = await getDataValues(r[0]); 150 + if(r[0].length)result = await getDataValues(ctx, r[0]);
163 ctx.body = { 151 ctx.body = {
164 code: 200, 152 code: 200,
165 message: 'success', 153 message: 'success',
@@ -45,15 +45,29 @@ @@ -45,15 +45,29 @@
45 <div class="panel panel-default"> 45 <div class="panel panel-default">
46 <div class="panel-body"> 46 <div class="panel-body">
47 <table id="table-oper-log" class="table table-striped table-bordered building-table"> 47 <table id="table-oper-log" class="table table-striped table-bordered building-table">
48 - 48 + <thead><tr><th>ID</th><th>关键词</th><th>操作</th></tr></thead>
  49 + {{# tabs}}
  50 + <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>
  51 + {{/ tabs}}
49 </table> 52 </table>
50 </div> 53 </div>
51 </div> 54 </div>
52 </div> 55 </div>
53 -<ul class="pager">  
54 - <li><a class="privious pages">上一页</a></li>  
55 - <li><a class="next pages">下一页</a></li> 56 +{{# pager}}
  57 + <div class="text-right">
  58 + <ul class="pagination">
  59 + {{# prePage}}
  60 + <li><a href="{{url}}">上一页</a></li>
  61 + {{/ prePage}}
  62 + {{# pages}}
  63 + <li class="{{#unless url}}disabled {{/unless}}{{#if cur}}active{{/if}}"><a {{#if url}}href="{{url}}"{{^}}href="javascript:;"{{/if}}>{{num}}</a></li>
  64 + {{/ pages}}
  65 + {{# nextPage}}
  66 + <li><a href="{{url}}">下一页</a></li>
  67 + {{/ nextPage}}
56 </ul> 68 </ul>
  69 + </div>
  70 +{{/ pager}}
57 <style> 71 <style>
58 .nostyle{ 72 .nostyle{
59 background: transparent; 73 background: transparent;
@@ -76,7 +90,7 @@ @@ -76,7 +90,7 @@
76 bottom: 0; 90 bottom: 0;
77 margin:auto; 91 margin:auto;
78 } 92 }
79 - .pager{ 93 + .text-right{
80 float: right; 94 float: right;
81 margin-right: 20px; 95 margin-right: 20px;
82 margin-top:0; 96 margin-top:0;
@@ -89,8 +103,8 @@ @@ -89,8 +103,8 @@
89 var currentPage=1,pageCount=10,pageTotal; 103 var currentPage=1,pageCount=10,pageTotal;
90 var tabHead='<thead><tr><th>ID</th><th>关键词</th><th>操作</th></tr></thead>'; 104 var tabHead='<thead><tr><th>ID</th><th>关键词</th><th>操作</th></tr></thead>';
91 //展示列表 105 //展示列表
92 - $(document).on('ready pjax:success', function() {  
93 - $.get('/keywords/getKeywords?', {currentPage:currentPage, pageCount:pageCount}, function(data){ 106 + /**$(document).on('ready pjax:success', function() {
  107 + $.get('/keywords/getKeywords', {page:currentPage, limit:pageCount}, function(data){
94 var html=''; 108 var html='';
95 let datas=data.data; 109 let datas=data.data;
96 if(datas){ 110 if(datas){
@@ -130,7 +144,7 @@ @@ -130,7 +144,7 @@
130 } 144 }
131 145
132 }); 146 });
133 - }); 147 + });**/
134 //删除 148 //删除
135 $(document).on('click', '.delete', function(){ 149 $(document).on('click', '.delete', function(){
136 var val=$($(this).parents('tr').find('input')[1]).val(); 150 var val=$($(this).parents('tr').find('input')[1]).val();
@@ -206,7 +220,7 @@ @@ -206,7 +220,7 @@
206 html+=tableHtml(item, index); 220 html+=tableHtml(item, index);
207 }); 221 });
208 $('#table-oper-log').html(tabHead+html); 222 $('#table-oper-log').html(tabHead+html);
209 - pageTotal=Math.ceil(data.total/pageCount); 223 + $('.text-right').hide();
210 } 224 }
211 }); 225 });
212 }); 226 });