info.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "xingjia-management-gateway/apis"
  14. "xingjia-management-gateway/errors"
  15. dbmodel "xingjia-management-gateway/model"
  16. )
  17. func checkUserInfoParam(req *apis.UserInfoRequest) error {
  18. if req.Uid == 0 {
  19. return status.Error(10003, "未知操作者")
  20. }
  21. return nil
  22. }
  23. //
  24. func UserInfo(ctx context.Context, req *apis.UserInfoRequest) (reply *apis.UserInfoReply, err error) {
  25. reply = &apis.UserInfoReply{}
  26. // 捕获各个task中的异常并返回给调用者
  27. defer func() {
  28. if r := recover(); r != nil {
  29. err = fmt.Errorf("%+v", r)
  30. e := &status.Status{}
  31. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  32. logger.Error("err",
  33. zap.String("system_err", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }
  37. }()
  38. if err = checkUserInfoParam(req); err != nil {
  39. return nil, err
  40. }
  41. where := map[string]interface{}{
  42. "id": req.Uid,
  43. }
  44. p := &dbmodel.TUser{}
  45. err = p.Find(database.DB(), where)
  46. if err != nil && err != gorm.ErrRecordNotFound {
  47. return nil, errors.DataBaseError
  48. }
  49. if p.ID == 0 {
  50. return nil, errors.ErrRecordNotFound
  51. }
  52. reply.User = p.User
  53. reply.RealName = p.RealName
  54. reply.UserType = p.UserType
  55. reply.EffectiveStart = p.EffectiveStart
  56. reply.EffectiveEnd = p.EffectiveEnd
  57. reply.Uid = req.Uid
  58. return reply, nil
  59. }