diff --git a/app.js b/app.js index b6d409d..f5d1667 100644 --- a/app.js +++ b/app.js @@ -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' diff --git a/database/db.js b/database/db.js index ea0c843..cdf838d 100644 --- a/database/db.js +++ b/database/db.js @@ -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; diff --git a/public/scripts/controllers/tags-controller.js b/public/scripts/controllers/tags-controller.js index 2fdaeff..4379751 100644 --- a/public/scripts/controllers/tags-controller.js +++ b/public/scripts/controllers/tags-controller.js @@ -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) diff --git a/public/scripts/services/bookmark-service.js b/public/scripts/services/bookmark-service.js index b700ff5..5e6a422 100644 --- a/public/scripts/services/bookmark-service.js +++ b/public/scripts/services/bookmark-service.js @@ -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(); diff --git a/public/views/tags.html b/public/views/tags.html index 65ea7a7..0e1b291 100644 --- a/public/views/tags.html +++ b/public/views/tags.html @@ -1 +1,58 @@ -

{{ tag }}

+
+
+
+
+
+ {{ tag.name }} x {{ tag.cnt }} +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
标题链接点击次数创建日期最后点击分类操作
+ + + {{ bookmark.title }} + + + {{ bookmark.url }} + {{ bookmark.click_count }}{{ bookmark.created_at }}{{ bookmark.last_click }} +
+ {{ tag.name }} +
+
+ + + +
+ +
diff --git a/routes/api.js b/routes/api.js index 73f8793..aac5b6a 100644 --- a/routes/api.js +++ b/routes/api.js @@ -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) {