可以增加书签啦

This commit is contained in:
luchenqun 2016-10-28 23:21:58 +08:00
parent 296b2fb6aa
commit 8ee2cabf7d
5 changed files with 195 additions and 41 deletions

View File

@ -24,6 +24,11 @@ app.controller('bookmarksCtr', ['$scope', '$stateParams', '$filter', '$window',
getBookmarks(params);
});
pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function(event, params) {
console.log('subscribe EditCtr.inserBookmarsSuccess', params);
getBookmarks(params);
});
function getBookmarks(params) {
bookmarkService.getBookmarks(params).then(
function(data) {

View File

@ -1,41 +1,63 @@
app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'pubSubService', function($scope, $state, $timeout, bookmarkService, pubSubService) {
var maxSelections = 3;
console.log("Hello , I enter editCtr...");
init();
semanticInit();
$scope.$watch('url', function(newValue, oldValue, scope) {
$scope.urlError = $scope.url == '';
console.log('url is changed', $('.ui.modal.js-add-bookmark').modal('is active'));
$timeout(function() {
$scope.urlError = $scope.url == '' && $('.ui.modal.js-add-bookmark').modal('is active');
});
});
$scope.$watch('title', function(newValue, oldValue, scope) {
$scope.titleError = $scope.title == '';
$timeout(function() {
$scope.titleError = $scope.title == '' && $('.ui.modal.js-add-bookmark').modal('is active');
});
});
$scope.addTags = function() {
console.log('Hello , you have click add tag btn......');
$scope.newTags = $scope.newTags.replace(//g, ",").replace(/,+/g, ","); // 先将中文逗号替换成英文逗号,然后将多个英文逗号换成一个英文逗号
// 先将中文逗号替换成英文逗号,然后将多个英文逗号换成一个英文逗号
$scope.newTags = $scope.newTags.replace(//g, ",").replace(/,+/g, ",");
var tags = $scope.newTags.split(",");
var params = [];
tags.forEach(function(tag) {
tag = tag.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' '); // 去除前后空格,多个空格转为一个空格;
var find = false;
for (var i = 0; i < $scope.tags.length; i++) {
if ($scope.tags[i].name === tag) {
find = true;
$('.ui.fluid.search.dropdown').dropdown('set selected', $scope.tags[i].id); // 在标签上已有的直接加入标签
}
};
if (!find && tag !== '') {
params.push(tag);
}
});
console.log(params);
bookmarkService.addTags(params).then(
function(data) {
$scope.tags = data;
console.log(JSON.stringify(data));
$timeout(function() {
var count = 0;
params.forEach(function(tagName) {
data.forEach(function(tag) {
if (tagName == tag.name) {
console.log(tag.id);
if (count < maxSelections) {
$('.ui.fluid.search.dropdown').dropdown('set selected', tag.id);
}
count++;
}
});
});
});
},
function(errorMsg) {}
);
}
$scope.cancel = function() {
console.log('Hello , you have click cancel btn......');
$('.ui.modal.js-add-bookmark').modal('hide');
$('.ui.modal.js-add-bookmark .ui.dropdown').dropdown('clear');
init();
}
$scope.ok = function() {
console.log('Hello , you have click ok btn......');
@ -43,13 +65,26 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p
console.log($scope.url, $scope.title, $scope.description, $scope.public, selectedTags);
$scope.urlError = $scope.url == '';
$scope.titleError = $scope.title == '';
$scope.tagsError = (selectedTags.length == 0 || selectedTags.length > 3);
$scope.tagsError = (selectedTags.length == 0 || selectedTags.length > maxSelections);
var params = {
url: $scope.url,
title: $scope.title,
public: '1',
tags: selectedTags,
description: $scope.description
}
bookmarkService.addBookmark({
a: 'Hello i love this world'
}).then(
bookmarkService.addBookmark(params).then(
function(data) {
console.log(data);
$('.ui.modal.js-add-bookmark').modal('hide');
$state.go('bookmarks', {
foo: 'i love you',
bar: 'hello world'
});
pubSubService.publish('EditCtr.inserBookmarsSuccess', {
show: 'navigate'
});
},
function(errorMsg) {
console.log(errorMsg);
@ -59,7 +94,9 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p
pubSubService.subscribe('MenuCtr.showAddBookmarkMoadl', $scope, function(event, params) {
console.log('subscribe MenuCtr.MenuCtr.showAddBookmarkMoadl', params);
$('.ui.modal.js-add-bookmark').modal('show');
$('.ui.modal.js-add-bookmark').modal({
closable: false,
}).modal('show');
$('.ui.modal.js-add-bookmark .ui.dropdown').dropdown('clear');
$('.ui.modal.js-add-bookmark .ui.dropdown').addClass('loading');
init();
@ -86,11 +123,11 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p
setTimeout(() => {
$('.ui.dropdown').dropdown({
forceSelection: false,
maxSelections: 3,
maxSelections: maxSelections,
onChange: function(value, text, $choice) {
var selectedTags = $('.ui.modal.js-add-bookmark .ui.dropdown').dropdown('get value');
$timeout(function() {
$scope.tagsError = (selectedTags.length == 0 || selectedTags.length > 3);
$scope.tagsError = (selectedTags.length == 0 || selectedTags.length > maxSelections) && ($('.ui.modal.js-add-bookmark').modal('is active'));
});
}
});

View File

@ -1,5 +1,5 @@
<div class="ui modal js-add-bookmark" ng-controller="editCtr">
<i class="close icon"></i>
<!-- <i class="close icon"></i> -->
<div class="header">
添加书签
</div>
@ -43,7 +43,7 @@
</div>
</div>
<div class="actions">
<div class="ui button" ng-click="cancel()">取消</div>
<div class="ui cancel button" ng-click="cancel()">取消</div>
<div class="ui green button" ng-click="ok()">发送</div>
</div>
</div>

View File

@ -1,21 +1,21 @@
var api = require('express').Router();
var mysql = require('mysql');
var client = mysql.createConnection({
host: '172.24.13.5',
user: 'root',
password: 'root123',
database: 'mybookmarks',
multipleStatements: true,
port: 3306
});
// var client = mysql.createConnection({
// host: '127.0.0.1',
// user: 'lcq',
// password: '123456',
// host: '172.24.13.5',
// user: 'root',
// password: 'root123',
// database: 'mybookmarks',
// multipleStatements: true,
// port: 3306
// });
var client = mysql.createConnection({
host: '127.0.0.1',
user: 'lcq',
password: '123456',
database: 'mybookmarks',
multipleStatements: true,
port: 3306
});
client.connect();
api.get('/bookmarks', function(req, res) {
@ -106,7 +106,7 @@ api.get('/bookmarks', function(req, res) {
api.get('/tags', function(req, res) {
console.log('hello tags', JSON.stringify(req.query));
var user_id = req.query.user_id;
var sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "'"
var sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "' ORDER BY last_use DESC"
client.query(sql, function(error, result, fields) {
if (error) {
res.json({
@ -120,16 +120,127 @@ api.get('/tags', function(req, res) {
api.post('/addBookmark', function(req, res) {
console.log('hello addBookmark', JSON.stringify(req.query), JSON.stringify(req.body));
var params = req.body.params;
var user_id = '1';
var tags = params.tags;
var sql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + params.title + "', '" + params.description + "', '" + params.url + "', '" + params.public + "', '1')";
console.log(sql);
client.query(sql, function(err, result) {
if (err) throw err;
var insertId = result.insertId;
sql = "INSERT INTO `tags_bookmarks` (`tag_id`, `bookmark_id`) VALUES";
for (var i = 0; i < tags.length; i++) {
if (i >= 1) {
sql += ','
}
sql += "('" + tags[i] + "', '" + insertId + "')";
}
client.query(sql, function(error, result, fields) {
if (error) {
res.json({
a: 'i love this world, too!'
error: 'error tags'
});
} else {
sql = "UPDATE tags SET last_use = NOW() WHERE user_id = '" + user_id + "' AND id in (";
for (var i = 0; i < tags.length; i++) {
if (i >= 1) {
sql += ','
}
sql += "'" + tags[i] + "'";
}
sql += ')'
console.log(sql);
client.query(sql, function(error, result1, fields) {
if (error) {
res.json({
error: 'error tags'
});
} else {
res.json({
hello: 'success'
});
}
})
}
})
console.log(result.insertId);
});
// res.json({
// a: 'i love this world, too!'
// });
});
api.post('/addTags', function(req, res) {
console.log('hello addTags', JSON.stringify(req.query), JSON.stringify(req.body));
var params = req.body.params;
var user_id = '1';
var addTagNames = [];
var sql = "SELECT * FROM `tags` WHERE `user_id` = '" + user_id + "' AND `name` in (";
for (var i = 0; i < params.length; i++) {
if (i >= 1) {
sql += ','
}
sql += "'" + params[i] + "'";
};
sql += ")";
console.log(sql);
client.query(sql, function(error, result1, fields) {
if (error) {
res.json({
a: 'i love this world, too!'
error: 'error tags'
});
} else {
params.forEach(function(name) {
var find = false;
result1.forEach(function(tag) {
if (tag.name == name) {
find = true;
}
})
if (!find) {
addTagNames.push(name);
}
})
sql = "INSERT INTO `tags` (`user_id`, `name`) VALUES";
for (var i = 0; i < addTagNames.length; i++) {
if (i >= 1) {
sql += ','
}
sql += "('" + user_id + "', '" + addTagNames[i] + "')";
}
if (addTagNames.length == 0) {
sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "'";
}
console.log(sql);
client.query(sql, function(error, result, fields) {
if (error) {
res.json({
error: 'error tags'
});
} else {
if (addTagNames.length == 0) {
res.json(result);
} else {
sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "' ORDER BY last_use DESC"
client.query(sql, function(error, result, fields) {
if (error) {
res.json({
error: 'error tags'
});
} else {
res.json(result);
}
})
}
}
})
}
})
});
// client.end();

View File

@ -34,6 +34,7 @@ CREATE TABLE `tags` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- id
`user_id` int(11) NOT NULL, -- 用户id
`name` varchar(32) NOT NULL, -- 标签
`last_use` datetime DEFAULT now(), -- 最后使用标签的时间
PRIMARY KEY (`id`),
KEY `userIdIdx` (`user_id`)
);