diff --git a/controllers/base.go b/controllers/base.go index 76ca9ae..450f473 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/astaxie/beego" "liteblog/models" + "liteblog/syserrors" ) const SESSION_USER_KEY = "SESSION_USER_KEY" @@ -37,12 +38,12 @@ func (ctx *BaseController) Prepare() { app.NestPrepare() } } -// -//func (ctx *BaseController) MustLogin() { -// if !ctx.IsLogin { -// ctx.Abort500(syserrors.NoUserError{}) -// } -//} + +func (ctx *BaseController) MustLogin() { + if !ctx.IsLogin { + ctx.Abort500(syserrors.NoUserError{}) + } +} func (ctx *BaseController) GetMustString(key string, msg string) string { KeyString := ctx.GetString(key, "") diff --git a/controllers/user.go b/controllers/user.go index 3bb22e7..0456937 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -12,16 +12,35 @@ type UserController struct { BaseController } +// 用户个人中心 +// @router /user [get] +func (c *UserController) User() { + if c.IsLogin { + c.TplName = "user.html" + } else { + c.Redirect("/login",200) + } +} + // 登陆页面 // @router /login [get] func (c *UserController) Login() { - c.TplName = "login.html" + if c.IsLogin { + c.Redirect("/user",200) + } else { + c.TplName = "login.html" + } + } // 注册页面 // @router /reg [get] func (c *UserController) Reg() { - c.TplName = "reg.html" + if c.IsLogin { + c.Redirect("/user",200) + } else { + c.TplName = "reg.html" + } } // 登陆接口 @@ -82,4 +101,12 @@ func (c *UserController) SignUp() { c.Abort500(syserrors.NewError("用户注册失败", err)) } c.JSONOk("注册成功", "/login") +} + +// 退出登陆接口 +// @router /signout [get] +func (c *UserController) SignOut() { + c.MustLogin() + c.DelSession(SESSION_USER_KEY) + c.Redirect("/", 302) } \ No newline at end of file diff --git a/syserrors/nousererror.go b/syserrors/nousererror.go new file mode 100644 index 0000000..82ca814 --- /dev/null +++ b/syserrors/nousererror.go @@ -0,0 +1,13 @@ +package syserrors + +type NoUserError struct { + UnKnowError +} + +func (err NoUserError) Error() string { + return "请登陆系统" +} + +func (err NoUserError) Code() int { + return 1001 +} \ No newline at end of file diff --git a/syserrors/unknow.go b/syserrors/unknowerror.go similarity index 100% rename from syserrors/unknow.go rename to syserrors/unknowerror.go diff --git a/views/shares/header.html b/views/shares/header.html index ece3324..086d6e7 100644 --- a/views/shares/header.html +++ b/views/shares/header.html @@ -20,7 +20,7 @@
  • 留言
  • 关于
  • - + diff --git a/views/user.html b/views/user.html new file mode 100644 index 0000000..bd05ac4 --- /dev/null +++ b/views/user.html @@ -0,0 +1,66 @@ + + + + + 关于-闲言轻博客 + + {{ template "shares/link.html". }} + + + +{{ template "shares/header.html". }} +
    +
    +
    +
    +
    + {{ if .IsLogin }} +

    +

    + {{.User.Name}},您已经登陆,是否登出? +

    + {{ else }} + {{ end }} +
    +
    +
    +
    +
    +{{ template "shares/footer.html". }} + + + \ No newline at end of file