get_apps_list_logic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package fastgpt
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "io/ioutil"
  9. "net/http"
  10. "strconv"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. )
  14. type GetAppsListLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetAppsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAppsListLogic {
  20. return &GetAppsListLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetAppsListLogic) GetAppsList(req *types.AppsListReq) (resp *types.AppsListResp, err error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. organizationIdStr := strconv.FormatUint(organizationId, 10)
  28. token, err := GetToken(organizationIdStr)
  29. if err != nil {
  30. return nil, fmt.Errorf("invalid token")
  31. }
  32. keyword := ""
  33. if req.Keyword != nil {
  34. keyword = *req.Keyword
  35. }
  36. response := CoreAppList(req.Type, keyword, token)
  37. return &types.AppsListResp{Data: response}, nil
  38. }
  39. // 定义一个结构体用于表示整个 JSON 响应
  40. type Response struct {
  41. Status string `json:"status"`
  42. Data []*types.AppsListRespInfo `json:"data"`
  43. }
  44. type AppListRequest struct {
  45. Type []string `json:"type"`
  46. SearchKey string `json:"searchKey"`
  47. }
  48. func CoreAppList(apps_type string, searchKey string, token string) []*types.AppsListRespInfo {
  49. url := "https://fastgpt.gkscrm.com/api/core/app/list"
  50. requestBodyData := AppListRequest{
  51. Type: []string{"folder", apps_type},
  52. SearchKey: searchKey,
  53. }
  54. // 将结构体转换为 JSON 字节切片
  55. requestBody, err := json.Marshal(requestBodyData)
  56. if err != nil {
  57. fmt.Printf("Error marshalling request body: %v\n", err)
  58. return nil
  59. }
  60. // 创建一个新的请求
  61. req1, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  62. if err != nil {
  63. fmt.Printf("Error creating request: %v\n", err)
  64. return nil
  65. }
  66. // 设置请求头
  67. req1.Header.Set("Content-Type", "application/json")
  68. req1.Header.Set("Cookie", fmt.Sprintf("fastgpt_token=%s", token))
  69. // 使用 http.Client 发送请求
  70. client := &http.Client{}
  71. resp1, err := client.Do(req1)
  72. if err != nil {
  73. fmt.Printf("Error sending request: %v\n", err)
  74. return nil
  75. }
  76. defer resp1.Body.Close()
  77. // 读取响应
  78. body, err := ioutil.ReadAll(resp1.Body)
  79. if err != nil {
  80. fmt.Printf("Error reading response: %v\n", err)
  81. return nil
  82. }
  83. var response Response
  84. err = json.Unmarshal(body, &response)
  85. if err != nil {
  86. fmt.Printf("Error unmarshalling JSON: %v\n", err)
  87. return nil
  88. }
  89. return response.Data
  90. }