Skip to content

Commit

Permalink
完成登录验证功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickeyhyq committed Mar 13, 2017
1 parent 6dfcbb7 commit d34a7e3
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 45 deletions.
17 changes: 15 additions & 2 deletions musicManager/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
'use strict'

const express = require('express')

const path = require('path')
const app = express()
const bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({
extended: false
}))

// app.use(bodyParser.json())

app.get('/', (req, res) => {

res.end('OK')
})

const accountRouter = require(path.join(__dirname, './src/router/accountRouter'))
const studentManagerRouter = require(path.join(__dirname, './src/router/studentManagerRouter'))

app.use(express.static(path.join(__dirname, 'src/statics')))
app.use('/account', accountRouter)
app.use('/list', studentManagerRouter)

app.listen(5000, (error) => {
if (error) console.log(error)
console.log('start server success')
Expand Down
29 changes: 26 additions & 3 deletions musicManager/src/controller/accountController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
'use strict'
const path = require('path')
const fs = require('fs')
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient

module.exports.getLoginPage = (req, res) => {

console.log('LoginPage')
fs.readFile(path.join(__dirname, '../views/login.html'), (error, content) => {
if (error) console.log(error)
res.setHeader('Content-Type', 'text/html;charset=utf-8')
res.end(content)
})
}

module.exports.login = (req, res) => {

console.log(req.body)

const url = 'mongodb://localhost:27017/musicmanager'
// 连接数据库
MongoClient.connect(url, (error, db) => {
db.collection('account').findOne({
username: req.body.uname,
password: req.body.pwd,
status: 0
}, (error, doc) => {
if (error) console.log(error)
console.log(doc)
if (doc !== null) {
res.end('login success')
} else {
res.end('<script>alert("用户名或密码错误,请重新登录"); window.location.href = "/account/login"</script>')
}
})
})
}
35 changes: 35 additions & 0 deletions musicManager/src/controller/studentManagerController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'
const path = require('path')
const fs = require('fs')
const MongoClient = require('mongodb').MongoClient

module.exports.getStudentList = (req, res) => {
console.log('studentList')
fs.readFile(path.join(__dirname, '../views/login.html'), (error, content) => {
if (error) console.log(error)
res.setHeader('Content-Type', 'text/html;charset=utf-8')
res.end(content)
})
}

module.exports.login = (req, res) => {
console.log(req.body)

const url = 'mongodb://localhost:27017/musicmanager'
// 连接数据库
MongoClient.connect(url, (error, db) => {
db.collection('account').findOne({
username: req.body.uname,
password: req.body.pwd,
status: 0
}, (error, doc) => {
if (error) console.log(error)
console.log(doc)
if (doc !== null) {
res.end('login success')
} else {
res.end('<script>alert("用户名或密码错误,请重新登录"); window.location.href = "/account/login"</script>')
}
})
})
}
9 changes: 7 additions & 2 deletions musicManager/src/router/accountRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
const express = require('express')
const path = require('path')

// 创建路由
const accountRouter = express.Router()

router.get('/login', accountCtrl.getLoginPage)
const accountCtrl = require(path.join(__dirname, '../controller/accountController'))

router.post('/login', accountCtrl.login)
accountRouter.get('/login', accountCtrl.getLoginPage)

accountRouter.post('/login', accountCtrl.login)

module.exports = accountRouter
14 changes: 14 additions & 0 deletions musicManager/src/router/studentManagerRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'
const express = require('express')
const path = require('path')

// 创建路由
const studentManagerRouter = express.Router()

const studentManagerCtrl = require(path.join(__dirname, '../controller/studentManagerController'))

studentManagerRouter.get('/login', studentManagerCtrl.getStudentList)

// studentManagerRouter.post('/login', studentManagerCtrl.login)

module.exports = studentManagerRouter
78 changes: 40 additions & 38 deletions musicManager/src/views/login.html
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
<!DOCTYPE html>
<html>

<head>
<title>欢迎登录</title>
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css">
<title>欢迎登录</title>
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css">
</head>

<body>
<div class="container" style="margin-top:180px; ">
<div class="container" style="margin-top:180px; ">

<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-heading">登录学生管理系统</div>
<div class="panel-body">
<form action="/account/login" method="post" class="form-horizontal">
<div class="form-group">
<label for="uname" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="uname" id="uname" placeholder="请输入用户名">
</div>
</div>
<div class="form-group">
<label for="pwd" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="pwd" id="upwd" placeholder="请输入密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登录</button>
&nbsp;&nbsp;
<input onclick="register();" type="button" value="注册" class="btn btn-danger">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function register() {
window.location.href = '/account/register';
}
</script>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-heading">登录音乐管理系统</div>
<div class="panel-body">
<form action="/account/login" method="post" class="form-horizontal">
<div class="form-group">
<label for="uname" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="uname" id="uname" placeholder="请输入用户名">
</div>
</div>
<div class="form-group">
<label for="pwd" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="pwd" id="upwd" placeholder="请输入密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登录</button> &nbsp;&nbsp;
<input onclick="register();" type="button" value="注册" class="btn btn-danger">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function register() {
window.location.href = '/account/register';
}
</script>
</body>
</html>

</html>
67 changes: 67 additions & 0 deletions musicManager/src/views/parent.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!-- 父页面中包含头部 左边的内容 还有右边的占位符 -->
<!DOCTYPE html>
<html lang="zh-CN">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title></title>
<!-- Bootstrap -->
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="/js/jquery.min.js"></script>>
<link rel="stylesheet" href="/css/site.css">
</head>

<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-5" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">欢迎进入后台管理</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-5">
<p class="navbar-text navbar-right">欢迎【{{loginedname}}】登录&nbsp;<a href="javascript:logout()" class="navbar-link">退出</a></p>
</div>
</div>
</nav>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="list-group">
<a href="#" class="list-group-item active">
学生管理
</a>
<a href="#" class="list-group-item">系统管理</a>
<a href="#" class="list-group-item">用户管理</a>
<a href="#" class="list-group-item">修改密码</a>
<a href="#" class="list-group-item">退出系统</a>
</div>
</div>
<div class="col-md-9">

{{{block ("body")}}}

</div>
</div>
</div>
<script type="text/javascript">
function logout(){
if (confirm("确认退出?")) {
window.location.href = '/account/logout';
}
}
</script>
</body>

</html>
73 changes: 73 additions & 0 deletions musicManager/src/views/studentlist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{{extend ("./parent.html")}}

{{#block ("body")}}
<div class="panel panel-default">
<div class="panel-heading">
<ol class="breadcrumb">
<li><a href="#">首页</a></li>
<li><a href="#">学生管理</a></li>
<li class="active">学生列表</li>
</ol>
</div>
<div class="panel-heading">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">学生姓名</span>
<input type="text" class="form-control" id="studentName" placeholder="请输入学生姓名进行查询" aria-describedby="basic-addon1" style="width:200px;margin-right:10px;" value="{{keyword}}">
<input type="button" class="btn btn-success" onclick='search()' value="查找" style="margin-right:10px;">
<input type="button" class="btn btn-success" value="新增" onclick="window.location='/studentmanager/add'">
</div>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>手机号码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{{#each(studentlist)}}
<tr>
<td>{{this.name}}</td>
<td>{{this.age}}</td>
<td>{{this.sex}}</td>
<td>{{this.phone}}</td>
<td>
<a href="/studentmanager/edit/{{this._id}}" class ="btn btn-success" >编辑</a> |

<input type="button" class ="btn btn-danger" onclick="del('{{this._id}}')" value="删除">
</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
function del(studentId) {
if (!confirm("确定要删除该条数据吗?")) {
return;
}

var url = '/studentmanager/delete/'+studentId;
$.getJSON(url,null,function(jsObj){
if (jsObj.status==0) {//失败
alert(jsObj.message)
}else{ //成功
//alert(jsObj.message)
window.location = window.location
}
})
}

//搜索
function search(){
var studentName = $("#studentName").val();

window.location.href="/studentmanager/list?keyword="+studentName;
}
</script>
{{/block}}

0 comments on commit d34a7e3

Please sign in to comment.