用户信息传到service
CI / changes (push) Successful in 1s
CI / docker-bff (push) Successful in 1s
CI / docker-product (push) Failing after 1s

This commit is contained in:
2026-08-10 15:46:49 +08:00
parent 38a8a5cafb
commit 7228d3fedc
19 changed files with 505 additions and 74 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ func GetConfigString(key string) string {
}
func GetConfigInt(key string) int {
data := make(map[string]int)
data["base.login_out_time"] = 86400
data["base.login_out_time"] = 60 * 24 * 30 //分
return data[key]
}
func GetConfigInt64(key string) int64 { return runViper.GetInt64(key) }
+67
View File
@@ -0,0 +1,67 @@
package utils
import (
"context"
"net/url"
"strconv"
"google.golang.org/grpc/metadata"
)
type UserInfo struct {
ID int64
Name string
RawUID string
Refresh string
Valid bool
}
// GetUserFromCtx 从rpc metadata获取用户
func GetUserFromCtx(ctx context.Context) UserInfo {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return UserInfo{Valid: false}
}
uidList := md.Get("x-user-id")
nameList := md.Get("x-user-name")
refreshList := md.Get("x-refresh")
rawUID := ""
encodeName := ""
refresh := ""
if len(uidList) > 0 {
rawUID = uidList[0]
}
if len(nameList) > 0 {
encodeName = nameList[0]
}
if len(refreshList) > 0 {
refresh = refreshList[0]
}
if rawUID == "" {
return UserInfo{Valid: false}
}
uid, err := strconv.ParseInt(rawUID, 10, 64)
if err != nil {
return UserInfo{
RawUID: rawUID,
Name: encodeName,
Refresh: refresh,
Valid: false,
}
}
// 解码中文
userName, _ := url.QueryUnescape(encodeName)
return UserInfo{
ID: uid,
Name: userName,
RawUID: rawUID,
Refresh: refresh,
Valid: true,
}
}
+7 -4
View File
@@ -45,8 +45,10 @@ const (
const (
// LoginUser 所有的登录 加上服务名:id
// %s 为,admin,user ,...
LoginUser = "login:service:%s:"
LoginUserKey = "login:service:%s:keys"
Login = "login:service:%s:"
LoginKey = "login:service:%s:keys"
LoginTypeAdmin = "admin"
LoginTypeUser = "user"
CodeKey = "code:"
)
@@ -62,6 +64,7 @@ var (
ErrorPwdError = Status{Code: 10002, Msg: "密码错误"}
ErrorNoLogin = Status{Code: 10003, Msg: "没有登录"}
ErrorAuthority = Status{Code: 10008, Msg: "没有权限"}
ErrorNoLoginInfo = Status{Code: 10003, Msg: "没有获取到登录信息"}
// 基础类
ErrorExist = NewError(998, "已存在")
@@ -101,9 +104,9 @@ func SetError(error Error, msg string) Error {
return NewError(error.GetCode(), msg)
}
func GetLoginKey(Type, key string) string {
return fmt.Sprintf(LoginUser, Type) + key
return fmt.Sprintf(Login, Type) + key
}
func GetLoginKeysKey(Type string) string {
return fmt.Sprintf(LoginUserKey, Type)
return fmt.Sprintf(LoginKey, Type)
}