You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.6 KiB
72 lines
1.6 KiB
|
7 years ago
|
package rest
|
||
|
8 years ago
|
|
||
|
7 years ago
|
import (
|
||
|
7 years ago
|
"github.com/eyebluecn/tank/code/core"
|
||
|
7 years ago
|
"github.com/eyebluecn/tank/code/tool/result"
|
||
|
|
"github.com/eyebluecn/tank/code/tool/util"
|
||
|
7 years ago
|
"net/http"
|
||
|
|
)
|
||
|
8 years ago
|
|
||
|
7 years ago
|
type BaseBean struct {
|
||
|
7 years ago
|
logger core.Logger
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
7 years ago
|
func (this *BaseBean) Init() {
|
||
|
7 years ago
|
this.logger = core.LOGGER
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
7 years ago
|
func (this *BaseBean) Bootstrap() {
|
||
|
7 years ago
|
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
//系统大清理,一般时产品即将上线时,清除脏数据,只执行一次。
|
||
|
7 years ago
|
func (this *BaseBean) Cleanup() {
|
||
|
7 years ago
|
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
//处理错误的统一方法 可以省去if err!=nil 这段代码
|
||
|
7 years ago
|
func (this *BaseBean) PanicError(err error) {
|
||
|
7 years ago
|
util.PanicError(err)
|
||
|
8 years ago
|
}
|
||
|
8 years ago
|
|
||
|
7 years ago
|
//能找到一个user就找到一个
|
||
|
7 years ago
|
func (this *BaseBean) findUser(writer http.ResponseWriter, request *http.Request) *User {
|
||
|
7 years ago
|
|
||
|
|
//验证用户是否已经登录。
|
||
|
7 years ago
|
//登录身份有效期以数据库中记录的为准
|
||
|
7 years ago
|
sessionId := util.GetSessionUuidFromRequest(request, core.COOKIE_AUTH_KEY)
|
||
|
7 years ago
|
if sessionId == "" {
|
||
|
7 years ago
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
//去缓存中捞取看看
|
||
|
7 years ago
|
cacheItem, err := core.CONTEXT.GetSessionCache().Value(sessionId)
|
||
|
7 years ago
|
if err != nil {
|
||
|
|
this.logger.Warn("获取缓存时出错了" + err.Error())
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
if cacheItem == nil || cacheItem.Data() == nil {
|
||
|
7 years ago
|
|
||
|
7 years ago
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
//获取当前登录的用户,找不到就返回登录错误
|
||
|
7 years ago
|
func (this *BaseBean) checkUser(writer http.ResponseWriter, request *http.Request) *User {
|
||
|
7 years ago
|
if this.findUser(writer, request) == nil {
|
||
|
7 years ago
|
panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN))
|
||
|
7 years ago
|
} else {
|
||
|
|
return this.findUser(writer, request)
|
||
|
|
}
|
||
|
|
}
|