Authored by yyq

友链管理

... ... @@ -459,6 +459,166 @@ const category = {
}
}
const friendLink = {
index: async(ctx, next) => {
let type = ctx.query.type || 'text';
let resData = {
typeList: [
{type: 'text', name: '文字友链'},
{type: 'img', name: '图片友链'},
]
};
let typeObj = _.find(resData.typeList, {'type': type});
if (_.isEmpty(typeObj)) {
resData.typeName = '文字友链';
type = 'text';
} else {
resData.typeName = typeObj.name;
type = typeObj.type;
}
let res = await ctx.redis.getAsync(`friend:${type}:links`);
try {
res = JSON.parse(res);
} catch (e) {
console.error(`friend links parse error : ${JSON.stringify(e)}`)
res = [];
}
await ctx.render('action/seo_friendlink', Object.assign(resData, {
title: '友链管理',
linkList: res,
type: type
}));
},
add: async(ctx, next) => {
let result = {code: 500, message: '非法参数'};
let {type, key, sort, link} = ctx.request.body;
type = type || 'text';
if (!type) {
return ctx.response.body = result;
}
let rkey = `friend:${type}:links`;
let list = await ctx.redis.getAsync(rkey);
try {
list = JSON.parse(list);
list = list || [];
} catch (e) {
console.error(`friend links parse error : ${JSON.stringify(e)}`);
return ctx.response.body = result;
}
let linkInfo = {
sort: sort || 0,
link: link,
modify_time: Date.parse(new Date()) / 1000
}
linkInfo[type] = key;
linkInfo.cid = md5(JSON.stringify(linkInfo));
list.push(linkInfo);
await ctx.redis.setAsync(rkey, JSON.stringify(list)).then(function(res) {
if (res === 'OK') {
Object.assign(result, {code:200, message: 'success'});
} else {
Object.assign(result, {code:400, message: 'failed'});
}
});
ctx.response.body = result;
},
edit: async(ctx, next) => {
let result = {code: 500, message: '非法参数'};
let {type, key, sort, link, cid} = ctx.request.body;
type = type || 'text';
if (!type) {
return ctx.response.body = result;
}
let rkey = `friend:${type}:links`;
let list = await ctx.redis.getAsync(rkey);
try {
list = JSON.parse(list);
list = list || [];
} catch (e) {
console.error(`friend links parse error : ${JSON.stringify(e)}`);
return ctx.response.body = result;
}
_.forEach(list, value => {
if (value.cid === cid) {
Object.assign(value, {
sort: sort || 0,
link: link
});
value[type] = key;
}
});
await ctx.redis.setAsync(rkey, JSON.stringify(list)).then(function(res) {
if (res === 'OK') {
Object.assign(result, {code:200, message: 'success'});
} else {
Object.assign(result, {code:400, message: 'failed'});
}
});
ctx.response.body = result;
},
delete: async(ctx, next) => {
let result = {code: 500, message: '非法参数'};
let delArr = ctx.request.body.list;
let type = _.get(delArr, '[0].type');
if (!delArr || !delArr.length || !type) {
return ctx.response.body = result;
}
let rkey = `friend:${type}:links`;
let list = await ctx.redis.getAsync(rkey);
try {
list = JSON.parse(list);
list = list || [];
} catch (e) {
console.error(`friend links parse error : ${JSON.stringify(e)}`);
return ctx.response.body = result;
}
_.remove(list, function(n) {
return _.findIndex(delArr, {cid: n.cid}) > -1;
});
await ctx.redis.setAsync(rkey, JSON.stringify(list)).then(function(res) {
if (res === 'OK') {
Object.assign(result, {code:200, message: 'success'});
} else {
Object.assign(result, {code:400, message: 'failed'});
}
});
ctx.response.body = result;
},
};
r.get('/', tdk.index);
r.get('/tdk', tdk.index);
r.post('/tdk/add', tdk.add);
... ... @@ -479,4 +639,10 @@ r.post('/category/add', category.add);
r.post('/category/edit', category.edit);
r.post('/category/delete', category.delete);
// 品类描述管理
r.get('/friendlink', friendLink.index);
r.post('/friendlink/add', friendLink.add);
r.post('/friendlink/edit', friendLink.edit);
r.post('/friendlink/delete', friendLink.delete);
module.exports = r;
... ...
<style>
.seo-friendlink-page ul {
padding: 0;
}
.seo-friendlink-page li {
list-style: none;
}
.seo-friendlink-page .query-form {
font-size: 0;
}
.seo-friendlink-page .query-form .btn-group {
margin-bottom: 0;
}
.seo-friendlink-page .query-form .dropdown-toggle {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.seo-friendlink-page .query-form .query-submit-btn {
height: 39px;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.seo-friendlink-page .dropdown-menu {
margin-left: -38px;
}
.seo-friendlink-page .query-key {
width: 300px;
height: 39px;
font-size: 14px;
vertical-align: middle;
outline: none;
}
.seo-friendlink-page .text-limit {
max-width: 78px;
display: inline-block;
vertical-align: top;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.seo-friendlink-page .pagination {
margin: 0;
}
.seo-friendlink-page #table-friendlink th,
.seo-friendlink-page #table-friendlink td {
height: 36px;
}
.seo-friendlink-page #pop{
width:500px;
height: 350px;
background: #fff;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin:auto;
}
.seo-friendlink-page #pop .cover-title {
position: absolute;
background: #fff;
margin-top: -34px;
}
.seo-friendlink-page #pop .control-label {
width: 60px;
text-align: right;
}
.seo-friendlink-page #pop li {
padding: 6px 0;
}
.seo-friendlink-page #pop select,
.seo-friendlink-page #pop input {
height: 30px;
width: 200px;
}
.seo-friendlink-page #pop textarea {
height: 70px;
vertical-align: text-top;
border-color: #ccc;
resize: none;
}
.seo-friendlink-page #pop .full-w {
width: 390px;
}
.seo-friendlink-page #pop .controls {
display: inline-block;
}
.seo-friendlink-page #pop .err-tip {
color: #a94442;
}
</style>
<div class="pageheader">
<div class="media">
<div class="pageicon pull-left">
<i class="fa fa-th-list"></i>
</div>
<div class="media-body">
<ul class="breadcrumb">
<li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
<li><a href="/seo/friendlink">{{title}}</a></li>
<li>{{typeName}}</li>
</ul>
<h4>{{title}}</h4>
</div>
</div>
<!-- media -->
</div>
<!-- pageheader -->
<div class="contentpanel seo-friendlink-page" style="padding-bottom:0;">
<div class="panel panel-default">
<div class="panel-body">
<label style="margin-right:20px;"><input type="checkbox" id="check-all" style="margin-right:5px;">全选</label>
<a data-toggle="modal" href="#pop" class="btn btn-default" style="margin-right:10px;">增加</a>
<button class="btn btn-default delete-all" type="submit">删除</button>
<div class="input-append pull-right">
<form id="query-form" action="/seo/friendlink" class="query-form" method="get">
<div class="btn-group">
<button class="btn dropdown-toggle" data-toggle="dropdown">
{{typeName}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
{{# typeList}}
<li><a href="?type={{type}}">{{name}}</a></li>
{{/ typeList}}
</ul>
</div>
<input type="hidden" name="type" value="{{type}}">
<input class="span2 query-key hide" type="text" name="query" value="{{query}}">
<button class="btn query-submit-btn hide" type="submit">搜索</button>
</form>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<table id="table-friendlink" class="table table-striped table-bordered responsive">
<thead>
<tr>
<th class="text-center" width="60">ID</th>
<th class="text-center" width="160">网站信息</th>
<th class="text-center">链接</th>
<th class="text-center">排序</th>
<th class="text-center" width="120">操作</th>
</tr>
</thead>
<tbody>
{{#each linkList}}
<tr data-type="{{../type}}" data-key="{{text}}{{img}}" data-cid="{{cid}}" data-link="{{link}}" data-sort="{{sort}}">
<td class="text-center"><input type="checkbox" style="margin-right:5px;">{{id}}</td>
<td>{{text}}</td>
<td>{{link}}</td>
<td>{{sort}}</td>
<td class="text-center">
<a href="#pop" class="edit-btn" data-toggle="modal">编辑</a>
<a href="javascript:;" class="del-btn">删除</a>
</td>
</tr>
{{/each}}
{{#unless linkList}}
<tr>
<td class="text-center" colspan="6">暂无数据</td>
</tr>
{{/unless}}
</tbody>
</table>
{{# pager}}
<div class="text-right">
{{#if pages}}
<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>
{{/if}}
</div>
{{/ pager}}
</div>
</div>
<div id="pop" class="modal fade in" style="display: none;">
<div class="modal-header">
<a class="close clear-input" data-dismiss="modal">×</a>
<h4>添加友链</h4>
<h4 class="cover-title"></h4>
</div>
<div class="modal-body">
<ul>
<li>
<label class="control-label" for="select-type">类型:</label>
<div class="controls">
<select id="select-type">
{{# typeList}}
<option value="{{type}}">{{name}}</option>
{{/ typeList}}
</select>
</div>
</li>
<li>
<label class="control-label key-label" for="input-key">名称:</label>
<div class="controls">
<input type="text" id="input-key">
</div>
</li>
<li>
<label class="control-label" for="input-link">链接:</label>
<div class="controls">
<input type="text" id="input-link" class="full-w">
</div>
</li>
<li>
<label class="control-label" for="input-sort">排序:</label>
<div class="controls">
<input type="text" id="input-sort" class="full-w" placeholder="数字越大排序越靠前">
</div>
</li>
</ul>
</div>
<div class="modal-footer">
<span class="err-tip"></span>
<a class="btn clear-input close-pop-btn" data-dismiss="modal">关闭</a>
<a class="btn btn-primary sure-btn">确定</a>
</div>
</div>
</div>
<script>
var pop = {
ltList: {
text: '名称:',
img: '图片:'
},
init: function() {
var that = this;
this.$base = $('#pop');
this.$popTitle = $('.cover-title', this.$base);
this.$keyLabel = $('.key-label', this.$base);
this.$type = $('#select-type', this.$base);
this.$key = $('#input-key', this.$base);
this.$link = $('#input-link', this.$base);
this.$sort = $('#input-sort', this.$base);
this.$closeBtn = $('.close-pop-btn', this.$base);
this.$errTip = $('.err-tip', this.$base);
this.$base.on('change', '#select-type', function() {
that.$keyLabel.text(that.ltList[$(this).val()] || that.ltList.skn);
}).on('click', '.clear-input', function() {
that.clearInput();
}).on('click', '.sure-btn', function() {
var data;
if (that.saving) {
return;
}
data = that.packReqData();
if (!data) {
that.$errTip.text('请填写完整友链信息');
return;
}
that.saving = true;
$.ajax({
url: that.editInfo ? '/seo/friendlink/edit' : '/seo/friendlink/add',
type: 'POST',
data: data,
}).done(function(res) {
if (res.code === 200) {
history.go(0);
}
}).always(function() {
that.saving = false;
});
});
},
clearInput: function() {
this.editInfo = false;
this.$popTitle.empty();
this.$errTip.empty();
this.$type.val('text').change();
this.$type.removeAttr('disabled');
$('input, textarea', this.$base).val('');
},
fillInput(info) {
console.log(info);
if (info) {
this.editInfo = info;
this.$popTitle.text('编辑友链');
this.$type.val(info.type).change().attr('disabled', true);
this.$key.val(info.key);
this.$link.val(info.link);
this.$sort.val(info.sort);
}
this.$base.show();
},
packReqData() {
var data = {
type: this.$type.val(),
key: $.trim(this.$key.val()),
link: $.trim(this.$link.val()),
sort: $.trim(this.$sort.val())
};
var i;
if (this.editInfo) {
data.cid = this.editInfo.cid;
}
for (i in data) {
if (data.hasOwnProperty(i) && !data[i]) {
return false;
}
}
return data;
},
close() {
this.$closeBtn.trigger('click');
}
};
$(function(){
var $checkboxs = $('#table-friendlink :checkbox');
var deling;
pop.init();
function delTdk(data) {
deling = true;
$.ajax({
url: '/seo/friendlink/delete',
type: 'POST',
data: {list: data},
}).done(function(res) {
if (res.code === 200) {
history.go(0);
}
}).always(function() {
deling = false;
});
};
$('#check-all').click(function() {
if ($(this).attr('checked') === 'checked') {
$checkboxs.attr('checked', 'checked');
} else {
$checkboxs.removeAttr('checked');
}
});
$('.delete-all').click(function() {
var arr = [];
$('#table-friendlink :checkbox:checked').each(function(){
arr.push($(this).closest('tr').data());
});
arr.length ? delTdk(arr) : false;
});
$('#table-friendlink').on('click', '.edit-btn', function(e) {
pop.fillInput($(e.target).closest('tr').data());
}).on('click', '.del-btn', function(e) {
var data = $(e.target).closest('tr').data();
delTdk([data]);
});
});
</script>
... ...
... ... @@ -76,7 +76,7 @@
<li><a href="/keywords/expand"> <span>关键词管理</span></a></li>
<li><a href="/seo/tdk"><span>TDK管理</span></a></li>
<li><a href="/seo/category"><span>品类描述管理</span></a></li>
<li><a href="/seo/tdk"><span>友链管理</span></a></li>
<li><a href="/seo/friendlink"><span>友链管理</span></a></li>
</ul>
</li>
... ...