增加一个搜索历史表

This commit is contained in:
luchenqun 2017-04-03 22:47:10 +08:00
parent 3cf3854718
commit 4b6bd2a404
7 changed files with 189 additions and 37 deletions

View File

@ -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) { db.register = function(user) {
console.log('register'); console.log('register');
var sql = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('" + user.username + "', '" + user.password + "', '" + user.email + "')"; var sql = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('" + user.username + "', '" + user.password + "', '" + user.email + "')";

View File

@ -5,6 +5,8 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', 'pubSu
$scope.selectNotLoginIndex = 0; /**< 默认未登陆之后的选择的菜单索引,下表从 0 开始 */ $scope.selectNotLoginIndex = 0; /**< 默认未登陆之后的选择的菜单索引,下表从 0 开始 */
$scope.searchWord = ''; /**< 搜索关键字 */ $scope.searchWord = ''; /**< 搜索关键字 */
$scope.showStyle = null; $scope.showStyle = null;
$scope.searchHistory = [];
// 防止在登陆的情况下在浏览器里面直接输入url这时候要更新菜单选项 // 防止在登陆的情况下在浏览器里面直接输入url这时候要更新菜单选项
pubSubService.subscribe('Common.menuActive', $scope, function(event, params) { pubSubService.subscribe('Common.menuActive', $scope, function(event, params) {
console.log("subscribe Common.menuActive", params) console.log("subscribe Common.menuActive", params)
@ -66,6 +68,57 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', 'pubSu
} else { } else {
$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(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) { $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').removeClass('selected');
$('.ui.menu a.item:eq(' + index + ')').addClass('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);
}]); }]);

View File

@ -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];
}
});

View File

@ -313,6 +313,19 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) {
}); });
return def.promise; 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) { getAdvices: function getAdvices(params) {
var def = $q.defer(); var def = $q.defer();
$http.get('/api/advices/', { $http.get('/api/advices/', {

View File

@ -50,7 +50,18 @@
</div> </div>
</div> </div>
</label> </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> <i class="search link icon" ng-click="search(searchWord)" style="cursor:default;"></i>
</div> </div>
</div> </div>

View File

@ -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) { api.get('/autoLogin', function(req, res) {
var ret = { var ret = {
logined: false, logined: false,

View File

@ -8,6 +8,7 @@ CREATE TABLE `users` (
`created_at` datetime DEFAULT now(), -- 创建时间 `created_at` datetime DEFAULT now(), -- 创建时间
`last_login` datetime DEFAULT NULL, -- 最后一次登录时间 `last_login` datetime DEFAULT NULL, -- 最后一次登录时间
`show_style` char(16) NOT NULL DEFAULT 'navigate', -- 显示风格 `show_style` char(16) NOT NULL DEFAULT 'navigate', -- 显示风格
`search_history` varchar(255) DEFAULT NULL, -- 历史搜索记录
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`), UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`) UNIQUE KEY `email` (`email`)