From d12976f68ef3c7819c9330f4daebc86f71bb642f Mon Sep 17 00:00:00 2001 From: ouqiang Date: Sun, 23 Apr 2017 19:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E9=99=86=E5=90=8E?= =?UTF-8?q?=E4=BF=9D=E5=AD=98uid=E5=88=B0session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/web.go | 11 +++-------- models/user.go | 3 +-- routers/routers.go | 1 + routers/user/user.go | 20 ++++++++++++++------ templates/common/header.html | 4 +++- templates/task/task_form.html | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 7a51462..29210e6 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -35,10 +35,9 @@ var CmdWeb = cli.Command{ Value: "prod", Usage: "runtime environment, dev|test|prod", }, - cli.StringFlag{ + cli.BoolFlag{ Name: "d", - Value: "false", - Usage: "-d=true, run app as daemon, not support windows", + Usage: "-d, run app as daemon, not support windows", }, }, } @@ -111,11 +110,7 @@ func becomeDaemon(ctx *cli.Context) { if utils.IsWindows() { return } - var daemond string = "false" - if ctx.IsSet("d") { - daemond = ctx.String("d") - } - if (daemond != "true") { + if !ctx.IsSet("d") { return } diff --git a/models/user.go b/models/user.go index 5de7ebe..29dac5a 100644 --- a/models/user.go +++ b/models/user.go @@ -19,8 +19,7 @@ type User struct { Deleted time.Time `xorm:"datetime deleted"` IsAdmin int8 `xorm:"tinyint notnull default 0"` // 是否是管理员 1:管理员 0:普通用户 Status Status `xorm:"tinyint notnull default 1"` // 1: 正常 0:禁用 - Page int `xorm:"-"` - PageSize int `xorm:"-"` + BaseModel `xorm:"-"` } // 新增 diff --git a/routers/routers.go b/routers/routers.go index 099883c..691590e 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -163,6 +163,7 @@ func setShareData(m *macaron.Macaron) { ctx.Data["Action"] = paths[1] } ctx.Data["LoginUsername"] = user.Username(sess) + ctx.Data["LoginUid"] = user.Uid(sess) }) } diff --git a/routers/user/user.go b/routers/user/user.go index 2718510..cc572a7 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -6,7 +6,6 @@ import ( "github.com/ouqiang/gocron/models" "github.com/go-macaron/session" "github.com/ouqiang/gocron/modules/logger" - "time" ) // @author qiang.ou @@ -30,20 +29,20 @@ func ValidateLogin(ctx *macaron.Context, sess session.Store) string { return json.CommonFailure("用户名或密码错误") } - sess.Set("username", username) + sess.Set("username", userModel.Name) + sess.Set("uid", userModel.Id) return json.Success("登录成功", nil) } func Logout(ctx *macaron.Context, sess session.Store) { if IsLogin(sess) { - err := sess.Delete("username") + err := sess.Destory(ctx) if err != nil { logger.Error("用户退出登录失败", err) } } - ctx.SetSecureCookie("MacaronSession", "", 0, "/", "", nil, nil, time.Now().AddDate(-1, 0, 0)) Login(ctx) } @@ -56,9 +55,18 @@ func Username(sess session.Store) string { return "" } +func Uid(sess session.Store) int { + uid,ok := sess.Get("uid").(int) + if ok { + return uid + } + + return 0 +} + func IsLogin(sess session.Store) bool { - username,ok := sess.Get("username").(string) - if ok && username != "" { + uid, ok := sess.Get("uid").(int) + if ok && uid > 0 { return true } diff --git a/templates/common/header.html b/templates/common/header.html index 45a1f1c..6016baa 100644 --- a/templates/common/header.html +++ b/templates/common/header.html @@ -59,8 +59,10 @@ \ No newline at end of file diff --git a/templates/task/task_form.html b/templates/task/task_form.html index ad1cacc..7df2f19 100644 --- a/templates/task/task_form.html +++ b/templates/task/task_form.html @@ -55,7 +55,7 @@ -
+