增加了一下标签功能

This commit is contained in:
luchenqun 2017-02-03 17:26:04 +08:00
parent 24be427ce9
commit 1bb8a0c4a9
6 changed files with 240 additions and 5 deletions

2
app.js
View File

@ -32,7 +32,7 @@ app.use(session({
saveUninitialized: true, //添加这行
secret: 'ILoveYiJia', // 建议使用 128 个字符的随机字符串
cookie: {
maxAge: 2592000000,
maxAge: 1000 * 60,
},
store: new mongoStore({
url: 'mongodb://localhost/mybookmarks'

View File

@ -195,7 +195,7 @@ db.getUser = function(username) {
db.getTags = function(user_id) {
console.log('getTags');
var sql = "SELECT * FROM `tags` WHERE `user_id` = '" + user_id + "' ORDER BY last_use DESC";
var sql = "SELECT t.*, tb.cnt FROM `tags` as t LEFT OUTER JOIN ( SELECT `tag_id`, COUNT(tag_id) as cnt FROM tags_bookmarks GROUP BY tag_id ) tb ON t.id = tb.tag_id WHERE t.user_id = '" + user_id + "' ORDER BY last_use DESC";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
@ -289,6 +289,37 @@ db.getBookmarksTable = function(params) {
})
}
db.getBookmarksByTag = function(params) {
var tag_id = params.tagId;
params.currentPage = params.currentPage || 1;
params.perPageItems = params.perPageItems || 20;
var sql = "SELECT bookmarks.id, bookmarks.user_id, bookmarks.title, bookmarks.description, bookmarks.url, bookmarks.public, bookmarks.click_count, DATE_FORMAT(bookmarks.created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(bookmarks.last_click, '%Y-%m-%d') as last_click FROM `tags_bookmarks`, `bookmarks` WHERE tags_bookmarks.tag_id = '"+ tag_id +"' AND tags_bookmarks.bookmark_id = bookmarks.id";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
sql += " LIMIT " + (params.currentPage - 1) * params.perPageItems + ", " + params.perPageItems;
var totalItems = result.length;
console.log(totalItems, sql);
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
var bookmarksData = {
totalItems: totalItems,
bookmarks: result,
}
resolve(bookmarksData);
}
});
}
});
})
}
db.getBookmarksSearch = function(params) {
params.currentPage = params.currentPage || 1;
params.perPageItems = params.perPageItems || 20;

View File

@ -1,14 +1,92 @@
app.controller('tagsCtr', ['$scope', '$filter', 'bookmarkService', 'pubSubService', function($scope, $filter, bookmarkService, pubSubService) {
app.controller('tagsCtr', ['$scope', '$filter', '$window', 'bookmarkService', 'pubSubService', function($scope, $filter, $window, bookmarkService, pubSubService) {
console.log("Hello tagsCtr...");
getTags({});
const perPageItems = 10;
$scope.tags = []; // 书签数据
$scope.bookmarkClicked = false;
$scope.bookmarks = [];
$scope.bookmarkCount = 0;
$scope.totalPages = 0;
$scope.currentPage = 1;
$scope.inputPage = '';
$scope.currentTagId = "";
pubSubService.subscribe('MenuCtr.tags', $scope, function(event, data) {
console.log('subscribe MenuCtr.tags', data);
getTags({});
});
$scope.getBookmarks = function(tagId, currentPage) {
console.log('tags getBookmarks', tagId);
$scope.bookmarkClicked = true;
$scope.currentTagId = tagId;
$scope.tags.forEach(function(tag) {
tag.bookmarkClicked = false;
if (tag.id == tagId) {
tag.bookmarkClicked = true;
}
});
var params = {
tagId: tagId,
currentPage: currentPage,
perPageItems: perPageItems,
};
bookmarkService.getBookmarksByTag(params)
.then((data) => {
$scope.bookmarks = data.bookmarks;
$scope.bookmarkCount = data.totalItems;
$scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems);
$scope.inputPage = ''
})
.catch((err) => console.log('getTags err', err));
};
$scope.changeCurrentPage = function(currentPage) {
currentPage = parseInt(currentPage) || 0;
console.log(currentPage);
if (currentPage <= $scope.totalPages && currentPage >= 1) {
$scope.getBookmarks($scope.currentTagId, currentPage);
$scope.currentPage = currentPage;
}
}
$scope.jumpToUrl = function(url, id) {
if (!$scope.edit) {
$window.open(url, '_blank');
bookmarkService.clickBookmark({
id: id
});
}
}
$scope.delBookmark = function(bookmarkId) {
toastr.warning('功能暂未实现。。。', "警告");
return;
var params = {
id: bookmarkId
}
bookmarkService.delBookmark(params)
.then((data) => $("#" + bookmarkId).remove())
.catch((err) => {
console.log('delBookmark err ', err)
});
}
$scope.editBookmark = function(bookmarkId) {
toastr.warning('功能暂未实现。。。', "警告");
return;
pubSubService.publish('bookmarksCtr.editBookmark', {
'bookmarkId': bookmarkId
});
}
$scope.detailBookmark = function(bookmarkId) {
toastr.warning('功能暂未实现。。。', "警告");
}
function getTags(params) {
bookmarkService.getTags(params)
.then((data) => $scope.tags = data)

View File

@ -83,6 +83,20 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) {
});
return def.promise;
},
getBookmarksByTag: function(params) {
var def = $q.defer();
$http.get('/api/bookmarksByTag/', {
params: params
})
.success(function(data) {
def.resolve(data);
})
.error(function(data, status) {
def.reject('bookmarksByTag error');
});
return def.promise;
},
searchBookmarks: function(params) {
var def = $q.defer();

View File

@ -1 +1,58 @@
<p ng-repeat="tag in tags"> {{ tag }} </p>
<div class="ui segment">
<div class="ui container">
<div class="ui grid">
<div class="two wide column" ng-class="" ng-mouseover="" ng-mouseleave="" ng-repeat="tag in tags">
<div class="ui small label" ng-class="{green:tag.bookmarkClicked}" ng-click="getBookmarks(tag.id, 1)">
{{ tag.name }} x {{ tag.cnt }}
</div>
</div>
</div>
</div>
</div>
<table class="ui celled table" ng-if="bookmarkCount > 0">
<thead>
<tr>
<th>标题</th>
<th>链接</th>
<th style="width:90px;">点击次数</th>
<th style="width:100px;">创建日期</th>
<th style="width:100px;">最后点击</th>
<th style="width:150px;">分类</th>
<th style="width:105px;">操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
<td>
<img class="ui ui middle aligned tiny image" src=" http://favicon.byi.pw/?url={{bookmark.url}}" style="width:16px;height:16px">
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}">
{{ bookmark.title }}
</span>
</td>
<td ng-click="jumpToUrl(bookmark.url, bookmark.id)">
<span title="{{bookmark.url}}">{{ bookmark.url }}</span>
</td>
<td>{{ bookmark.click_count }}</td>
<td>{{ bookmark.created_at }}</td>
<td>{{ bookmark.last_click }}</td>
<td>
<div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}">
{{ tag.name }}
</div>
</td>
<td>
<img class="ui mini spaced image" style="width:16px;height:16px;margin:0 5px" ng-src="./images/delete.png" ng-click="delBookmark(bookmark.id)">
<img class="ui mini spaced image" style="width:16px;height:16px;margin:0 5px" ng-src="./images/edit-bookmark.png" ng-click="editBookmark(bookmark.id)">
<img class="ui mini spaced image" style="width:16px;height:16px;margin:0 5px" ng-src="./images/detail.png" ng-click="detailBookmark(bookmark.id)">
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="7">
<pagination></pagination>
</th>
</tr>
</tfoot>
</table>

View File

@ -217,6 +217,61 @@ api.get('/bookmarks', function(req, res) {
}
});
api.get('/bookmarksByTag', function(req, res) {
console.log('hello bookmarksByTag', JSON.stringify(req.query), req.session.username);
if (!req.session.user) {
res.send(401);
return;
}
var userId = req.session.user.id;
var params = req.query;
var bookmarks = [];
var tagsBookmarks = [];
var totalItems = 0;
var totalItems = 0;
var sendData = {
totalItems: totalItems,
bookmarks: []
}
db.getBookmarksByTag(params)
.then((bookmarksData) => {
bookmarks = bookmarksData.bookmarks;
totalItems = bookmarksData.totalItems;
var bookmarkIds = bookmarks.map((bookmark) => bookmark.id);
return db.getTagsBookmarks(bookmarkIds);
})
.then((tbs) => {
tagsBookmarks = tbs;
return db.getTags(userId);
})
.then((tags) => {
var data = [];
// 获取每个书签的所有分类标签
bookmarks.forEach(function(bookmark) {
var bookmarkTags = [];
tagsBookmarks.forEach(function(tb) {
if (tb.bookmark_id == bookmark.id) {
tags.forEach(function(tag) {
if (tb.tag_id == tag.id) {
bookmarkTags.push(tag)
}
})
}
});
bookmark.tags = bookmarkTags;
data.push(bookmark);
})
sendData.totalItems = totalItems;
sendData.bookmarks = data;
res.json(sendData);
})
.catch((err) => console.log('bookmarks table or card err', err))
});
api.get('/searchBookmarks', function(req, res) {
console.log('hello searchBookmarks', JSON.stringify(req.query), req.session.username);
if (!req.session.user) {