用户接口远程调用
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.

207 lines
4.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package userrpc
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "time"
  6. "git.tetele.net/tgo/crypter"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. func GetUserByToken(dbname, token string, url ...string) (map[string]string, error) {
  10. var user_rpc_url string = "127.0.0.1:7976"
  11. if len(url) > 0 && url[0] != "" {
  12. user_rpc_url = url[0]
  13. }
  14. conn, _, err := DialUserService("tcp", user_rpc_url)
  15. if err != nil {
  16. return map[string]string{}, err
  17. }
  18. defer conn.Close()
  19. req := &UserRequest{proto.String(dbname), proto.String(token), nil}
  20. res := &UserResponse{}
  21. err = conn.GetByToken(req, res)
  22. if err != nil {
  23. return map[string]string{}, err
  24. }
  25. if res.GetUserId() != "" {
  26. return map[string]string{
  27. "UserId": res.GetUserId(),
  28. "Username": res.GetUsername(),
  29. "Nickname": res.GetNickname(),
  30. "Mobile": res.GetMobile(),
  31. "Email": res.GetEmail(),
  32. "Status": res.GetStatus(),
  33. "BusinessId": res.GetBusinessId(),
  34. "StoreId": res.GetStoreId(),
  35. "FansTo": res.GetFansTo(),
  36. "IsVip": res.GetIsVip(),
  37. "Usercode": res.GetUsercode(),
  38. "GroupId": res.GetGroupId(),
  39. "Type": res.GetType(),
  40. "ThirdId": res.GetThirdId(),
  41. "Platform": res.GetPlatform(),
  42. }, nil
  43. }
  44. return map[string]string{}, nil
  45. }
  46. /**
  47. * 使用用户名查询
  48. */
  49. func GetUserByUsername(dbname, username string, url ...string) (*UserResponse, error) {
  50. var user_rpc_url string = "127.0.0.1:7976"
  51. if len(url) > 0 && url[0] != "" {
  52. user_rpc_url = url[0]
  53. }
  54. conn, _, err := DialUserService("tcp", user_rpc_url)
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer conn.Close()
  59. req := &UserInfoByUsername{proto.String(dbname), proto.String(username), nil}
  60. res := &UserResponse{}
  61. err = conn.GetByUsername(req, res)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return res, nil
  66. }
  67. /**
  68. * 使用编号查询
  69. */
  70. func GetUserByUsercode(dbname, usercode string, url ...string) (*UserResponse, error) {
  71. var user_rpc_url string = "127.0.0.1:7976"
  72. if len(url) > 0 && url[0] != "" {
  73. user_rpc_url = url[0]
  74. }
  75. conn, _, err := DialUserService("tcp", user_rpc_url)
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer conn.Close()
  80. req := &UserInfoByUsercode{proto.String(dbname), proto.String(usercode), nil}
  81. res := &UserResponse{}
  82. err = conn.GetByUsercode(req, res)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return res, nil
  87. }
  88. func Login(dbname, account, password string, url ...string) (map[string]string, error) {
  89. var user_rpc_url string = "127.0.0.1:7976"
  90. if len(url) > 0 && url[0] != "" {
  91. user_rpc_url = url[0]
  92. }
  93. conn, _, err := DialUserService("tcp", user_rpc_url)
  94. if err != nil {
  95. return map[string]string{}, err
  96. }
  97. defer conn.Close()
  98. req := &LoginRequest{proto.String(dbname), proto.String(account), proto.String(password), nil}
  99. res := &LoginResponse{}
  100. err = conn.Login(req, res)
  101. if err != nil {
  102. return map[string]string{}, err
  103. }
  104. if res.GetUserId() != "" {
  105. return map[string]string{
  106. "UserId": res.GetUserId(),
  107. "Username": res.GetUsername(),
  108. "Nickname": res.GetNickname(),
  109. "Mobile": res.GetMobile(),
  110. "Email": res.GetEmail(),
  111. "Status": res.GetStatus(),
  112. "BusinessId": res.GetBusinessId(),
  113. "StoreId": res.GetStoreId(),
  114. "FansTo": res.GetFansTo(),
  115. "IsVip": res.GetIsVip(),
  116. "Token": res.GetToken(),
  117. "Usercode": res.GetUsercode(),
  118. "GroupId": res.GetGroupId(),
  119. "Type": res.GetType(),
  120. }, nil
  121. }
  122. return map[string]string{}, nil
  123. }
  124. /**
  125. * 查找商户信息
  126. * 2021/04/06
  127. * gz
  128. */
  129. func GetById(site_id, dbname, user_id string, url ...string) (map[string]string, error) {
  130. conn, err := rpc_server_conn(url...)
  131. if err != nil {
  132. return nil, err
  133. }
  134. defer conn.Close()
  135. data := GetUserReq{}
  136. data.SiteId = site_id
  137. data.Dbname = dbname
  138. data.UserId = user_id
  139. data_json, err := json.Marshal(data)
  140. if err != nil {
  141. return nil, err
  142. }
  143. encryData := crypter.DesEn(string(data_json), DES_KEY)
  144. now_int64 := time.Now().Unix()
  145. now := strconv.FormatInt(now_int64, 10)
  146. sign := Sign(encryData, now)
  147. req := &Request{
  148. proto.String(encryData),
  149. proto.String(now),
  150. proto.String(sign),
  151. nil}
  152. res := &Response{}
  153. err = conn.GetById(req, res)
  154. if err != nil {
  155. return nil, err
  156. }
  157. return HandleUserRes(res)
  158. }