版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33781658/article/details/86130768
beego登录记住用户名
本来这是一个非常非常简单的业务
但是自己太粗心
结果搞了好久
真是无语
开始吧
首先我们有登录业务
<form method="post" action="/login">
<input type="text" name="username" class="name_input" placeholder="请输入用户名" value="{{.username}}">
<input type="password" name="pwd" class="pass_input" placeholder="请输入密码">
<div class="more_input clearfix">
<input type="checkbox" name="remember" {{.checked}}>
<label>记住用户名</label>
</div>
<input type="submit" name="" value="登录" class="input_submit">
</form>
func (this *UserController) ShowLogin() {
this.TplName = "login.html"
}
func (this *UserController) HandleLogin() {
username := this.GetString("username")
password := this.GetString("pwd")
this.SetSession("username", username)
this.Redirect("/index", 302)
}
我把无关代码都删掉了
现在是点登录按钮
就跳转到index页面
现在我们来看一下逻辑
我们点了登录按钮之后
要获取remember是什么状态
func (this *UserController) HandleLogin() {
username := this.GetString("username")
password := this.GetString("pwd")
remember := this.GetString("remember")
if remember == "on" {
this.Ctx.SetCookie("username", username, time.Second*60*60*24)
} else {
this.Ctx.SetCookie("username", username, -1)
}
this.SetSession("username", username)
this.Redirect("/register", 302)
}
所以这里我们GetString获取remember
如果是on的话
就要设置cookie
然后我们再看显示登录页面
func (this *UserController) ShowLogin() {
username := this.Ctx.GetCookie("username")
if username != "" {
this.Data["username"] = username
this.Data["checked"] = "checked"
} else {
this.Data["username"] = ""
this.Data["checked"] = ""
}
this.TplName = "login.html"
}