Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ade199e

Browse files
committed
robot
1 parent 003aaaa commit ade199e

File tree

12 files changed

+236
-55
lines changed

12 files changed

+236
-55
lines changed

public/css/style.css

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
@import "base.css";
55
@import "form.css";
6+
@import "table.css";
67
@import "tip.css";
78
@import "sidebar.css";
89
/*= Login && Register

public/js/models/ajax.js

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ define(function(require,exports,module){
3333
else
3434
window.location.reload();
3535
},2000);
36+
}else if(res.flg === 2){
37+
tips.close();
38+
$('#answers').html('<div class="grey-tips">'+res.answers.a+'</div>');
3639
}else{
3740
tips.close();
3841
new popbox.tinyTips('error',res.msg);

public/js/models/validate.js

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ define(function(require,exports,module){
4949
,tips : '请输入网站地址'
5050
,error : '网站地址出错!'
5151
}
52+
,'q' : {
53+
name : '问题'
54+
,rex : '^[^\\s]+$'
55+
,tips : '请输入问题!'
56+
,error :'问题不能为空哦!'
57+
}
5258
}
5359
function getLen(s){
5460
var l = 0;

routes.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var login = require('./routes/login')
55
,register = require('./routes/register')
66
,student = require('./routes/student')
7+
,question = require('./routes/question')
78

89

910
var admin = require('./routes/admin/index')
@@ -23,18 +24,26 @@ module.exports = function(app){
2324
//-------------------
2425
// 注册
2526
//-------------------
27+
app.get('/reg',checkNotLogin);
2628
app.get('/reg',register.index);
2729
app.post('/reg',register.index);
2830
//--------------------
29-
// 学生个人中心
31+
// 个人资料
3032
//--------------------
31-
app.get('/:username',student.index);
32-
33+
app.all('/stu/*',checkLogin);
34+
app.get('/stu/:username',student.index);
35+
app.post('/stu/:username',student.index);
36+
//-------------------
37+
// 机器人问答
38+
//-------------------
39+
app.get('/q/robot',question.index);
40+
app.post('/q/robot',question.index);
3341

3442

3543
/*===================
3644
后台路由
3745
===================*/
46+
app.all(/\/admin\/?\w*/,checkLogin);
3847
//-------------------
3948
// 首页
4049
//-------------------
@@ -76,4 +85,17 @@ module.exports = function(app){
7685
req.session.user = null;
7786
return res.redirect('/');
7887
})
79-
}
88+
}
89+
90+
function checkLogin(req,res,next){
91+
if(!req.session.user){
92+
return res.redirect('/');
93+
}
94+
next();
95+
}
96+
function checkNotLogin(req,res,next){
97+
if(req.session.user){
98+
return res.redirect('/');
99+
}
100+
next();
101+
}

routes/admin/user.js

