增加一个搜索历史表
This commit is contained in:
parent
3cf3854718
commit
4b6bd2a404
|
|
@ -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.getBookmarkbyUrl = function(user_id, url) {
|
||||
var sql = "SELECT * FROM `bookmarks` WHERE `user_id` = '" + user_id + "' AND `url` = '" + url + "'"
|
||||
|
|
@ -148,7 +148,7 @@ db.getBookmarkTags = function(bookmard_id) {
|
|||
}
|
||||
|
||||
db.getBookmarkTagsNames = function(bookmard_id) {
|
||||
var sql = "SELECT tags_bookmarks.tag_id as id, tags.name FROM tags_bookmarks LEFT JOIN tags ON tags.id = tags_bookmarks.tag_id WHERE bookmark_id = '"+ bookmard_id +"'";
|
||||
var sql = "SELECT tags_bookmarks.tag_id as id, tags.name FROM tags_bookmarks LEFT JOIN tags ON tags.id = tags_bookmarks.tag_id WHERE bookmark_id = '" + bookmard_id + "'";
|
||||
return new Promise(function(resolve, reject) {
|
||||
client.query(sql, (err, result) => {
|
||||
if (err) {
|
||||
|
|
@ -325,6 +325,20 @@ db.updateShowStyle = function(userId, show_style) {
|
|||
});
|
||||
}
|
||||
|
||||
db.updateSearchHistory = function(userId, search_history) {
|
||||
var sql = "UPDATE `users` SET `search_history`=" + client.escape(search_history) + " WHERE (`id`='" + userId + "')";
|
||||
console.log('updateSearchHistory', sql);
|
||||
return new Promise(function(resolve, reject) {
|
||||
client.query(sql, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(result.affectedRows);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
db.register = function(user) {
|
||||
console.log('register');
|
||||
var sql = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('" + user.username + "', '" + user.password + "', '" + user.email + "')";
|
||||
|
|
@ -601,7 +615,7 @@ db.getBookmarksTable = function(params) {
|
|||
})
|
||||
.slice(begin, end)
|
||||
.forEach((b) => {
|
||||
var bookmark = JSON.parse(JSON.stringify(b));// 执行深度复制
|
||||
var bookmark = JSON.parse(JSON.stringify(b)); // 执行深度复制
|
||||
bookmark.type = 1;
|
||||
bookmarks.push(bookmark);
|
||||
})
|
||||
|
|
@ -609,7 +623,7 @@ db.getBookmarksTable = function(params) {
|
|||
result.sort((a, b) => a.created_at >= b.created_at ? -1 : 1)
|
||||
.slice(begin, end)
|
||||
.forEach((b) => {
|
||||
var bookmark = JSON.parse(JSON.stringify(b));// 执行深度复制
|
||||
var bookmark = JSON.parse(JSON.stringify(b)); // 执行深度复制
|
||||
bookmark.type = 2;
|
||||
bookmarks.push(bookmark);
|
||||
})
|
||||
|
|
@ -617,7 +631,7 @@ db.getBookmarksTable = function(params) {
|
|||
result.sort((a, b) => a.last_click >= b.last_click ? -1 : 1)
|
||||
.slice(begin, end)
|
||||
.forEach((b) => {
|
||||
var bookmark = JSON.parse(JSON.stringify(b));// 执行深度复制
|
||||
var bookmark = JSON.parse(JSON.stringify(b)); // 执行深度复制
|
||||
bookmark.type = 3;
|
||||
bookmarks.push(bookmark);
|
||||
})
|
||||
|
|
@ -662,7 +676,7 @@ db.getBookmarksByTag = function(params) {
|
|||
})
|
||||
.slice(begin, end)
|
||||
.forEach((b) => {
|
||||
var bookmark = JSON.parse(JSON.stringify(b));// 执行深度复制
|
||||
var bookmark = JSON.parse(JSON.stringify(b)); // 执行深度复制
|
||||
bookmark.type = 1;
|
||||
bookmarks.push(bookmark);
|
||||
})
|
||||
|
|
@ -670,7 +684,7 @@ db.getBookmarksByTag = function(params) {
|
|||
result.sort((a, b) => a.created_at >= b.created_at ? -1 : 1)
|
||||
.slice(begin, end)
|
||||
.forEach((b) => {
|
||||
var bookmark = JSON.parse(JSON.stringify(b));// 执行深度复制
|
||||
var bookmark = JSON.parse(JSON.stringify(b)); // 执行深度复制
|
||||
bookmark.type = 2;
|
||||
bookmarks.push(bookmark);
|
||||
})
|
||||
|
|
@ -678,7 +692,7 @@ db.getBookmarksByTag = function(params) {
|
|||
result.sort((a, b) => a.last_click >= b.last_click ? -1 : 1)
|
||||
.slice(begin, end)
|
||||
.forEach((b) => {
|
||||
var bookmark = JSON.parse(JSON.stringify(b));// 执行深度复制
|
||||
var bookmark = JSON.parse(JSON.stringify(b)); // 执行深度复制
|
||||
bookmark.type = 3;
|
||||
bookmarks.push(bookmark);
|
||||
})
|
||||
|
|
@ -754,7 +768,7 @@ db.getBookmarksSearch = function(params) {
|
|||
find = true;
|
||||
}
|
||||
})
|
||||
if(!find){
|
||||
if (!find) {
|
||||
bookmarks.push(b1);
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', 'pubSu
|
|||
$scope.selectNotLoginIndex = 0; /**< 默认未登陆之后的选择的菜单索引,下表从 0 开始 */
|
||||
$scope.searchWord = ''; /**< 搜索关键字 */
|
||||
$scope.showStyle = null;
|
||||
$scope.searchHistory = [];
|
||||
|
||||
// 防止在登陆的情况下,在浏览器里面直接输入url,这时候要更新菜单选项
|
||||
pubSubService.subscribe('Common.menuActive', $scope, function(event, params) {
|
||||
console.log("subscribe Common.menuActive", params)
|
||||
|
|
@ -57,15 +59,66 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', 'pubSu
|
|||
reload: true,
|
||||
})
|
||||
updateMenuActive($scope.selectLoginIndex = 0);
|
||||
} else if(searchOption == 1){
|
||||
} else if (searchOption == 1) {
|
||||
$window.open('https://www.google.com.hk/#newwindow=1&safe=strict&q=' + encodeURIComponent(searchWord), '_blank');
|
||||
} else if(searchOption == 2){
|
||||
$window.open('https://github.com/search?utf8=%E2%9C%93&q='+ encodeURIComponent(searchWord) +'&type=', '_blank');
|
||||
} else if(searchOption == 3){
|
||||
$window.open('https://stackoverflow.com/search?q='+ encodeURIComponent(searchWord), '_blank');
|
||||
} else if (searchOption == 2) {
|
||||
$window.open('https://github.com/search?utf8=%E2%9C%93&q=' + encodeURIComponent(searchWord) + '&type=', '_blank');
|
||||
} else if (searchOption == 3) {
|
||||
$window.open('https://stackoverflow.com/search?q=' + encodeURIComponent(searchWord), '_blank');
|
||||
} else {
|
||||
$window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(searchWord), '_blank');
|
||||
}
|
||||
// 如果第一个显示的搜索分类跟关键字跟列表一样,那么不要更新
|
||||
var newItem = {
|
||||
t: searchOption,
|
||||
d: searchWord,
|
||||
}
|
||||
var delIndex = -1;
|
||||
$scope.searchHistory.unshift(newItem);
|
||||
$scope.searchHistory.forEach((item, index) => {
|
||||
if (index >= 1 && item.t == searchOption && item.d == searchWord) {
|
||||
delIndex = index;
|
||||
}
|
||||
})
|
||||
if (delIndex >= 0) {
|
||||
$scope.searchHistory.splice(delIndex, 1);
|
||||
}
|
||||
|
||||
var datas = [];
|
||||
$scope.searchHistory.forEach((item, index) => {
|
||||
datas.push({
|
||||
t: item.t,
|
||||
d: item.d,
|
||||
})
|
||||
})
|
||||
|
||||
var parmes = {
|
||||
searchHistory: JSON.stringify(datas),
|
||||
};
|
||||
bookmarkService.updateSearchHistory(parmes)
|
||||
.then((data) => {
|
||||
if (data.retCode == 0) {
|
||||
// toastr.success('历史搜索更新成功', "提示");
|
||||
} else {
|
||||
toastr.error('历史搜索更新失败。错误信息:' + data.msg, "错误");
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
toastr.error('历史搜索更新失败。错误信息:' + JSON.stringify(err), "错误");
|
||||
});
|
||||
}
|
||||
|
||||
$scope.searchByHistory = function(type, data) {
|
||||
$scope.searchWord = data;
|
||||
$('.js-search-option').dropdown('set value', type);
|
||||
var types = {};
|
||||
types[0] = '书签';
|
||||
types[1] = '谷歌';
|
||||
types[2] = 'Github';
|
||||
types[3] = '栈溢出';
|
||||
types[4] = '百度';
|
||||
$('.js-search-option').dropdown('set text', types[type]);
|
||||
$('.js-search-option').dropdown('save defaults', types[type]);
|
||||
}
|
||||
|
||||
$scope.updateShowStyle = function(showStyle) {
|
||||
|
|
@ -98,4 +151,20 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', 'pubSu
|
|||
$('.ui.menu a.item').removeClass('selected');
|
||||
$('.ui.menu a.item:eq(' + index + ')').addClass('selected');
|
||||
}
|
||||
|
||||
setTimeout(function() {
|
||||
bookmarkService.userInfo({})
|
||||
.then((data) => {
|
||||
$scope.searchHistory = JSON.parse(data.search_history || '[]');
|
||||
setTimeout(function() {
|
||||
$('.search-item')
|
||||
.popup({
|
||||
on: 'focus'
|
||||
});
|
||||
}, 1000);
|
||||
})
|
||||
.catch((err) => {
|
||||
toastr.error('获取信息失败。错误信息:' + JSON.stringify(err), "错误");
|
||||
});
|
||||
}, 1000);
|
||||
}]);
|
||||
|
|
|
|||
|
|
@ -217,3 +217,16 @@ app.directive('faviconErr', function() {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.filter('searchType', function() {
|
||||
return function(type) {
|
||||
var types = {};
|
||||
types[0] = '书签';
|
||||
types[1] = '谷歌';
|
||||
types[2] = 'Github';
|
||||
types[3] = '栈溢出';
|
||||
types[4] = '百度';
|
||||
|
||||
return types[type];
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -313,6 +313,19 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) {
|
|||
});
|
||||
return def.promise;
|
||||
},
|
||||
updateSearchHistory: function(params) {
|
||||
var def = $q.defer();
|
||||
$http.post('/api/updateSearchHistory/', {
|
||||
params: params
|
||||
})
|
||||
.success(function(data) {
|
||||
def.resolve(data);
|
||||
})
|
||||
.error(function(data) {
|
||||
def.reject('updateSearchHistory error');
|
||||
});
|
||||
return def.promise;
|
||||
},
|
||||
getAdvices: function getAdvices(params) {
|
||||
var def = $q.defer();
|
||||
$http.get('/api/advices/', {
|
||||
|
|
|
|||
|
|
@ -50,7 +50,18 @@
|
|||
</div>
|
||||
</div>
|
||||
</label>
|
||||
<input class="prompt" type="text" ng-model="searchWord" placeholder="" ng-keypress="($event.which === 13)?search(searchWord):0" id="lcq">
|
||||
<input class="prompt search-item" type="text" ng-model="searchWord" placeholder="" ng-keypress="($event.which === 13)?search(searchWord):0" data-position="bottom left" id="lcq">
|
||||
<div class="ui fluid popup top left transition hidden">
|
||||
<div class="ui selection list">
|
||||
<div class="item" ng-repeat="item in searchHistory" ng-click="searchByHistory(item.t, item.d)">
|
||||
<div class="right floated content">
|
||||
{{ item.t | searchType }}
|
||||
</div>
|
||||
<div class="content">{{ item.d}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<i class="search link icon" ng-click="search(searchWord)" style="cursor:default;"></i>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -196,6 +196,37 @@ api.post('/updateShowStyle', function(req, res) {
|
|||
});
|
||||
});
|
||||
|
||||
api.post('/updateSearchHistory', function(req, res) {
|
||||
console.log("updateSearchHistory");
|
||||
if (!req.session.user) {
|
||||
res.send(401);
|
||||
return;
|
||||
}
|
||||
|
||||
var params = req.body.params;
|
||||
db.getUser(req.session.user.username)
|
||||
.then((user) => {
|
||||
if (user) {
|
||||
return db.updateSearchHistory(req.session.userId, params.searchHistory)
|
||||
} else {
|
||||
return Promise.resolve(0)
|
||||
}
|
||||
})
|
||||
.then((affectedRows) => {
|
||||
res.json({
|
||||
retCode: (affectedRows == 1 ? 0 : 1),
|
||||
msg: req.session.username + " 更新历史搜索成功!",
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log('resetPassword error', err);
|
||||
res.json({
|
||||
retCode: 2,
|
||||
msg: req.session.username + " 更新历史搜索失败!: " + JSON.stringify(err),
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
api.get('/autoLogin', function(req, res) {
|
||||
var ret = {
|
||||
logined: false,
|
||||
|
|
@ -735,7 +766,7 @@ api.post('/addBookmark', function(req, res) {
|
|||
db.getBookmarkbyUrl(userId, bookmark.url)
|
||||
.then((bookmarkId) => {
|
||||
// 如果这个url的书签存在了,那么直接返回书签,否则返回插入的书签
|
||||
if(bookmarkId){
|
||||
if (bookmarkId) {
|
||||
bookmark.id = bookmarkId;
|
||||
db.updateBookmark(bookmark); // 如果存在,更新一下。
|
||||
update = true;
|
||||
|
|
@ -778,7 +809,7 @@ api.post('/favoriteBookmark', function(req, res) {
|
|||
db.getBookmarkbyUrl(userId, bookmark.url)
|
||||
.then((bookmarkId) => {
|
||||
// 如果这个url的书签存在了,那么直接返回书签,否则返回插入的书签
|
||||
if(bookmarkId){
|
||||
if (bookmarkId) {
|
||||
bookmark.id = bookmarkId;
|
||||
db.updateBookmark(bookmark); // 如果存在,更新一下。
|
||||
update = true;
|
||||
|
|
@ -1142,7 +1173,7 @@ api.getHotBookmarksByTimer = function() {
|
|||
console.log('getHotBookmarks is busy')
|
||||
return;
|
||||
}
|
||||
if(timeout < 1000 * 5){
|
||||
if (timeout < 1000 * 5) {
|
||||
busy = true; // 实践证明很容易出错导致busy一直是true,所以干脆去掉此选项了。
|
||||
}
|
||||
console.log('begin getHotBookmarks...');
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ CREATE TABLE `users` (
|
|||
`created_at` datetime DEFAULT now(), -- 创建时间
|
||||
`last_login` datetime DEFAULT NULL, -- 最后一次登录时间
|
||||
`show_style` char(16) NOT NULL DEFAULT 'navigate', -- 显示风格
|
||||
`search_history` varchar(255) DEFAULT NULL, -- 历史搜索记录
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `username` (`username`),
|
||||
UNIQUE KEY `email` (`email`)
|
||||
|
|
|
|||
Loading…
Reference in New Issue