| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package handlers
- import (
- "Vali-Tools/utils"
- "context"
- "fmt"
- )
- type HandlerRequests struct {
- token string
- httpClient *utils.HTTPClient // 添加HTTP客户端
- ctx context.Context
- host string
- }
- func NewHandlerRequests(context context.Context, token string, host string) *HandlerRequests {
- return &HandlerRequests{
- token: token,
- ctx: context,
- host: host,
- httpClient: utils.NewHTTPClient(host, token),
- }
- }
- // MakeGetRequest 网络请求示例方法
- func (c *HandlerRequests) MakeGetRequest(endpoint string) (interface{}, error) {
- if c.httpClient == nil {
- return nil, fmt.Errorf("HTTP客户端未初始化")
- }
- var result interface{}
- err := c.httpClient.GetJSON(c.ctx, endpoint, &result, nil)
- if err != nil {
- return nil, fmt.Errorf("GET请求失败: %w", err)
- }
- if result != nil {
- // 类型断言检查result是否为map[string]interface{}
- if resultMap, ok := result.(map[string]interface{}); ok {
- var codeValue int
- if code, ok := resultMap["code"].(float64); ok {
- codeValue = int(code)
- } else if code, ok := resultMap["code"].(int); ok {
- codeValue = code
- }
- if codeValue != 0 {
- // 处理错误码不为0的情况
- if msg, msgExists := resultMap["message"]; msgExists {
- return nil, fmt.Errorf("API返回错误: code=%v, message=%s", codeValue, msg)
- }
- return nil, fmt.Errorf("API返回错误: code=%v", codeValue)
- }
- }
- }
- return result, nil
- }
- func (c *HandlerRequests) MakePostRequest(endpoint string, data interface{}) (interface{}, error) {
- if c.httpClient == nil {
- return nil, fmt.Errorf("HTTP客户端未初始化")
- }
- // 添加调试信息
- fmt.Printf("发送POST请求到: %s, 数据: %+v\n", endpoint, data)
- var result interface{}
- err := c.httpClient.PostJSON(c.ctx, endpoint, data, &result, nil)
- if err != nil {
- return nil, fmt.Errorf("POST请求失败: %w", err)
- }
- if result != nil {
- // 类型断言检查result是否为map[string]interface{}
- if resultMap, ok := result.(map[string]interface{}); ok {
- var codeValue int
- if code, ok := resultMap["code"].(float64); ok {
- codeValue = int(code)
- } else if code, ok := resultMap["code"].(int); ok {
- codeValue = code
- }
- if codeValue != 0 {
- // 处理错误码不为0的情况
- if msg, msgExists := resultMap["message"]; msgExists {
- return nil, fmt.Errorf("API返回错误: code=%v, message=%s", codeValue, msg)
- }
- return nil, fmt.Errorf("API返回错误: code=%v", codeValue)
- }
- }
- }
- return result, nil
- }
- // MakeFileRequest 文件上传
- func (c *HandlerRequests) MakeFileRequest(endpoint string, filePath string, fieldName string) (interface{}, error) {
- if c.httpClient == nil {
- return nil, fmt.Errorf("HTTP客户端未初始化")
- }
- var result interface{}
- err := c.httpClient.UploadFile(c.ctx, endpoint, filePath, fieldName, nil, &result)
- if err != nil {
- return nil, fmt.Errorf("文件上传失败: %w", err)
- }
- if result != nil {
- // 类型断言检查result是否为map[string]interface{}
- if resultMap, ok := result.(map[string]interface{}); ok {
- var codeValue int
- if code, ok := resultMap["code"].(float64); ok {
- codeValue = int(code)
- } else if code, ok := resultMap["code"].(int); ok {
- codeValue = code
- }
- if codeValue != 0 {
- // 处理错误码不为0的情况
- if msg, msgExists := resultMap["message"]; msgExists {
- return nil, fmt.Errorf("API返回错误: code=%v, message=%s", codeValue, msg)
- }
- return nil, fmt.Errorf("API返回错误: code=%v", codeValue)
- }
- }
- }
- return result, nil
- }
- // GetUserInfo 获取用户信息
- func (c *HandlerRequests) GetUserInfo() (interface{}, error) {
- println("userInfoApi:", utils.GetUserInfo)
- userInfo, err := c.MakeGetRequest(utils.GetUserInfo)
- if err != nil {
- return nil, err
- }
- return userInfo, nil
- }
|