作者 yangfu

chore: UserToken valid

... ... @@ -9,6 +9,7 @@ HTTP_PORT = 8081
ENABLE_KAFKA_LOG11 = true
HTTPS_PORT = 8143
ALLIED_CREATION_USER_HOST = http://allied-creation-user-dev.fjmaimaimai.com
# AUTH_SERVER_HOST = http://127.0.0.1:8081
BYTE_CORE_HOST = http://47.97.5.102:8303
METADATA_BASTION_HOST = http://106.75.231.90:9999
... ...
... ... @@ -24,7 +24,7 @@ var METADATA_BASTION_HOST = "http://127.0.0.1:8080"
var BYTE_CORE_HOST = "http://192.168.100.34:8303"
var AUTH_SERVER_HOST = "http://digital-platform-dev.fjmaimaimai.com/"
var AUTH_SERVER_HOST = "http://digital-platform-dev.fjmaimaimai.com"
//var CUSTOMER_ACCOUNT = []int64{3129687560814592, 3129687690100739, 3492238958608384}
... ... @@ -45,6 +45,7 @@ func init() {
BYTE_CORE_HOST = Configurator.DefaultString("BYTE_CORE_HOST", BYTE_CORE_HOST)
SERVICE_ENV = Configurator.DefaultString("SERVICE_ENV", SERVICE_ENV)
HTTP_PORT = Configurator.DefaultInt("HTTP_PORT", HTTP_PORT)
AUTH_SERVER_HOST = Configurator.DefaultString("AUTH_SERVER_HOST", AUTH_SERVER_HOST)
SERVICE_NAME = fmt.Sprintf("%v-%v", SERVICE_NAME, SERVICE_ENV)
PPROF_ON = Configurator.DefaultBool("PPROF_ON", PPROF_ON)
CACHE_PREFIX = SERVICE_NAME + ":" + SERVICE_ENV
... ...
... ... @@ -39,3 +39,8 @@ func (tk *UserToken) ParseToken(str string) error {
}
return errors.New("token 解析失败")
}
// CheckUserInfo 如果UserToken有效 返回:true 否则返回false
func (tk *UserToken) CheckUserInfo() bool {
return !(tk.UserId > 100000000 || tk.UserId <= 0)
}
... ...
package authlib
type RequestUserMeQuery struct {
Token string
//Token string
UserId int
CompanyId int
}
type DataUserMe struct {
... ...
... ... @@ -49,15 +49,18 @@ func ParseContext(c beego.BaseController) *domain.Context {
userToken, ok := token.(*domain.UserToken)
if ok {
// cache user info
key := cache.KeyCompanyUser(companyId, userId)
if userToken.UserId != 0 {
key := cache.KeyCompanyUser(int(userToken.CompanyId), int(userToken.UserId))
if userToken.CheckUserInfo() {
if cacheItem, ok := cache.DefaultCache.Get(key); ok {
v := cacheItem.(*authlib.DataUserMe)
userName = v.User.NickName
} else {
requestToken, _ := userToken.GenerateToken()
authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST).WithToken(requestToken)
userInfo, err := authLib.MeInfo(authlib.RequestUserMeQuery{})
userInfo, err := authLib.MeInfo(authlib.RequestUserMeQuery{
UserId: int(userToken.UserId),
CompanyId: int(userToken.CompanyId),
})
if err != nil || userInfo == nil || userInfo.User == nil {
goto END
}
... ...