handler_requests.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package handlers
  2. import (
  3. "Vali-Tools/utils"
  4. "context"
  5. "fmt"
  6. )
  7. type HandlerRequests struct {
  8. token string
  9. httpClient *utils.HTTPClient // 添加HTTP客户端
  10. ctx context.Context
  11. host string
  12. }
  13. func NewHandlerRequests(context context.Context, token string, host string) *HandlerRequests {
  14. return &HandlerRequests{
  15. token: token,
  16. ctx: context,
  17. host: host,
  18. httpClient: utils.NewHTTPClient(host, token),
  19. }
  20. }
  21. // MakeGetRequest 网络请求示例方法
  22. func (c *HandlerRequests) MakeGetRequest(endpoint string) (interface{}, error) {
  23. if c.httpClient == nil {
  24. return nil, fmt.Errorf("HTTP客户端未初始化")
  25. }
  26. var result interface{}
  27. err := c.httpClient.GetJSON(c.ctx, endpoint, &result, nil)
  28. if err != nil {
  29. return nil, fmt.Errorf("GET请求失败: %w", err)
  30. }
  31. if result != nil {
  32. // 类型断言检查result是否为map[string]interface{}
  33. if resultMap, ok := result.(map[string]interface{}); ok {
  34. var codeValue int
  35. if code, ok := resultMap["code"].(float64); ok {
  36. codeValue = int(code)
  37. } else if code, ok := resultMap["code"].(int); ok {
  38. codeValue = code
  39. }
  40. if codeValue != 0 {
  41. // 处理错误码不为0的情况
  42. if msg, msgExists := resultMap["message"]; msgExists {
  43. return nil, fmt.Errorf("API返回错误: code=%v, message=%s", codeValue, msg)
  44. }
  45. return nil, fmt.Errorf("API返回错误: code=%v", codeValue)
  46. }
  47. }
  48. }
  49. return result, nil
  50. }
  51. func (c *HandlerRequests) MakePostRequest(endpoint string, data interface{}) (interface{}, error) {
  52. if c.httpClient == nil {
  53. return nil, fmt.Errorf("HTTP客户端未初始化")
  54. }
  55. // 添加调试信息
  56. fmt.Printf("发送POST请求到: %s, 数据: %+v\n", endpoint, data)
  57. var result interface{}
  58. err := c.httpClient.PostJSON(c.ctx, endpoint, data, &result, nil)
  59. if err != nil {
  60. return nil, fmt.Errorf("POST请求失败: %w", err)
  61. }
  62. if result != nil {
  63. // 类型断言检查result是否为map[string]interface{}
  64. if resultMap, ok := result.(map[string]interface{}); ok {
  65. var codeValue int
  66. if code, ok := resultMap["code"].(float64); ok {
  67. codeValue = int(code)
  68. } else if code, ok := resultMap["code"].(int); ok {
  69. codeValue = code
  70. }
  71. if codeValue != 0 {
  72. // 处理错误码不为0的情况
  73. if msg, msgExists := resultMap["message"]; msgExists {
  74. return nil, fmt.Errorf("API返回错误: code=%v, message=%s", codeValue, msg)
  75. }
  76. return nil, fmt.Errorf("API返回错误: code=%v", codeValue)
  77. }
  78. }
  79. }
  80. return result, nil
  81. }
  82. // MakeFileRequest 文件上传
  83. func (c *HandlerRequests) MakeFileRequest(endpoint string, filePath string, fieldName string) (interface{}, error) {
  84. if c.httpClient == nil {
  85. return nil, fmt.Errorf("HTTP客户端未初始化")
  86. }
  87. var result interface{}
  88. err := c.httpClient.UploadFile(c.ctx, endpoint, filePath, fieldName, nil, &result)
  89. if err != nil {
  90. return nil, fmt.Errorf("文件上传失败: %w", err)
  91. }
  92. if result != nil {
  93. // 类型断言检查result是否为map[string]interface{}
  94. if resultMap, ok := result.(map[string]interface{}); ok {
  95. var codeValue int
  96. if code, ok := resultMap["code"].(float64); ok {
  97. codeValue = int(code)
  98. } else if code, ok := resultMap["code"].(int); ok {
  99. codeValue = code
  100. }
  101. if codeValue != 0 {
  102. // 处理错误码不为0的情况
  103. if msg, msgExists := resultMap["message"]; msgExists {
  104. return nil, fmt.Errorf("API返回错误: code=%v, message=%s", codeValue, msg)
  105. }
  106. return nil, fmt.Errorf("API返回错误: code=%v", codeValue)
  107. }
  108. }
  109. }
  110. return result, nil
  111. }
  112. // GetUserInfo 获取用户信息
  113. func (c *HandlerRequests) GetUserInfo() (interface{}, error) {
  114. println("userInfoApi:", utils.GetUserInfo)
  115. userInfo, err := c.MakeGetRequest(utils.GetUserInfo)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return userInfo, nil
  120. }