Try to refine the panic things.

This commit is contained in:
zicla
2018-11-30 17:37:52 +08:00
parent 6485e3b48e
commit b55a993b35
10 changed files with 84 additions and 90 deletions

View File

@ -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)
}
}