+51-28
Original file line numberDiff line numberDiff line change
@@ -103,37 +103,60 @@ exports.infouser = function(req,res){
103103
});
104104
});
105105
}else if(req.method =='POST'){
106-
var condition ={};
107-
if(cat == 3){
108-
condition.modify = {
109-
'$set' : {
110-
rank : req.body.rank
111-
}
112-
}
106+
if(parseInt(req.query.front,10) && (req.body.oldpassword || req.body.password)){
107+
if(/^[\w]{6,12}$/.test(req.body.oldpassword) && /^[\w]{6,12}$/.test(req.body.password)){
108+
return res.json({flg:0,msg:'密码必须要6位以上'});
109+
}
110+
jixiang.getOne({_id:uid},'users',function(err,doc){
111+
if(err)return res.json({flg:0,msg:err});
112+
var md5 = crypto.createHash('md5');
113+
var oldpassword = md5.update(req.body.oldpassword).digest('base64');
114+
if(doc.password !== oldpassword)
115+
return res.json({flg:0,msg:'原密码不正确!'});
116+
setInfo();
117+
})
113118
}else{
114-
condition.modify = {
115-
'$set' : {
116-
sex : req.body.sex
119+
setInfo();
120+
}
121+
function setInfo(){
122+
var condition ={};
123+
if(cat == 3){
124+
condition.modify = {
125+
'$set' : {
126+
rank : req.body.rank
127+
}
128+
}
129+
}else{
130+
condition.modify = {
131+
'$set' : {
132+
sex : req.body.sex
133+
}
117134
}
118135
}
119-
}
120-
if(cat == 2){
121-
condition.modify['$set'].school = req.body.school;
122-
}
123-
if(req.body.password){
124-
//生成口令散列
125-
var md5 = crypto.createHash('md5');
126-
var password = md5.update(req.body.password).digest('base64');
127-
condition.modify['$set'].password = password;
128-
}
129-
condition.query = {
130-
_id : uid
131-
}
132-
jixiang.update(condition,'users',function(err){
133-
if(err){
134-
return res.json({flg:0,msg:'修改失败!'});
136+
if(cat == 2){
137+
condition.modify['$set'].school = req.body.school;
135138
}
136-
return res.json({flg:1,msg:'修改成功!',redirect:'/admin/user?cat='+cat});
137-
});
139+
if(req.body.password){
140+
//生成口令散列
141+
var md5 = crypto.createHash('md5');
142+
var password = md5.update(req.body.password).digest('base64');
143+
condition.modify['$set'].password = password;
144+
}
145+
condition.query = {
146+
_id : uid
147+
}
148+
jixiang.update(condition,'users',function(err){
149+
if(err){
150+
return res.json({flg:0,msg:'修改失败!'});
151+
}
152+
var redirect = '/admin/user?cat='+cat;
153+
if(parseInt(req.query.front,10)){
154+
redirect = (parseInt(req.query.front,10)==1) ? '/stu/' : '/teach/';
155+
redirect += req.session.user.username;
156+
}
157+
return res.json({flg:1,msg:'修改成功!',redirect:redirect});
158+
});
159+
}
160+
138161
}
139162
}

routes/question.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 问题路由
3+
*/
4+
var jixiang = require('../models/base');
5+
var crypto = require('crypto');
6+
var config = require('../config');
7+
8+
exports.index = function(req,res){
9+
if(req.method =='GET'){
10+
res.render('./index/question',
11+
{
12+
title : config.name +'提问机器人'
13+
,user : req.session.user
14+
});
15+
}else if(req.method == 'POST'){
16+
if(!req.body.q)return res.json({flg:0,msg:'问题不能为空哦>_<'});
17+
var q = req.body.q;
18+
jixiang.get({query:{q:new RegExp(q,'gi')}},'qa',function(err,doc){
19+
if(err)doc=[];
20+
if(doc.length){
21+
var num = parseInt(Math.random()*doc.length,10);
22+
return res.json({flg:2,answers:doc[num]});
23+
}
24+
return res.json({flg:0,msg:'抱歉这题我还不会>_<'});
25+
})
26+
}
27+
28+
}

routes/student.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
/**
2-
* student.js
2+
* 学生路由
33
*/
4-
var jixiang = require('../models/base');
5-
var crypto = require('crypto');
6-
var config = require('../config');
4+
var jixiang = require('../models/base')
5+
,crypto = require('crypto')
6+
,config = require('../config')
7+
,format = require('../models/utils').format
78

89
var index = function(req,res){
9-
res.render('./index/student',{
10-
title : '学生个人中心'
11-
,user : req.session.user
12-
})
13-
}
10+
if(req.method == 'GET'){
11+
jixiang.getOne({_id:req.session.user._id},'users',function(err,doc){
12+
if(err){
13+
console.log(err);
14+
}
15+
doc.regdate = format.call(new Date(doc.regdate),'yyyy-MM-dd hh:mm:ss');
16+
doc.logindate = format.call(new Date(doc.logindate),'yyyy-MM-dd hh:mm:ss');
17+
res.render('./index/student',
18+
{
19+
title : config.name+'个人中心'
20+
,user : req.session.user
21+
,people : doc
22+
});
23+
});
24+
25+
}else if(req.method == 'POST'){
26+
27+
}
28+
29+
}
30+
exports.index = index;

views/index/index.jade

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ block content
3232
button.btn(type='submit') 登入
3333
p 还没注册? <a href="/reg">点我注册</a>
3434
else
35-
.container
35+
.main
3636
//公告处
3737
if !!result.notice
3838
.grey-tips.red 公告:#{result.notice}
39-
if result.links && result.links.length
40-
.container.link
41-
h2.hbt 友情链接
42-
ul.clearfix
43-
each item in result.links
44-
li
45-
a(href=item.url,target="_blank",title=item.description)=item.name
39+
block link
40+
if result.links && result.links.length
41+
.container.link
42+
h2.hbt 友情链接
43+
ul.clearfix
44+
each item in result.links
45+
li
46+
a(href=item.url,target="_blank",title=item.description)=item.name
4647

4748

views/index/question.jade

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extends ../layout/layout
2+
block content
3+
.main
4+
.grey-tips 我是一个机器人,有什么问题,就问我吧~
5+
form#q.form-inline.ajax-form(action="/q/robot",method="post")
6+
input#q(type="text",name="q",class="require")
7+
button.btn 提交
8+
#answers
9+

views/index/student.jade

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
extends ../layout/layout
2+
block content
3+
.main
4+
form.form-inline.ajax-form(action="/admin/user/info?front=1&cat=1&uid="+people._id,method="post")
5+
table.table.order-table
6+
caption.bread
7+
a.parent 个人资料
8+
thead.tab-title
9+
tr.separate
10+
th(colspan="2")
11+
tbody
12+
tr
13+
td 用户名
14+
td.aleft=people.username
15+
tr
16+
td 邮箱
17+
td.aleft=people.email
18+
tr
19+
td 性别
20+
td.aleft
21+
-var select= {'1':false,'2':false,'3':false};
22+
each item,key in select
23+
if people.sex == key
24+
-select[key]=true;
25+
-break;
26+
select#sex(name="sex")
27+
option(value="1",selected=select["1"])
28+
option(value="2",selected=select["2"])
29+
option(value="3",selected=select["3"]) 其他
30+
tr
31+
td 注册时间
32+
td.aleft=people.regdate
33+
tr
34+
td 最后登入时间
35+
td.aleft=people.logindate
36+
tr
37+
td 原密码
38+
td.aleft
39+
input#oldpassword(type="password",name="oldpassword")
40+
| 不修改密码请留空
41+
tr
42+
td 新密码
43+
td.aleft
44+
input#password(type="password",name="password")
45+
tr
46+
td
47+
td.aleft
48+
button.btn(type="submit") 修改

views/layout/layout.jade

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,14 @@ html
55
title #{title}
66
link(rel="stylesheet",href="/css/style.css")
77
body
8-
block content
8+
if user
9+
.container.clearfix
10+
include header
11+
#main.clearfix
12+
.content
13+
block content
14+
include sidebar
15+
block link
16+
else
17+
block content
918
include footer

views/layout/sidebar.jade

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
.sidebar.sideleft
22
div.category-item
3-
.name
4-
a(href="/admin/notice") 个人资料
3+
.category.clearfix
4+
.name
5+
a(href="/") 首页
6+
div.category-item
7+
.category.clearfix
8+
.name
9+
a(href="/stu/#{user.username}/ask") 我要提问
10+
.plus
11+
ul
12+
li
13+
a.item(href="/q/robot") 问机器人
14+
li
15+
a.item(href="/q/teacher") 提问老师
516
div.category-item
617
.category.clearfix
718
.name
8-
a(href="/admin/user") 我提问的
19+
a(href="/stu/#{user.username}/question") 我提问的
920
.plus
1021
ul
1122
li
12-
a.item(href="/admin/user?cat=1") 已解决
23+
a.item(href="/stu/#{user.username}/question?cat=1") 已解决
1324
li
14-
a.item(href="/admin/user?cat=2") 未解决
25+
a.item(href="/stu/#{user.username}/question?cat=2") 未解决
26+
div.category-item
27+
.name
28+
a(href="/stu/#{user.username}") 个人资料

0 commit comments

Comments
 (0)