From cfb24b1a447b06e18fc950441f987e555021ea29 Mon Sep 17 00:00:00 2001 From: luchenqun Date: Fri, 18 Nov 2016 17:53:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=81=9A=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/db.js | 10 +++- .../scripts/controllers/menus-controller.js | 1 - .../scripts/controllers/search-controller.js | 54 +++++++++++++++++-- .../scripts/directives/js-init-directive.js | 19 ++++--- public/scripts/services/bookmark-service.js | 2 +- public/views/search.html | 43 ++++++++------- routes/api.js | 3 +- 7 files changed, 97 insertions(+), 35 deletions(-) diff --git a/database/db.js b/database/db.js index 59cff0a..f43590a 100644 --- a/database/db.js +++ b/database/db.js @@ -253,9 +253,15 @@ db.getBookmarksTable = function(user_id) { } db.getBookmarksSearch = function(params) { - var search_word = params.searchWord; + var search_word = params.searchWord || 'test'; var user_id = '1'; - var sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(last_click, '%Y-%m-%d') as last_click FROM `bookmarks` WHERE user_id='" + user_id + "' AND (`title` LIKE '%"+ search_word +"%' OR `url` LIKE '%"+ search_word +"%') ORDER BY click_count DESC, created_at DESC LIMIT 0, 50"; + var sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(last_click, '%Y-%m-%d') as last_click FROM `bookmarks` WHERE 1=1"; + if (params.userRange == '1') { + if (params.userId) { + sql += "AND user_id = '"+ params.userId +"'" + } + } + sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(last_click, '%Y-%m-%d') as last_click FROM `bookmarks` WHERE user_id='" + user_id + "' AND (`title` LIKE '%"+ search_word +"%' OR `url` LIKE '%"+ search_word +"%') ORDER BY click_count DESC, created_at DESC LIMIT 0, 50"; console.log(sql); return new Promise(function(resolve, reject) { client.query(sql, (err, result) => { diff --git a/public/scripts/controllers/menus-controller.js b/public/scripts/controllers/menus-controller.js index ec62d4e..4ef1135 100644 --- a/public/scripts/controllers/menus-controller.js +++ b/public/scripts/controllers/menus-controller.js @@ -41,7 +41,6 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', 'pubSubService', * @desc 点击搜索按钮搜索书签 */ $scope.searchBookmarks = function(searchWord) { - console.log(searchWord); $state.go('search', { searchWord: searchWord, }) diff --git a/public/scripts/controllers/search-controller.js b/public/scripts/controllers/search-controller.js index 6ee8596..12750da 100644 --- a/public/scripts/controllers/search-controller.js +++ b/public/scripts/controllers/search-controller.js @@ -12,12 +12,24 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.username = ''; $scope.userRange = ''; $scope.bookmarkCount = 0; + $scope.tags = [] + + bookmarkService.getTags({ + user_id: '1', + }) + .then((data) => { + $scope.tags = data; + }) + .catch((err) => console.log('getTags err', err)); var searchParams = { searchWord: $scope.searchWord, } + if ($scope.searchWord) { + searchBookmarks(searchParams); + } else { - searchBookmarks(searchParams); + } $scope.delBookmark = function(bookmarkId) { var params = { @@ -40,11 +52,39 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi } $scope.search = function() { - var params = { - searchWord: $scope.searchWord, + var params = {} + params.userRange = $('.js-user-range').dropdown('get value'); + if (params.userRange == '1') { + var tags = $('.js-search-tags').dropdown('get value') + if (tags) { + params.tags = tags; + } + } else if ($scope.username) { + params.username = $scope.username } + if ($scope.searchWord) { + params.searchWord = $scope.searchWord; + } + + + var dateCreate = $('.js-create-date').dropdown('get value') || undefined; + if (dateCreate) { + params.dateCreate = dateCreate; + } else { + params.dateCreateBegin = $scope.dateCreateBegin; + params.dateCreateEnd = $scope.dateCreateEnd; + } + + var dateClick = $('.js-click-date').dropdown('get value') || undefined; + if (dateClick) { + params.dateClick = dateClick + } else { + params.dateClickBegin = $scope.dateClickBegin; + params.dateClickEnd = $scope.dateClickEnd; + } + searchBookmarks(params) - console.log('search..', $scope.searchWord, $scope.dateBegin, $scope.clickCount, $scope.username, $scope.userRange) + console.log('search..', params) } $scope.updateCreateDate = function() { console.log($scope.dateCreateBegin, $scope.dateCreateEnd); @@ -64,6 +104,12 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi } } + $scope.updateTagsSelect = function() { + $('.ui.dropdown.js-search-tags .text').removeClass('default'); + var text = $('.ui.dropdown.js-search-tags .text').text().replace('selected', '个分类已选'); + $('.ui.dropdown.js-search-tags .text').text(text); + } + function searchBookmarks(params) { bookmarkService.searchBookmarks(params) .then((data) => { diff --git a/public/scripts/directives/js-init-directive.js b/public/scripts/directives/js-init-directive.js index 8481acb..3255741 100644 --- a/public/scripts/directives/js-init-directive.js +++ b/public/scripts/directives/js-init-directive.js @@ -3,7 +3,7 @@ function date(date, settings) { var day = date.getDate(); var month = date.getMonth() + 1; var year = date.getFullYear(); - return year + '/' + month + '/' + day; + return year + '-' + month + '-' + day; }; app.directive('jsDataCreateInit', function($compile) { @@ -67,26 +67,31 @@ app.directive('jsDataClickInit', function($compile) { }; }); -app.directive('jsDropdownUserRangeInit', function($compile) { +app.directive('jsDropdownUserRangeInit', function($compile, $timeout) { return { restrict: 'A', link: function($scope, $element, $attrs) { $('.ui.dropdown.js-user-range').dropdown({ onChange: function(value, text, $choice) { - $scope.showTags = (value === '1'); - console.log(value, text, $choice, $scope.showTags, $scope.username); - $scope.$apply(); + $timeout(function() { + $scope.showTags = (value == '1'); + }) }, }); + $('.js-user-range').dropdown('set value', '1'); }, }; }); -app.directive('jsDropdownInit', function($compile) { +app.directive('jsDropdownTagsInit', function($compile) { return { restrict: 'A', link: function($scope, $element, $attrs) { - $('.ui.dropdown').dropdown(); + $('.ui.dropdown.js-search-tags').dropdown({ + useLabels: false + }); + + $('.ui.dropdown.js-search-tags .text').removeClass('default'); }, }; }); diff --git a/public/scripts/services/bookmark-service.js b/public/scripts/services/bookmark-service.js index 30f7f3e..b700ff5 100644 --- a/public/scripts/services/bookmark-service.js +++ b/public/scripts/services/bookmark-service.js @@ -200,7 +200,7 @@ app.factory('httpInterceptor', ['$q', '$injector', function($q, $injector) { }, responseError: function(err) { if (401 === err.status) { - toastr.warning("您需要先登录才能使用该功能", "警告"); + // toastr.warning("您需要先登录才能使用该功能", "警告"); $injector.get('$state').go('login', {}) } else { toastr.error(JSON.stringify(err), "错误"); diff --git a/public/views/search.html b/public/views/search.html index 4b5a1e8..503df24 100644 --- a/public/views/search.html +++ b/public/views/search.html @@ -8,7 +8,7 @@