From 2a9d8cc5dfb3f2a9e8441b08dd79c859cc06e0ea Mon Sep 17 00:00:00 2001 From: Minho Date: Fri, 9 Mar 2018 10:55:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3Cookie=E8=AE=B0=E4=BD=8F?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/app.conf.example | 3 ++- controllers/account.go | 15 +++++---------- controllers/base.go | 30 +++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/conf/app.conf.example b/conf/app.conf.example index 72acf61e..9b60c560 100644 --- a/conf/app.conf.example +++ b/conf/app.conf.example @@ -5,7 +5,8 @@ runmode = dev sessionon = true sessionname = mindoc_id copyrequestbody = true - +#系统完整URL(http://doc.iminho.me),如果该项不设置,会从请求头中获取地址。 +baseurl= #默认Session生成Key的秘钥 beegoserversessionkey=123456 diff --git a/controllers/account.go b/controllers/account.go index 12b21450..dca04ee1 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -25,11 +25,6 @@ func (c *AccountController) Login() { c.Prepare() c.TplName = "account/login.tpl" - var remember struct { - MemberId int - Account string - Time time.Time - } if member, ok := c.GetSession(conf.LoginSessionName).(models.Member); ok && member.MemberId > 0 { u := c.GetString("url") @@ -41,13 +36,12 @@ func (c *AccountController) Login() { } c.Redirect(u,302) } - + var remember CookieRemember // 如果 Cookie 中存在登录信息 if cookie, ok := c.GetSecureCookie(conf.GetAppKey(), "login"); ok { if err := utils.Decode(cookie, &remember); err == nil { if member, err := models.NewMember().Find(remember.MemberId); err == nil { c.SetMember(*member) - c.LoggedIn(false) c.StopRun() } @@ -58,7 +52,7 @@ func (c *AccountController) Login() { account := c.GetString("account") password := c.GetString("password") captcha := c.GetString("code") - is_remember := c.GetString("is_remember") + isRemember := c.GetString("is_remember") // 如果开启了验证码 if v, ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v, "true") { @@ -74,13 +68,14 @@ func (c *AccountController) Login() { member.Update() c.SetMember(*member) - if strings.EqualFold(is_remember, "yes") { + beego.Info("是否记住密码:" , isRemember) + if strings.EqualFold(isRemember, "yes") { remember.MemberId = member.MemberId remember.Account = member.Account remember.Time = time.Now() v, err := utils.Encode(remember) if err == nil { - c.SetSecureCookie(conf.GetAppKey(), "login", v) + c.SetSecureCookie(conf.GetAppKey(), "login", v,time.Now().Add(time.Hour * 24 * 30).Unix()) } } u,_ := url.PathUnescape(c.GetString("url")) diff --git a/controllers/base.go b/controllers/base.go index ed891a48..148ea340 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -4,11 +4,14 @@ import ( "bytes" "encoding/json" + "io" + "strings" + "time" + "github.com/astaxie/beego" "github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/models" - "io" - "strings" + "github.com/lifei6671/mindoc/utils" ) type BaseController struct { @@ -19,11 +22,17 @@ type BaseController struct { EnableDocumentHistory bool } +type CookieRemember struct { + MemberId int + Account string + Time time.Time +} + // Prepare 预处理. func (c *BaseController) Prepare() { c.Data["SiteName"] = "MinDoc" - c.Data["Member"] = models.Member{} - controller,action := c.GetControllerAndAction() + c.Data["Member"] = models.NewMember() + controller, action := c.GetControllerAndAction() c.Data["ActionName"] = action c.Data["ControllerName"] = controller @@ -36,6 +45,17 @@ func (c *BaseController) Prepare() { c.Member = &member c.Data["Member"] = c.Member } else { + var remember CookieRemember + // //如果Cookie中存在登录信息,从cookie中获取用户信息 + if cookie, ok := c.GetSecureCookie(conf.GetAppKey(), "login"); ok { + if err := utils.Decode(cookie, &remember); err == nil { + if member, err := models.NewMember().Find(remember.MemberId); err == nil { + c.Member = member + c.Data["Member"] = member + c.SetMember(*member) + } + } + } //c.Member = models.NewMember() //c.Member.Find(1) //c.Data["Member"] = *c.Member @@ -138,5 +158,5 @@ func (c *BaseController) ShowErrorPage(errCode int, errMsg string) { c.Abort("500") } - c.CustomAbort(200,buf.String()) + c.CustomAbort(200, buf.String()) }