Try to refine the panic things.
This commit is contained in:
42
rest/bean.go
42
rest/bean.go
@ -29,3 +29,45 @@ func (this *Bean) PanicError(err error) {
|
||||
func (this *Bean) PanicWebError(msg string, httpStatusCode int) {
|
||||
panic(&WebError{Msg: msg, Code: httpStatusCode})
|
||||
}
|
||||
|
||||
//能找到一个user就找到一个
|
||||
func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *User {
|
||||
|
||||
//验证用户是否已经登录。
|
||||
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
||||
if err != nil {
|
||||
this.logger.Warn("cookie 信息不存在~")
|
||||
return nil
|
||||
}
|
||||
|
||||
sessionId := sessionCookie.Value
|
||||
|
||||
//去缓存中捞取看看
|
||||
cacheItem, err := CONTEXT.SessionCache.Value(sessionId)
|
||||
if err != nil {
|
||||
this.logger.Warn("获取缓存时出错了" + err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
if cacheItem == nil || cacheItem.Data() == nil {
|
||||
this.logger.Warn("cache item中已经不存在了 ")
|
||||
return nil
|
||||
}
|
||||
|
||||
if value, ok := cacheItem.Data().(*User); ok {
|
||||
return value
|
||||
} else {
|
||||
this.logger.Error("cache item中的类型不是*User ")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//获取当前登录的用户,找不到就返回登录错误
|
||||
func (this *Bean) checkUser(writer http.ResponseWriter, request *http.Request) *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