增加一个留言功能

This commit is contained in:
luchenqun 2017-02-08 14:51:13 +08:00
parent 7be848eea6
commit 4639538a5a
15 changed files with 211 additions and 2 deletions

View File

@ -278,6 +278,34 @@ db.getTags = function(user_id) {
});
};
db.getAdvices = function(params) {
console.log('getAdvices');
var sql = "SELECT mod(CEIL(RAND()*100), 5) as head_id, a.id, a.user_id, u.username, a.comment, a.category, DATE_FORMAT(a.created_at, '%Y-%m-%d %H:%i:%s') as created_at, a.state FROM `advices` as a LEFT OUTER JOIN users as u ON a.user_id = u.id ORDER BY a.created_at DESC LIMIT 0, 100";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
};
db.addAdvice = function(params) {
console.log('addAdvice');
var sql = "INSERT INTO `advices` (`user_id`, `comment`, `category`) VALUES ('"+ params.user_id +"', '"+ params.comment +"', '"+ params.category +"')";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result.affectedRows);
}
});
});
};
db.getTagsByNames = function(user_id, tags_name) {
console.log('getTagsByNames');
var sql = "SELECT * FROM `tags` WHERE `user_id` = '" + user_id + "' AND `name` in (" + tags_name.toString() + ")";

BIN
public/images/head0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
public/images/head1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
public/images/head2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

BIN
public/images/head3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
public/images/head4.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@ -51,6 +51,7 @@
<script src="/scripts/controllers/settings-controller.js"></script>
<script src="/scripts/controllers/login-controller.js"></script>
<script src="/scripts/controllers/tags-controller.js"></script>
<script src="/scripts/controllers/advice-controller.js"></script>
<script src="/scripts/controllers/menus-controller.js"></script>
<script src="/scripts/controllers/edit-controller.js"></script>
<script src="/scripts/controllers/search-controller.js"></script>

View File

@ -33,6 +33,7 @@ app.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
.state('advice', {
url: '/advice',
templateUrl: '/views/advice.html',
controller: 'adviceCtr'
})
.state('settings', {
url: '/settings',

View File

@ -0,0 +1,55 @@
app.controller('adviceCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'pubSubService', function($scope, $state, $timeout, bookmarkService, pubSubService) {
console.log("Hello adviceCtr");
var maxSelections = 3;
$scope.comment = '';
$scope.advices = [];
$scope.category = ["功能", "BUG", "其他"]
$scope.ok = function() {
if($scope.comment == ''){
toastr.error('留言失败内容不能为空', "错误");
return;
}
var advice = {
category: $('.ui.dropdown.js-categorys').dropdown('get value'),
comment: $scope.comment,
};
console.log(advice);
bookmarkService.addAdvice(advice)
.then((data) => {
if (data.retCode == 0) {
toastr.success('留言成功', "提示");
$scope.comment = "";
getAdvices({});
} else {
toastr.error('留言失败。错误信息:' + data.msg, "错误");
}
})
.catch((err) => {
toastr.error('留言失败:' + JSON.stringify(err), "错误");
});
}
function getAdvices(params) {
bookmarkService.getAdvices(params)
.then((data) => {
$scope.advices = data;
pubSubService.publish('Common.menuActive', {
login: true,
index: 2
});
})
.catch((err) => console.log('getAdvices err', err));
}
setTimeout(function() {
$('.ui.dropdown.js-categorys').dropdown({
onChange: function(value, text, $choice) {
}
});
getAdvices({});
}, 100)
}]);

View File

@ -19,6 +19,9 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', 'pubSubService',
}, {
uiSref: 'tags',
title: '书签分类'
}, {
uiSref: 'advice',
title: '留言'
}, {
uiSref: 'settings',
title: '设置'

View File

@ -45,6 +45,6 @@ app.controller('settingsCtr', ['$scope', '$stateParams', '$filter', '$state', '$
pubSubService.publish('Common.menuActive', {
login: true,
index: 2
index: 3
});
}]);

