golang web 开发 实现校园浴室预约方案
登陆验证模块
实体类
//Student 学生实体类
type Student struct {
ID int
StudentID string
Password string
Name string
}
//Room 浴室实体类
type Room struct {
ID int
Type string
}
//Logstr 记录信息
type Logstr struct {
ID int
StudentID string
RoomID string
Type string
TimeStr string
EndTime string
}
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
<script type="text/javascript" src="/static/js/jquery-3.4.1.js"></script>
<title></title>
</head>
<body>
<form action="/login" method="POST">
<p>学号<input type="text" id="studentid" name = "studentid" value=""/></p>
<p>密码<input type="text" id="password" name = "password" value=""/></p>
<input type="submit" id="but1" value="提交" />
</form>
<p><b> {
{
.}}</b></P>
</body>
</html>
建立web服务
const (
countTotalRoomSQL = "select count(*) from room"
countOccupyRoomSQL = "select count(*) from room where type ='0' "
findRoomSQL = "select * from room where type ='0'"
updateRoomSQL = "update room set type = $1 where id = $2"
insertLogSQL = "insert into log (student_id,room_id,type,timestr,end_time) values($1,$2,$3,$4,$5) returning id"
updateLogSQL = "update log set type = '0' where student_id = $1 and type = '1'"
findLogSQL = "select room_id from log where type = '1' and student_id = $1"
clearLogSQL = "update log set type= '0' where end_time <now() and type = '1' returning room_id"
findRoomByTypeAndStudentIDSQL = "select id,timestr from log where student_id=$1 and type = '1'"
//查找对象语句
selectSQL = "select * from student where student_id = $1 and password = $2"
)
var (
sessionMgr *SessionMgr = nil //session管理器
mu sync.Mutex
)
func init() {
sessionMgr = NewSessionMgr("TestCookieName", 3600)
}
func HTTPTest() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/login", login)
http.HandleFunc("/index", addpHanderFunc(index))
http.HandleFunc("/success", addpHanderFunc(success))
http.HandleFunc("/exitRoom", addpHanderFunc(exitRoom))
err := http.ListenAndServe("localhost:8080", nil)
if err != nil {
fmt.Println(err)
}
}
//将验证用户是否登陆包装到路由中
func addpHanderFunc(a http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if testToken(w, r) {
a(w, r)
}
}
}
登陆验证方法
index 方法 以及接收前端账号密码 验证返回页面
func index(w http.ResponseWriter, r *http.Request) {
var (
viewEntity View1
Entity View2
)
//这里是判断用户之前有没有预约的数据如果有直接进入预约成功的界面
loginUser := getLoginUser(w, r)
DB.QueryRow(findRoomByTypeAndStudentIDSQL, loginUser.StudentID).Scan(&Entity.Number, &Entity.TimeStr)
if Entity.Number != "" && Entity.TimeStr != "" {
tmpl, err := template.ParseFiles("view/success.html")
if err != nil {
fmt.Println(err)
}
tmpl.Execute(w, Entity)
return
}
fmt.Println("登陆成功这是登陆用户", getLoginUser(w, r))
t, err := template.ParseFiles("view/index.html")
if err != nil {
Mylog.Println(err)
}
viewEntity.Count = Count()
t.Execute(w, viewEntity)
}
//处理登录
func login(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
fmt.Println("请求到了登陆页面")
t, _ := template.ParseFiles("view/login.html")
t.Execute(w, nil)
} else if r.Method == "POST" {
fmt.Println("请求到了验证")
//请求的是登陆数据,那么执行登陆的逻辑判断
r.ParseForm()
//可以使用template.HTMLEscapeString()来避免用户进行js注入
var student Student
studentid := r.FormValue