用户接口远程调用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.2 KiB

3 years ago
  1. package user
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. )
  5. func GetUserByToken(dbname, token string, url ...string) (map[string]string, error) {
  6. var user_rpc_url string = "127.0.0.1:7976"
  7. if len(url) > 0 && url[0] != "" {
  8. user_rpc_url = url[0]
  9. }
  10. conn, _, err := DialUserService("tcp", user_rpc_url)
  11. if err != nil {
  12. return map[string]string{}, err
  13. }
  14. defer conn.Close()
  15. req := &UserRequest{proto.String(dbname), proto.String(token), nil}
  16. res := &UserResponse{}
  17. err = conn.GetByToken(req, res)
  18. if err != nil {
  19. return map[string]string{}, err
  20. }
  21. if res.GetUserId() != "" {
  22. return map[string]string{
  23. "UserId": res.GetUserId(),
  24. "Username": res.GetUsername(),
  25. "Nickname": res.GetNickname(),
  26. "Mobile": res.GetMobile(),
  27. "Email": res.GetEmail(),
  28. "Status": res.GetStatus(),
  29. "BusinessId": res.GetBusinessId(),
  30. "StoreId": res.GetStoreId(),
  31. "FansTo": res.GetFansTo(),
  32. "IsVip": res.GetIsVip(),
  33. "Usercode": res.GetUsercode(),
  34. "GroupId": res.GetGroupId(),
  35. "Type": res.GetType(),
  36. }, nil
  37. }
  38. return map[string]string{}, nil
  39. }
  40. /**
  41. * 使用用户名查询
  42. */
  43. func GetUserByUsername(dbname, username string, url ...string) (*UserResponse, error) {
  44. var user_rpc_url string = "127.0.0.1:7976"
  45. if len(url) > 0 && url[0] != "" {
  46. user_rpc_url = url[0]
  47. }
  48. conn, _, err := DialUserService("tcp", user_rpc_url)
  49. if err != nil {
  50. return nil, err
  51. }
  52. defer conn.Close()
  53. req := &UserInfoByUsername{proto.String(dbname), proto.String(username), nil}
  54. res := &UserResponse{}
  55. err = conn.GetByUsername(req, res)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return res, nil
  60. }
  61. /**
  62. * 使用编号查询
  63. */
  64. func GetUserByUsercode(dbname, usercode string, url ...string) (*UserResponse, error) {
  65. var user_rpc_url string = "127.0.0.1:7976"
  66. if len(url) > 0 && url[0] != "" {
  67. user_rpc_url = url[0]
  68. }
  69. conn, _, err := DialUserService("tcp", user_rpc_url)
  70. if err != nil {
  71. return nil, err
  72. }
  73. defer conn.Close()
  74. req := &UserInfoByUsercode{proto.String(dbname), proto.String(usercode), nil}
  75. res := &UserResponse{}
  76. err = conn.GetByUsercode(req, res)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return res, nil
  81. }
  82. func Login(dbname, account, password string, url ...string) (map[string]string, error) {
  83. var user_rpc_url string = "127.0.0.1:7976"
  84. if len(url) > 0 && url[0] != "" {
  85. user_rpc_url = url[0]
  86. }
  87. conn, _, err := DialUserService("tcp", user_rpc_url)
  88. if err != nil {
  89. return map[string]string{}, err
  90. }
  91. defer conn.Close()
  92. req := &LoginRequest{proto.String(dbname), proto.String(account), proto.String(password), nil}
  93. res := &LoginResponse{}
  94. err = conn.Login(req, res)
  95. if err != nil {
  96. return map[string]string{}, err
  97. }
  98. if res.GetUserId() != "" {
  99. return map[string]string{
  100. "UserId": res.GetUserId(),
  101. "Username": res.GetUsername(),
  102. "Nickname": res.GetNickname(),
  103. "Mobile": res.GetMobile(),
  104. "Email": res.GetEmail(),
  105. "Status": res.GetStatus(),
  106. "BusinessId": res.GetBusinessId(),
  107. "StoreId": res.GetStoreId(),
  108. "FansTo": res.GetFansTo(),
  109. "IsVip": res.GetIsVip(),
  110. "Token": res.GetToken(),
  111. "Usercode": res.GetUsercode(),
  112. "GroupId": res.GetGroupId(),
  113. "Type": res.GetType(),
  114. }, nil
  115. }
  116. return map[string]string{}, nil
  117. }