[ADD]退出登陆

This commit is contained in:
viletyy 2019-06-27 13:24:38 +08:00
parent 32629777fa
commit 3b2c6346cd
6 changed files with 116 additions and 9 deletions

View File

@ -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, "")

View File

@ -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)
}

13
syserrors/nousererror.go Normal file
View File

@ -0,0 +1,13 @@
package syserrors
type NoUserError struct {
UnKnowError
}
func (err NoUserError) Error() string {
return "请登陆系统"
}
func (err NoUserError) Code() int {
return 1001
}

View File

@ -20,7 +20,7 @@
<li class="layui-nav-item {{ if equrl "/message" .Path }}layui-this{{ end }}"><a href="/message">留言</a></li>
<li class="layui-nav-item {{ if equrl "/about" .Path }}layui-this{{ end }}"><a href="/about">关于</a></li>
</ul>
<a href="#" class="personal pull-left">
<a href="/user" class="personal pull-left">
<i class="layui-icon layui-icon-username"></i>
</a>
</div>

66
views/user.html Normal file
View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>关于-闲言轻博客</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
{{ template "shares/link.html". }}
<style>
.layui-form-label {
width: 50px;
}
.layui-input-block {
margin-left: 80px;
}
@media screen and (max-width: 450px) {
.layui-form-item .layui-input-inline {
margin: 0 0 10px 80px;
}
}
</style>
</head>
<body class="lay-blog">
{{ template "shares/header.html". }}
<div class="container-wrap">
<div class="container container-message container-details container-about">
<div class="contar-wrap">
<div class="item">
<div class="item-box">
{{ if .IsLogin }}
<h4 class="item-title">
<p><i class="layui-icon layui-icon-speaker"></i>
{{.User.Name}},您已经登陆,是否<a href="/signout"><span>登出</span></a>
</p></h4>
{{ else }}
{{ end }}
</div>
</div>
</div>
</div>
</div>
{{ template "shares/footer.html". }}
<script>
layui.use(['form', 'jquery', 'layer', 'sysn'], function () {
var form = layui.form,
sysn = layui.sysn,
$ = layui.jquery,
layer = layui.layer;
//监听提交
form.on('submit(login)', function (formData) {
sysn.post("/signin", formData.field)
// .setTimeout(5000)
.success(function (data) {
layer.msg(data.msg);
if (data.action) {
setTimeout(function () {
window.location.href = data.action;
}, 300)
}
}).run();
return false;
});
});
</script>
</body>
</html>