From 23b4ed211a74eaef2641a3109f6278f9611d5897 Mon Sep 17 00:00:00 2001 From: luchenqun Date: Mon, 6 Mar 2017 12:43:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99=E5=88=86=E7=B1=BB=E5=8A=A0=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/db.js | 58 ++++++++++++------ public/scripts/controllers/tags-controller.js | 27 ++++++++- public/views/tags.html | 18 +++--- routes/api.js | 59 ++++++++++--------- 4 files changed, 105 insertions(+), 57 deletions(-) diff --git a/database/db.js b/database/db.js index 677b7f8..5a78164 100644 --- a/database/db.js +++ b/database/db.js @@ -52,15 +52,15 @@ Date.prototype.format = function(fmt) { //author: meizz // update delete 返回影响的行数 var db = { -} -// var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun1'"; -// client.query(sql, (err, result) => { -// if (err) { -// console.log(err); -// } else { -// console.log(result); -// } -// }); + } + // var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun1'"; + // client.query(sql, (err, result) => { + // if (err) { + // console.log(err); + // } else { + // console.log(result); + // } + // }); db.addBookmark = function(user_id, bookmark) { var insertSql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + bookmark.title + "', " + client.escape(bookmark.description) + ", '" + bookmark.url + "', '" + bookmark.public + "', '1')"; @@ -499,13 +499,13 @@ db.getBookmarksNavigate = function(tags) { // var sql = "SELECT t.id as tag_id, t.name as tag_name, b.* FROM `tags` as t LEFT OUTER JOIN tags_bookmarks as tb ON t.id = tb.tag_id LEFT OUTER JOIN bookmarks as b ON tb.bookmark_id = b.id WHERE t.user_id='" + user_id + "' ORDER BY t.id ASC, b.click_count DESC"; var sql = ""; tags.forEach((tag, index) => { - var t = 't' + tag.id; - if (index >= 1) { - sql += " UNION " - } - sql += "(SELECT * FROM ((SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.click_count DESC LIMIT 0, 16) UNION (SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.created_at DESC LIMIT 0, 16)) as " + t + " ORDER BY " + t + ".click_count DESC, " + t + ".created_at DESC)"; - }) - // console.log('getBookmarksNavigate ', sql); + var t = 't' + tag.id; + if (index >= 1) { + sql += " UNION " + } + sql += "(SELECT * FROM ((SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.click_count DESC LIMIT 0, 16) UNION (SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.created_at DESC LIMIT 0, 16)) as " + t + " ORDER BY " + t + ".click_count DESC, " + t + ".created_at DESC)"; + }) + // console.log('getBookmarksNavigate ', sql); return new Promise(function(resolve, reject) { client.query(sql, (err, result) => { @@ -567,16 +567,38 @@ db.getBookmarksByTag = function(params) { 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 ORDER BY bookmarks.click_count DESC, bookmarks.created_at DESC"; + 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 %H:%i:%s') as created_at, DATE_FORMAT(bookmarks.last_click, '%Y-%m-%d %H:%i:%s') 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 { + var bookmarksClickCount, bookmarksCreatedAt, bookmarksLatestClick; + result.sort((a, b) => { + var click1 = parseInt(a.click_count); + var click2 = parseInt(b.click_count); + if (click1 > click2) { + return -1; + } else if (click1 == click2) { + return a.created_at >= b.created_at ? -1 : 1; + } else { + return 1; + } + }) + bookmarksClickCount = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems); + + result.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); + bookmarksCreatedAt = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems); + + result.sort((a, b) => a.last_click >= b.last_click ? -1 : 1); + bookmarksLatestClick = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems); + var bookmarksData = { totalItems: result.length, - bookmarks: result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems), + bookmarksClickCount: bookmarksClickCount, + bookmarksCreatedAt: bookmarksCreatedAt, + bookmarksLatestClick: bookmarksLatestClick, } resolve(bookmarksData); } diff --git a/public/scripts/controllers/tags-controller.js b/public/scripts/controllers/tags-controller.js index b9119e5..9dd937f 100644 --- a/public/scripts/controllers/tags-controller.js +++ b/public/scripts/controllers/tags-controller.js @@ -4,6 +4,8 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim const perPageItems = 20; var dialog = null; + $scope.order = [false, false, false]; + $scope.order[($stateParams && $stateParams.orderIndex) || 0] = true; $scope.loadBookmarks = false; $scope.loadTags = false; $scope.tags = []; // 书签数据 @@ -19,12 +21,25 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim $scope.newTag = ''; $scope.waitDelTag = {}; $scope.waitDelBookmark = {}; + $scope.bookmarkData = {}; pubSubService.subscribe('MenuCtr.tags', $scope, function(event, data) { console.log('subscribe MenuCtr.tags', data); getTags({}); }); + $scope.changeOrder = function(index) { + $scope.order = $scope.order.map(() => false); + $scope.order[index] = true; + if ($scope.order[0]) { + $scope.bookmarks = $scope.bookmarkData.bookmarksClickCount; + } else if ($scope.order[1]) { + $scope.bookmarks = $scope.bookmarkData.bookmarksCreatedAt; + } else { + $scope.bookmarks = $scope.bookmarkData.bookmarksLatestClick; + } + } + $scope.getBookmarks = function(tagId, currentPage) { $scope.bookmarkClicked = true; $scope.currentTagId = tagId; @@ -44,10 +59,18 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim perPageItems: perPageItems, }; $('.js-tags-table').transition('hide'); + bookmarkService.getBookmarksByTag(params) .then((data) => { - $scope.bookmarks = data.bookmarks; - $scope.bookmarkCount = data.totalItems; + $scope.bookmarkData = data; + if ($scope.order[0]) { + $scope.bookmarks = $scope.bookmarkData.bookmarksClickCount; + } else if ($scope.order[1]) { + $scope.bookmarks = $scope.bookmarkData.bookmarksCreatedAt; + } else { + $scope.bookmarks = $scope.bookmarkData.bookmarksLatestClick; + } + $scope.bookmarkCount = $scope.bookmarkData.totalItems; $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); $scope.inputPage = ''; diff --git a/public/views/tags.html b/public/views/tags.html index 7698708..db83665 100644 --- a/public/views/tags.html +++ b/public/views/tags.html @@ -51,13 +51,13 @@ - - - - - - - + + + + + + + @@ -72,8 +72,8 @@ {{ bookmark.url }} - - + +
标题链接点击次数创建日期最后点击分类操作标题链接点击次数创建日期最后点击分类操作
{{ bookmark.click_count }}{{ bookmark.created_at }}{{ bookmark.last_click }}{{ bookmark.created_at.substr(0, 10) }}{{ bookmark.last_click.substr(0, 10) }}
{{ tag.name }} diff --git a/routes/api.js b/routes/api.js index 01a03b2..e91b276 100644 --- a/routes/api.js +++ b/routes/api.js @@ -358,14 +358,18 @@ api.get('/bookmarksByTag', function(req, res) { var totalItems = 0; var totalItems = 0; var sendData = { - totalItems: totalItems, - bookmarks: [] + totalItems: 0, + bookmarksClickCount: [], + bookmarksCreatedAt: [], + bookmarksLatestClick: [], } db.getBookmarksByTag(params) .then((bookmarksData) => { - bookmarks = bookmarksData.bookmarks; - totalItems = bookmarksData.totalItems; - var bookmarkIds = bookmarks.map((bookmark) => bookmark.id); + sendData = bookmarksData; + var bookmarkIds = [] + .concat(sendData.bookmarksClickCount.map((bookmark) => bookmark.id)) + .concat(sendData.bookmarksCreatedAt.map((bookmark) => bookmark.id)) + .concat(sendData.bookmarksLatestClick.map((bookmark) => bookmark.id)) return db.getTagsBookmarks(bookmarkIds); }) .then((tbs) => { @@ -373,25 +377,24 @@ api.get('/bookmarksByTag', function(req, res) { 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); + var objectName = ['bookmarksClickCount', 'bookmarksCreatedAt', 'bookmarksLatestClick']; + objectName.forEach((name) => { + console.log(JSON.stringify(tagsBookmarks)); + sendData[name].forEach(function(bookmark, index) { + 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) + } + }) + } + }); + sendData[name][index].tags = bookmarkTags; + }) }) - sendData.totalItems = totalItems; - sendData.bookmarks = data; - res.json(sendData); }) .catch((err) => console.log('bookmarks table or card err', err)) @@ -591,13 +594,13 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) { var tags = []; item.tags.forEach((tag) => { - allTags.forEach((at) => { - if (at.name == tag) { - tags.push(at.id); - } + allTags.forEach((at) => { + if (at.name == tag) { + tags.push(at.id); + } + }) }) - }) - // 插入书签 + // 插入书签 db.addBookmark(userId, bookmark) // 插入书签 .then((bookmark_id) => { db.delBookmarkTags(bookmark_id); // 不管3721,先删掉旧的分类