Commit 715c6ab2f64ee7800983e0d3b0904e0d300f542b

Authored by Administrator
1 parent 66245c3a
Exists in master

login function is ok

conf/app.conf
1 1 appname = abc
2 2 httpport = 80
3 3 runmode = dev
  4 +uname=admin
  5 +pwd=admin
... ...
controllers/default.go
... ... @@ -1,16 +0,0 @@
1   -package controllers
2   -
3   -import (
4   - "github.com/astaxie/beego"
5   -)
6   -
7   -type HomeController struct {
8   - beego.Controller
9   -}
10   -
11   -
12   -func (this *HomeController) Get() {
13   - this.TplName = "home.html"
14   -
15   -
16   -}
controllers/home.go 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/astaxie/beego"
  5 +)
  6 +
  7 +type HomeController struct {
  8 + beego.Controller
  9 +}
  10 +
  11 +
  12 +func (this *HomeController) Get() {
  13 + this.TplName = "home.html"
  14 + this.Data["IsHome"] = true
  15 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  16 +
  17 +
  18 +}
... ...
controllers/login.go 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/astaxie/beego"
  5 + "github.com/astaxie/beego/context"
  6 +)
  7 +
  8 +type LoginController struct {
  9 + beego.Controller
  10 +}
  11 +
  12 +
  13 +func (this *LoginController) Get() {
  14 + this.TplName = "login.html"
  15 + //get方法的input.get指的是http://localhost/login?exit=true,并不是http的body和head部分
  16 + //post方法里面的input.get指的是http的body中的表单
  17 + isExit := this.Input().Get("exit") == "true"
  18 + if isExit {
  19 + this.Ctx.SetCookie("uname","",-1,"/")
  20 + this.Ctx.SetCookie("pwd","",-1,"/")
  21 + this.Redirect("/",302)
  22 + return
  23 + }
  24 +}
  25 +
  26 +func (this *LoginController) Post() {
  27 + uname := this.Input().Get("uname")
  28 + pwd := this.Input().Get("pwd")
  29 + autoLogin := this.Input().Get("autoLogin") == "on"
  30 +
  31 + if beego.AppConfig.String("uname") == uname &&
  32 + beego.AppConfig.String("pwd") == pwd {
  33 + //如果没有勾选自动登录,浏览器关闭自动失效
  34 + //maxage为0表示浏览器关闭失效,设置为-1表示立即失效
  35 + maxAge := 0
  36 + if autoLogin {
  37 + maxAge = 1<<31 -1
  38 + }
  39 + this.Ctx.SetCookie("uname",uname,maxAge,"/")
  40 + this.Ctx.SetCookie("pwd",pwd,maxAge,"/")
  41 + }
  42 + //不管密码是否正确,都跳转
  43 + this.Redirect("/",302)
  44 + return
  45 +}
  46 +
  47 +func checkAccount(ctx *context.Context) bool {
  48 + ck, err := ctx.Request.Cookie("uname")
  49 + if err != nil {
  50 + return false
  51 + }
  52 + uname := ck.Value
  53 +
  54 + ck, err = ctx.Request.Cookie("pwd")
  55 + if err != nil {
  56 + return false
  57 + }
  58 + pwd := ck.Value
  59 + return beego.AppConfig.String("uname") == uname &&
  60 + beego.AppConfig.String("pwd") == pwd
  61 +}
  62 +
  63 +
