base.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package grpc
  2. import (
  3. "context"
  4. "google.golang.org/grpc/metadata"
  5. pb "sportfitness/base/api/grpc/base"
  6. "sportfitness/base/assembly/base/service"
  7. "strings"
  8. )
  9. type Api struct {
  10. pb.UnimplementedApiServer
  11. }
  12. func (Api) getRemoteIp(ctx context.Context) string {
  13. if md, ok := metadata.FromIncomingContext(ctx); ok {
  14. ip := md.Get("x-forwarded-for")
  15. if len(ip) > 0 {
  16. return ip[0]
  17. }
  18. }
  19. return ""
  20. }
  21. func (Api) getUserAgent(ctx context.Context) string {
  22. if md, ok := metadata.FromIncomingContext(ctx); ok {
  23. agents := md.Get("user-agent")
  24. return strings.Join(agents, "\n")
  25. }
  26. return ""
  27. }
  28. func (Api) getToken(ctx context.Context) (token string) {
  29. if md, ok := metadata.FromIncomingContext(ctx); ok {
  30. sl := md.Get("token")
  31. if len(sl) >= 1 {
  32. token = sl[0]
  33. }
  34. }
  35. return
  36. }
  37. func (a Api) SignIn(ctx context.Context, q *pb.SignInRequest) (*pb.SignInReply, error) {
  38. token := service.User{}.SignInUserCodePassword(
  39. q.Name, q.Password, a.getRemoteIp(ctx), a.getUserAgent(ctx), q.CodeID, q.VerifyCode)
  40. return &pb.SignInReply{Token: token}, nil
  41. }
  42. func (a Api) GenVerifyImage(ctx context.Context, q *pb.GenVerifyImageRequest) (*pb.GenVerifyImageReply, error) {
  43. codeId, imageBase64 := service.User{}.GenVerifyImage(q.Height, q.Width)
  44. return &pb.GenVerifyImageReply{ImageBase64: imageBase64, CodeId: codeId}, nil
  45. }
  46. //func (a Api) SignUpInWithPhone() {
  47. //
  48. //}