View File

@ -221,6 +221,32 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) {
});
return def.promise;
},
getAdvices: function getAdvices(params) {
var def = $q.defer();
$http.get('/api/advices/', {
params: params
})
.success(function(data) {
def.resolve(data);
})
.error(function(data) {
def.reject('getAdvices error');
});
return def.promise;
},
addAdvice: function(params) {
var def = $q.defer();
$http.post('/api/addAdvice/', {
params: params
})
.success(function(data) {
def.resolve(data);
})
.error(function(data) {
def.reject('addAdvice error');
});
return def.promise;
},
};
return service;

View File

@ -1 +1,45 @@
<p> 这是建议界面 </p>
<div class="ui segment">
<div class="ui form">
<div class="required field">
<label>留言分类</label>
<div class="ui selection dropdown js-categorys">
<input name="gender" type="hidden" value="0">
<i class="dropdown icon"></i>
<div class="text">功能</div>
<div class="menu">
<div class="item" data-value="0">功能</div>
<div class="item" data-value="1">BUG</div>
<div class="item" data-value="2">其他</div>
</div>
</div>
</div>
<div class="required field">
<label>留言内容</label>
<textarea rows="4" placeholder="" ng-model="comment"></textarea>
</div>
<div class="field">
<div class="actions">
<div class="ui green button" ng-click="ok()">提交</div>
</div>
</div>
</div>
<div class="ui divider"></div>
<div class="ui comments" style="width:100%">
<h3 class="ui header">最新100条留言</h3>
<div class="comment" ng-repeat="advice in advices">
<a class="avatar">
<img ng-src="/images/head{{advice.head_id}}.jpg">
</a>
<div class="content">
<a class="author">{{ advice.username }}</a>
<div class="metadata">
<span class="date">{{ advice.created_at }}</span>
</div>
<div class="metadata">
<span class="category">{{ category[advice.category] }}</span>
</div>
<div class="text">{{ advice.comment }} </div>
</div>
</div>
</div>
</div>

View File

@ -400,6 +400,44 @@ api.get('/tags', function(req, res) {
.catch((err) => console.log('tags', err));
});
api.get('/advices', function(req, res) {
if (!req.session.user) {
res.send(401);
return;
}
var params = req.query;
db.getAdvices(params)
.then((advices) => res.json(advices))
.catch((err) => console.log('tags', err));
});
api.post('/addAdvice', function(req, res) {
console.log('hello addAdvice', JSON.stringify(req.body));
if (!req.session.user) {
res.send(401);
return;
}
var params = req.body.params;
params.user_id = req.session.user.id;
db.addAdvice(params)
.then((affectedRows) => {
res.json({
retCode: 0,
msg: "留言成功 ",
})
console.log('addAdvice affectedRows ', affectedRows)
})
.catch((err) => {
console.log('addAdvice error', err);
res.json({
retCode: 1,
msg: "留言失败: " + JSON.stringify(err),
})
});
});
api.post('/addBookmark', function(req, res) {
console.log('hello addBookmark', JSON.stringify(req.body));
if (!req.session.user) {

View File

@ -46,3 +46,16 @@ CREATE TABLE `tags_bookmarks` (
`bookmark_id` int(11) NOT NULL, -- 书签id
PRIMARY KEY (`tag_id`, `bookmark_id`)
);
-- 建议留言
drop table if exists advices;
CREATE TABLE `advices` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- id
`user_id` int(11) NOT NULL, -- 用户id
`comment` text NOT NULL, -- 评论
`category` tinyint(4) DEFAULT '1', -- 分类
`created_at` datetime DEFAULT now(), -- 创建时间
`state` tinyint(4) DEFAULT '0', -- 处理结果
PRIMARY KEY (`id`),
KEY `userIdIdx` (`user_id`)
);