diff --git a/src/controller/api.js b/src/controller/api.js index 2e3d2f9..29b54e9 100644 --- a/src/controller/api.js +++ b/src/controller/api.js @@ -235,11 +235,10 @@ module.exports = class extends Base { } // 根据书签id获取书签 - async getBookmarksByTagAction() { + async bookmarksByTagAction() { let tagId = this.get("tagId"); let showType = this.get("showType") || "createdAt"; // tagId = -1 个人定制 从自己里面取 - // tagId = -2 全局定制 从非个人里面取 let where = {}; let order = showType + ' DESC'; @@ -259,6 +258,28 @@ module.exports = class extends Base { } } + async bookmarksSearchAction() { + let condition = {}; + let keyword = this.get("keyword"); + let username = this.get("username"); + if (username) { + + } else { + condition.userId = this.ctx.state.user.id; + } + + if (keyword) { + condition.url = ['like', `%${keyword}%`]; + } + + try { + let data = await this.model('bookmarks').where(condition).page(this.get('page') || 1, this.get('pageSize') || 50).countSelect(); + this.json({ code: 0, data }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + // 点击书签 async clickBookmarkAction() { let id = this.post("id"); @@ -412,9 +433,9 @@ module.exports = class extends Base { async notesAction() { let where = {}; try { - let searchWord = this.get('searchWord'); - if (searchWord) { - where.content = ['like', `%${searchWord}%`] + let keyword = this.get('keyword'); + if (keyword) { + where.content = ['like', `%${keyword}%`] } let data = await this.model('notes').where(where).order("createdAt DESC").page(this.get('page'), this.get('pageSize')).countSelect(); this.json({ code: 0, data }); diff --git a/www/scripts/app-angular.js b/www/scripts/app-angular.js index e9d2809..739b2f1 100644 --- a/www/scripts/app-angular.js +++ b/www/scripts/app-angular.js @@ -72,7 +72,7 @@ app.config(function ($stateProvider, $urlRouterProvider, $httpProvider) { url: '/note', templateUrl: 'views/note.html', params: { - searchWord: null, + keyword: null, key: null, }, controller: 'noteCtr' @@ -81,7 +81,7 @@ app.config(function ($stateProvider, $urlRouterProvider, $httpProvider) { url: '/search', templateUrl: 'views/search.html', params: { - searchWord: null, + keyword: null, }, controller: 'searchCtr' }) diff --git a/www/scripts/controllers/menus-controller.js b/www/scripts/controllers/menus-controller.js index 089cbc3..67d6777 100644 --- a/www/scripts/controllers/menus-controller.js +++ b/www/scripts/controllers/menus-controller.js @@ -3,7 +3,7 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time $scope.login = false; /**< 是否登陆 */ $scope.selectLoginIndex = 0; /**< 默认登陆之后的选择的菜单索引,下表从 0 开始 */ $scope.selectNotLoginIndex = 0; /**< 默认未登陆之后的选择的菜单索引,下表从 0 开始 */ - $scope.searchWord = ''; /**< 搜索关键字 */ + $scope.keyword = ''; /**< 搜索关键字 */ $scope.showStyle = null; $scope.searchHistory = []; $scope.historyTypes = dataService.historyTypes; @@ -70,9 +70,9 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time * @func * @desc 点击搜索按钮搜索书签 */ - $scope.search = function (searchWord, searchOption) { - console.log('search......', searchWord); - if (!searchWord) { + $scope.search = function (keyword, searchOption) { + console.log('search......', keyword); + if (!keyword) { toastr.error('请输入搜索关键字', "错误"); return; } @@ -81,42 +81,42 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time // var searchOption = $('.js-search-option').dropdown('get value') || 0; if (searchOption == 0) { $state.go('search', { - searchWord: searchWord, + keyword: keyword, }, { reload: true, }) updateMenuActive($scope.selectLoginIndex = 0); } else if (searchOption == 1) { - $window.open('https://www.google.com.hk/#newwindow=1&safe=strict&q=' + encodeURIComponent(searchWord), '_blank'); + $window.open('https://www.google.com.hk/#newwindow=1&safe=strict&q=' + encodeURIComponent(keyword), '_blank'); } else if (searchOption == 2) { - $window.open('https://github.com/search?utf8=%E2%9C%93&q=' + encodeURIComponent(searchWord) + '&type=', '_blank'); + $window.open('https://github.com/search?utf8=%E2%9C%93&q=' + encodeURIComponent(keyword) + '&type=', '_blank'); } else if (searchOption == 3) { - $window.open('https://stackoverflow.com/search?q=' + encodeURIComponent(searchWord), '_blank'); + $window.open('https://stackoverflow.com/search?q=' + encodeURIComponent(keyword), '_blank'); } else if (searchOption == 4) { - $window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(searchWord), '_blank'); + $window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(keyword), '_blank'); } else if (searchOption == 5) { - console.log('search note, word = ', searchWord); + console.log('search note, word = ', keyword); $state.go('note', { - searchWord: searchWord, + keyword: keyword, }, { reload: true, }) updateMenuActive($scope.selectLoginIndex = dataService.LoginIndexNote); } - if (!searchWord) { + if (!keyword) { return; } var newItem = { t: searchOption, - d: searchWord, + d: keyword, } $scope.searchIcon(newItem) var delIndex = -1; $scope.searchHistory.unshift(newItem); $scope.searchHistory.forEach((item, index) => { - if (index >= 1 && item.t == searchOption && item.d == searchWord) { + if (index >= 1 && item.t == searchOption && item.d == keyword) { delIndex = index; } }) @@ -125,7 +125,7 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time } // 大于30的不保存到数据库 - if (searchWord.length <= 30) { + if (keyword.length <= 30) { saveHistory(); } } @@ -133,8 +133,8 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time $scope.searchByHistory = function (type, data, $event) { console.log("searchByHistory", type, data); $event && $event.stopPropagation(); - $scope.searchWord = data; - $('.search-item').val($scope.searchWord); + $scope.keyword = data; + $('.search-item').val($scope.keyword); $('.js-search-option').dropdown('set value', type); var types = $scope.historyTypes; diff --git a/www/scripts/controllers/note-controller.js b/www/scripts/controllers/note-controller.js index 11b759e..b3a8fc3 100644 --- a/www/scripts/controllers/note-controller.js +++ b/www/scripts/controllers/note-controller.js @@ -20,7 +20,7 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind $scope.totalPages = 0; $scope.currentPage = 1; $scope.inputPage = ''; - $scope.searchWord = $stateParams.searchWord + $scope.keyword = $stateParams.keyword $scope.key = $stateParams.key $scope.totalItems = 0; @@ -119,7 +119,7 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind $scope.content = ''; $scope.currentTagId = null; $scope.currentPage = 1; - $scope.searchWord = ''; + $scope.keyword = ''; getNotes(); } @@ -316,7 +316,7 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind var params = { page: $scope.currentPage, pageSize: perPageItems, - searchWord: $scope.searchWord, + keyword: $scope.keyword, tagId: tagId || $scope.currentTagId }; diff --git a/www/scripts/controllers/search-controller.js b/www/scripts/controllers/search-controller.js index ccb7dc5..65a4137 100644 --- a/www/scripts/controllers/search-controller.js +++ b/www/scripts/controllers/search-controller.js @@ -11,7 +11,7 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.searchBookmarks = []; // 书签数据 $scope.showSearch = false; // $scope.showTags = false; // - $scope.searchWord = ($stateParams && $stateParams.searchWord) || '' + $scope.keyword = ($stateParams && $stateParams.keyword) || '' $scope.dateCreateBegin = ''; $scope.dateCreateEnd = ''; $scope.dateClickBegin = ''; @@ -50,12 +50,12 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi }); var searchParams = { - searchWord: $scope.searchWord, + keyword: $scope.keyword, page: 1, perPageItems: perPageItems, userRange: '1', // 默认搜索自己的书签 } - if ($scope.searchWord) { + if ($scope.keyword) { searchBookmarks(searchParams); } else { toastr.warning("请输入搜索关键字再进行查询!", "提示"); @@ -69,7 +69,7 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.searchBookmarks.forEach(function (bookmark) { if (bookmark.id == id && bookmark.own) { bookmark.click_count += 1; - bookmark.last_click = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); + bookmark.lastClick = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); } }) $timeout(function () { @@ -114,7 +114,7 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.favoriteBookmark = async function (bookmark) { let id = await post("addBookmark", bookmark); - let bookmark = await get("bookmark", { id }); + bookmark = await get("bookmark", { id }); pubSubService.publish('EditCtr.inserBookmarsSuccess', bookmark); } @@ -133,8 +133,8 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi } else if ($scope.username) { params.username = $scope.username } - if ($scope.searchWord) { - params.searchWord = $scope.searchWord; + if ($scope.keyword) { + params.keyword = $scope.keyword; } var dateCreate = $('.js-create-date').dropdown('get value') || undefined; @@ -224,7 +224,7 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi }) }); - function searchBookmarks(params) { + async function searchBookmarks(params) { $scope.loading = true; $('.js-table-search').transition('hide'); if ($scope.searchHotBookmarks) { @@ -237,8 +237,8 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi id: -1, name: bookmark.created_by, // 给转存用 }] - bookmark.created_at = $filter('date')(new Date(bookmark.created_at), "yyyy-MM-dd HH:mm:ss"); - bookmark.last_click = $filter('date')(new Date(bookmark.last_click), "yyyy-MM-dd HH:mm:ss"); + bookmark.createdAt = $filter('date')(new Date(bookmark.createdAt), "yyyy-MM-dd HH:mm:ss"); + bookmark.lastClick = $filter('date')(new Date(bookmark.lastClick), "yyyy-MM-dd HH:mm:ss"); $scope.searchBookmarks.push(bookmark); }) $scope.bookmarkCount = data.totalItems; @@ -251,18 +251,13 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.loading = false; }); } else { - bookmarkService.searchBookmarks(params) - .then((data) => { - $scope.searchBookmarks = data.bookmarks; - $scope.bookmarkCount = data.totalItems; - $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); - $scope.loading = false; - transition(); - }) - .catch((err) => { - console.log('getBookmarks err', err); - $scope.loading = false; - }); + console.log(params); + let reply = await get('bookmarksSearch', params); + $scope.searchBookmarks = reply.data; + $scope.totalPages = reply.totalPages; + $scope.bookmarkCount = reply.count; + $scope.loading = false; + transition(); } } diff --git a/www/scripts/controllers/tags-controller.js b/www/scripts/controllers/tags-controller.js index 8d40770..073d60a 100644 --- a/www/scripts/controllers/tags-controller.js +++ b/www/scripts/controllers/tags-controller.js @@ -57,7 +57,7 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara showType: $scope.showType }; - let reply = await get('getBookmarksByTag', params); + let reply = await get('bookmarksByTag', params); $scope.bookmarks = reply.data; $scope.totalPages = reply.totalPages; $scope.inputPage = ''; diff --git a/www/views/search.html b/www/views/search.html index a832179..b40c6f8 100644 --- a/www/views/search.html +++ b/www/views/search.html @@ -143,19 +143,19 @@