package service import ( "context" user "git.diulo.com/mogfee/kit/api" "git.diulo.com/mogfee/kit/middleware/jwt" ) type userServer struct { user.UnimplementedUserServer tokenKey string } func NewUserService(tokenKey string) user.UserServer { return &userServer{ tokenKey: tokenKey, } } func (userServer) List(ctx context.Context, request *user.Request) (*user.Response, error) { userInfo, _ := jwt.FromUserContext(ctx) return &user.Response{ Token: userInfo.UserName, }, nil } func (userServer) All(ctx context.Context, request *user.Request) (*user.Response, error) { userInfo, _ := jwt.FromUserContext(ctx) return &user.Response{ Token: userInfo.UserName, }, nil } func (userServer) Auto(ctx context.Context, request *user.Request) (*user.Response, error) { userInfo, ok := jwt.FromUserContext(ctx) if ok { return &user.Response{ Token: userInfo.UserName, }, nil } else { return &user.Response{ Token: "no login", }, nil } } func (s *userServer) LoginWithList(ctx context.Context, request *user.Request) (*user.Response, error) { token, _, err := jwt.GetToken(s.tokenKey, &jwt.UserInfo{ UserId: 1, UserName: "test", UserType: "user", Permissions: []string{"user:list"}, UniqueId: "", }) if err != nil { return nil, err } return &user.Response{Token: token}, nil } func (s *userServer) Login(ctx context.Context, request *user.Request) (*user.Response, error) { token, _, err := jwt.GetToken(s.tokenKey, &jwt.UserInfo{ UserId: 1, UserName: "test", UserType: "user", UniqueId: "", }) if err != nil { return nil, err } return &user.Response{Token: token}, nil } func (s *userServer) Login1(ctx context.Context, request *user.Request) (*user.Response, error) { token, _, err := jwt.GetToken(s.tokenKey, &jwt.UserInfo{ UserId: 1, UserName: "test", UserType: "user", UniqueId: "", }) if err != nil { return nil, err } return &user.Response{Token: token}, nil }