Authored by yyq

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

@@ -17,10 +17,6 @@ client.on("error", function (err) { @@ -17,10 +17,6 @@ client.on("error", function (err) {
17 console.log("Error " + err); 17 console.log("Error " + err);
18 }); 18 });
19 19
20 -const {  
21 - OperationLogger  
22 -} = require('../../models');  
23 -  
24 const r = new Router(); 20 const r = new Router();
25 21
26 const multiAsync=()=>{ 22 const multiAsync=()=>{
@@ -34,8 +30,11 @@ const multiAsync=()=>{ @@ -34,8 +30,11 @@ const multiAsync=()=>{
34 } 30 }
35 31
36 //查看 32 //查看
37 -const getDataList = async(key)=>{  
38 - multi.lrange("keywords_mana_list", 0, -1); 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);
39 return multiAsync(); 38 return multiAsync();
40 } 39 }
41 40
@@ -58,12 +57,13 @@ const delDataValues = async(arr)=>{ @@ -58,12 +57,13 @@ const delDataValues = async(arr)=>{
58 const addDataValues = async(value)=>{ 57 const addDataValues = async(value)=>{
59 let key=`keywords_mana:${value}`; 58 let key=`keywords_mana:${value}`;
60 multi.set(key, value); 59 multi.set(key, value);
  60 + multi.lrem('keywords_mana_list', 1, key);
61 multi.lpush('keywords_mana_list', key); 61 multi.lpush('keywords_mana_list', key);
62 62
63 return multiAsync(); 63 return multiAsync();
64 } 64 }
65 //编辑 65 //编辑
66 -const editDataValues = async(value, oldValue)=>{console.log(value, oldValue); 66 +const editDataValues = async(value, oldValue)=>{
67 try{ 67 try{
68 await delDataValues([oldValue]); 68 await delDataValues([oldValue]);
69 await addDataValues(value); 69 await addDataValues(value);
@@ -73,6 +73,12 @@ const editDataValues = async(value, oldValue)=>{console.log(value, oldValue); @@ -73,6 +73,12 @@ const editDataValues = async(value, oldValue)=>{console.log(value, oldValue);
73 } 73 }
74 74
75 } 75 }
  76 +//搜索
  77 +const searchDataValues = async(value)=>{
  78 + let key=`keywords_mana:*${value}*`;
  79 + multi.KEYS(key);
  80 + return multiAsync();
  81 +}
76 82
77 r.get('/', async(ctx) => { 83 r.get('/', async(ctx) => {
78 await ctx.render('action/keywords'); 84 await ctx.render('action/keywords');
@@ -80,12 +86,14 @@ r.get('/', async(ctx) => { @@ -80,12 +86,14 @@ r.get('/', async(ctx) => {
80 86
81 r.get('/getKeywords', async(ctx) => { 87 r.get('/getKeywords', async(ctx) => {
82 let q = ctx.request.query; 88 let q = ctx.request.query;
83 - let r = await getDataList("keywords_mana_list");console.log(r);  
84 - let result = await getDataValues(r); 89 + let r = await getDataList("keywords_mana_list", q.currentPage, q.pageCount);
  90 + let result = [];
  91 + if(r[0].length)result = await getDataValues(r[0]);
85 ctx.body = { 92 ctx.body = {
86 code: 200, 93 code: 200,
87 message: 'success', 94 message: 'success',
88 - data: result 95 + data: result,
  96 + total:r[1]
89 }; 97 };
90 }); 98 });
91 99
@@ -145,4 +153,18 @@ r.get('/editKeywords', async(ctx) => { @@ -145,4 +153,18 @@ r.get('/editKeywords', async(ctx) => {
145 }; 153 };
146 } 154 }
147 }); 155 });
  156 +
  157 +r.get('/searchKeywords', async(ctx) => {
  158 + let q = ctx.request.query;
  159 + let key = q['association'];
  160 + let r = await searchDataValues(key);
  161 + let result = [];
  162 + if(r[0].length)result = await getDataValues(r[0]);
  163 + ctx.body = {
  164 + code: 200,
  165 + message: 'success',
  166 + data: result,
  167 + total:1
  168 + };
  169 +});
148 module.exports = r; 170 module.exports = r;
@@ -17,9 +17,13 @@ @@ -17,9 +17,13 @@
17 <div class="contentpanel project-index-page" style="padding-bottom:0;"> 17 <div class="contentpanel project-index-page" style="padding-bottom:0;">
18 <div class="panel panel-default"> 18 <div class="panel panel-default">
19 <div class="panel-body"> 19 <div class="panel-body">
20 - <label style="margin-right:20px;"><input type="checkbox" style="margin-right:5px;">全选</label> 20 + <label style="margin-right:20px;"><input id="allSelected" type="checkbox" style="margin-right:5px;">全选</label>
21 <a data-toggle="modal" href="#pop" class="btn btn-default" style="margin-right:10px;">增加</a> 21 <a data-toggle="modal" href="#pop" class="btn btn-default" style="margin-right:10px;">增加</a>
22 - <button class="btn btn-default" type="submit">删除</button> 22 + <a class="btn btn-default deleteAll">删除</a>
  23 + <button class="btn btn-default search" style="float:right;">搜索</button>
  24 + <div class="col-sm-3" style="float:right;">
  25 + <input type="text" class="form-control" id="firstname" placeholder="请输入关键词">
  26 + </div>
23 </div> 27 </div>
24 </div> 28 </div>
25 </div> 29 </div>
@@ -41,17 +45,15 @@ @@ -41,17 +45,15 @@
41 <div class="panel panel-default"> 45 <div class="panel panel-default">
42 <div class="panel-body"> 46 <div class="panel-body">
43 <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">
44 - <thead>  
45 - <tr>  
46 - <th>ID</th>  
47 - <th>关键词</th>  
48 - <th>操作</th>  
49 - </tr>  
50 - </thead> 48 +
51 </table> 49 </table>
52 </div> 50 </div>
53 </div> 51 </div>
54 </div> 52 </div>
  53 +<ul class="pager">
  54 + <li><a class="privious pages">上一页</a></li>
  55 + <li><a class="next pages">下一页</a></li>
  56 + </ul>
55 <style> 57 <style>
56 .nostyle{ 58 .nostyle{
57 background: transparent; 59 background: transparent;
@@ -74,21 +76,62 @@ @@ -74,21 +76,62 @@
74 bottom: 0; 76 bottom: 0;
75 margin:auto; 77 margin:auto;
76 } 78 }
  79 + .pager{
  80 + float: right;
  81 + margin-right: 20px;
  82 + margin-top:0;
  83 + }
  84 + .pages{
  85 + cursor: pointer;
  86 + }
77 </style> 87 </style>
78 <script> 88 <script>
  89 + var currentPage=1,pageCount=10,pageTotal;
  90 + var tabHead='<thead><tr><th>ID</th><th>关键词</th><th>操作</th></tr></thead>';
  91 + //展示列表
79 $(document).on('ready pjax:success', function() { 92 $(document).on('ready pjax:success', function() {
80 - $.get('/keywords/getKeywords', function(data){ 93 + $.get('/keywords/getKeywords?', {currentPage:currentPage, pageCount:pageCount}, function(data){
  94 + var html='';
  95 + let datas=data.data;
  96 + if(datas){
  97 + datas.forEach(function(item, index){
  98 + html+=tableHtml(item, index);
  99 + });
  100 + $('#table-oper-log').html(tabHead+html);
  101 + pageTotal=Math.ceil(data.total/pageCount);
  102 + }
  103 +
  104 + });
  105 + });
  106 + $('.privious').on('click', function() {
  107 + if(currentPage==1)return;
  108 + $.get('/keywords/getKeywords?',{currentPage:--currentPage, pageCount:pageCount}, function(data){
  109 + var html='';
  110 + let datas=data.data;
  111 + if(datas){
  112 + datas.forEach(function(item, index){
  113 + html+=tableHtml(item, index);
  114 + });
  115 + $('#table-oper-log').html(tabHead+html);
  116 + }
  117 +
  118 + });
  119 + });
  120 + $('.next').on('click', function() {
  121 + if(currentPage>=pageTotal)return;
  122 + $.get('/keywords/getKeywords?',{currentPage:++currentPage, pageCount:pageCount}, function(data){
81 var html=''; 123 var html='';
82 let datas=data.data; 124 let datas=data.data;
83 if(datas){ 125 if(datas){
84 datas.forEach(function(item, index){ 126 datas.forEach(function(item, index){
85 - html+='<tr><td><label><input type="checkbox">'+index+'</label></td><td><input value="'+item+'" disabled></td><td><button class="btn btn-default nostyle edit">编辑</button><button class="btn btn-default nostyle delete">删除</button></td></tr>'; 127 + html+=tableHtml(item, index);
86 }); 128 });
87 - $('#table-oper-log').html($('#table-oper-log').html()+html); 129 + $('#table-oper-log').html(tabHead+html);
88 } 130 }
89 131
90 }); 132 });
91 }); 133 });
  134 + //删除
92 $(document).on('click', '.delete', function(){ 135 $(document).on('click', '.delete', function(){
93 var val=$($(this).parents('tr').find('input')[1]).val(); 136 var val=$($(this).parents('tr').find('input')[1]).val();
94 var parent=$(this).parents('tr'); 137 var parent=$(this).parents('tr');
@@ -108,7 +151,14 @@ @@ -108,7 +151,14 @@
108 var val=input.val(); 151 var val=input.val();
109 $.get('/keywords/editKeywords', {keywords:JSON.stringify([val, oldValue])}, function(data){ 152 $.get('/keywords/editKeywords', {keywords:JSON.stringify([val, oldValue])}, function(data){
110 if(data.code===200){ 153 if(data.code===200){
111 - input.attr('disabled',true); 154 + var isSame=false;
  155 + $('table .values').each(function(){
  156 + if($(this)!=input){
  157 + if($(this).val()==input.val())isSame=true;
  158 + }
  159 + });
  160 + if(isSame) location.reload();
  161 + else input.attr('disabled',true);
112 } 162 }
113 }); 163 });
114 }); 164 });
@@ -122,4 +172,42 @@ @@ -122,4 +172,42 @@
122 } 172 }
123 }); 173 });
124 }); 174 });
  175 + //全选
  176 + $('#allSelected').on('click', function(){
  177 + var check=false;
  178 + if($(this).attr('checked')){
  179 + check=true;
  180 + }
  181 + $("table :checkbox").attr("checked", check);
  182 + });
  183 + $('.deleteAll').on('click',function(){
  184 + let arr=[];
  185 + $("table :checkbox:checked").each(function(item){
  186 + arr.push($($(this).parents('tr').find('input')[1]).val());
  187 + });
  188 + if(!arr.length)return;
  189 + $.get('/keywords/delKeywords', {keywords:JSON.stringify(arr)}, function(data){
  190 + if(data.code===200){
  191 + location.reload();
  192 + }
  193 + });
  194 + });
  195 + function tableHtml(item, index){
  196 + return '<tr><td><label><input type="checkbox">'+index+'</label></td><td><input class="values" value="'+item+'" disabled></td><td><button class="btn btn-default nostyle edit">编辑</button><button class="btn btn-default nostyle delete">删除</button></td></tr>';
  197 + }
  198 + //搜索
  199 + $('.search').on('click',function(){
  200 + if(!$('#firstname').val())return;
  201 + $.get('/keywords/searchKeywords', {association:$('#firstname').val()}, function(data){
  202 + var html='';
  203 + let datas=data.data;
  204 + if(data.code===200){
  205 + datas.forEach(function(item, index){
  206 + html+=tableHtml(item, index);
  207 + });
  208 + $('#table-oper-log').html(tabHead+html);
  209 + pageTotal=Math.ceil(data.total/pageCount);
  210 + }
  211 + });
  212 + });
125 </script> 213 </script>