... ...
routers/router.go
... ... @@ -7,4 +7,5 @@ import (
7 7  
8 8 func init() {
9 9 beego.Router("/", &controllers.HomeController{})
  10 + beego.Router("/login", &controllers.LoginController{})
10 11 }
... ...
views/header.html 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +{{define "header"}}
  2 +<!DOCTYPE html>
  3 +
  4 +<html>
  5 + <head>
  6 + <link rel="shortcut icon" href="/static/img/favicon.png" />
  7 + <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css" />
  8 +{{end}}
0 9 \ No newline at end of file
... ...
views/home.html
1   -<!DOCTYPE html>
2   -
3   -<html>
4   - <head>
  1 +{{template "header"}}
5 2 <title>首页 - 我的 beego 博客</title>
6   - <link rel="shortcut icon" href="/static/img/favicon.png" />
7   - <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css" />
8 3 </head>
9 4  
10 5 <body>
11 6 <div class="navbar navbar-default navbar-fixed-top">
12 7 <div class="container">
13   - <a class="navbar-brand" href="/">我的博客</a>
14   - <div>
15   - <ul class="nav navbar-nav">
16   - <li class="active"><a href="/">首页</a></li>
17   - <li><a href="/category">分类</a></li>
18   - <li><a href="/topic">文章</a></li>
19   - </ul>
20   - </div>
  8 + {{template "navbar" .}}
21 9 </div>
22 10 </div>
23 11 <div class="container">
... ...
views/login.html 0 → 100644
... ... @@ -0,0 +1,81 @@
  1 +{{template "header"}}
  2 +<title xmlns="http://www.w3.org/1999/html">登录 - 我的 beego 博客</title>
  3 + </head>
  4 +
  5 + <body>
  6 + <div class="container" style="width: 500px;">
  7 + <form class="form-horizontal" method="POST" action="/login">
  8 + <div class="form-group">
  9 + <label class="col-1g-4 control-label">请输入管理员账号:</label>
  10 + <div class="col-1g-6">
  11 + <input id="uname" class="form-control" name="uname" placeholder="Account">
  12 + </div>
  13 + </div>
  14 + <div class="form-group">
  15 + <label class="col-1g-4 control-label">请输入管理员密码:</label>
  16 + <div class="col-1g-6">
  17 + <input id="pwd" type="password" class="form-control" name="pwd" placeholder="Password">
  18 + </div>
  19 + </div>
  20 + <div class="form-group">
  21 + <div class="col-1g-offset-4 col-1g-10">
  22 + <div class="checkbox">
  23 + <label>
  24 + <input name="autoLogin" type="checkbox">下次自动登录
  25 + </label>
  26 + </div>
  27 + </div>
  28 + </div>
  29 + <div class="form-group">
  30 + <div class="col-1g-offset-2 col-1g-10">
  31 + <a href="/login"><button type="submit" class="btn btn-default" onclick="return checkInput();">登录</button> </a>
  32 + <button class="btn btn-default" onclick="return backToHome();">返回</button>
  33 + <script type="text/javascript">
  34 + function backToHome() {
  35 + windows.location.href = "/";
  36 + return false;
  37 + }
  38 + function checkInput() {
  39 + var uname = document.getElementById("uname");
  40 + if (uname.value.length ==0) {
  41 + alert("请输入管理员账号");
  42 + return false;
  43 + }
  44 + var pwd = document.getElementById("pwd");
  45 + if (pwd.value.length ==0) {
  46 + alert("请输入管理员密码");
  47 + return false;
  48 + }
  49 + }
  50 + </script>
  51 + </div>
  52 + </div>
  53 + </form>
  54 + </div>
  55 + </body>
  56 +</html>
  57 +
  58 +
  59 +
  60 +
  61 +
  62 +
  63 +
  64 +
  65 +
  66 +
  67 +
  68 +
  69 +
  70 +
  71 +
  72 +
  73 +
  74 +
  75 +
  76 +
  77 +
  78 +
  79 +
  80 +
  81 +
... ...
views/navbar.html 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +{{define "navbar"}}
  2 +<a class="navbar-brand" href="/">我的博客</a>
  3 +<div>
  4 + <ul class="nav navbar-nav">
  5 + <li {{if .IsHome}}class="active"{{end}}><a href="/">首页</a></li>
  6 + <li {{if .IsCategory}}class="active"{{end}}><a href="/category">分类</a></li>
  7 + <li {{if .IsTopic}}class="active"{{end}}><a href="/topic">文章</a></li>
  8 + </ul>
  9 +</div>
  10 +
  11 +<div class="pull-right">
  12 + <u class="nav navbar-nav">
  13 + {{if .IsLogin}}
  14 + <li><a href="/login?exit=true">退出登录</a></li>
  15 + {{else}}
  16 + <li><a href="/login">管理员登录</a></li>
  17 + {{end}}
  18 + </u>
  19 +</div>
  20 +{{end}}
0 21 \ No newline at end of file
... ...