Finish the session link.
This commit is contained in:
@ -5,7 +5,6 @@ import (
|
||||
"github.com/json-iterator/go"
|
||||
"go/types"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IController interface {
|
||||
@ -131,64 +130,47 @@ func (this *BaseController) Error(err interface{}) *WebResult {
|
||||
return webResult
|
||||
}
|
||||
|
||||
//能找到一个user就找到一个,遇到问题直接抛出错误
|
||||
func (this *BaseController) checkLogin(writer http.ResponseWriter, request *http.Request) (*Session, *User) {
|
||||
|
||||
//验证用户是否已经登录。
|
||||
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
||||
if err != nil {
|
||||
panic(ConstWebResult(CODE_WRAPPER_LOGIN))
|
||||
}
|
||||
|
||||
session := this.sessionDao.FindByUuid(sessionCookie.Value)
|
||||
if session == nil {
|
||||
panic(ConstWebResult(CODE_WRAPPER_LOGIN))
|
||||
} else {
|
||||
if session.ExpireTime.Before(time.Now()) {
|
||||
panic(ConstWebResult(CODE_WRAPPER_LOGIN_EXPIRE))
|
||||
} else {
|
||||
|
||||
user := this.userDao.FindByUuid(session.UserUuid)
|
||||
if user == nil {
|
||||
panic(ConstWebResult(CODE_WRAPPER_LOGIN))
|
||||
} else {
|
||||
return session, user
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//能找到一个user就找到一个
|
||||
func (this *BaseController) findUser(writer http.ResponseWriter, request *http.Request) *User {
|
||||
|
||||
//验证用户是否已经登录。
|
||||
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
||||
if err != nil {
|
||||
LogError("找不到任何登录信息")
|
||||
LogInfo("获取用户cookie时出错啦")
|
||||
return nil
|
||||
}
|
||||
|
||||
session := this.sessionDao.FindByUuid(sessionCookie.Value)
|
||||
if session != nil {
|
||||
if session.ExpireTime.Before(time.Now()) {
|
||||
LogError("登录信息已过期")
|
||||
return nil
|
||||
} else {
|
||||
user := this.userDao.FindByUuid(session.UserUuid)
|
||||
if user != nil {
|
||||
return user
|
||||
}
|
||||
}
|
||||
sessionId := sessionCookie.Value
|
||||
|
||||
LogInfo("findUser sessionId = " + sessionId)
|
||||
|
||||
//去缓存中捞取看看
|
||||
cacheItem, err := CONTEXT.SessionCache.Value(sessionId)
|
||||
if err != nil {
|
||||
LogError("获取缓存时出错了" + err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
if cacheItem.Data() == nil {
|
||||
LogError("cache item中已经不存在了 " + err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
if value, ok := cacheItem.Data().(*User); ok {
|
||||
return value
|
||||
} else {
|
||||
LogError("cache item中的类型不是*User ")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *BaseController) checkUser(writer http.ResponseWriter, request *http.Request) *User {
|
||||
_, user := this.checkLogin(writer, request)
|
||||
return user
|
||||
if this.findUser(writer, request) == nil {
|
||||
panic(ConstWebResult(CODE_WRAPPER_LOGIN))
|
||||
} else {
|
||||
return this.findUser(writer, request)
|
||||
}
|
||||
}
|
||||
|
||||
//允许跨域请求
|
||||
|
Reference in New Issue
Block a user