update_app_logic.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package fastgpt
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "net/http"
  9. "strconv"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. )
  13. type UpdateAppLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewUpdateAppLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAppLogic {
  19. return &UpdateAppLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *UpdateAppLogic) UpdateApp(req *types.UpdateAppsReq) (resp *types.BaseMsgResp, err error) {
  25. intro := ""
  26. if req.Intro != nil {
  27. intro = *req.Intro
  28. }
  29. organizationId := l.ctx.Value("organizationId").(uint64)
  30. organizationIdStr := strconv.FormatUint(organizationId, 10)
  31. token, err := GetToken(organizationIdStr)
  32. if err != nil {
  33. return nil, fmt.Errorf("invalid token")
  34. }
  35. AppUpdate(req.Id, req.Name, intro, token)
  36. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  37. }
  38. func AppUpdate(id string, name string, intro string, token string) {
  39. url := fmt.Sprintf("https://fastgpt.gkscrm.com/api/core/app/update?appId=%s", id)
  40. jsonStr := fmt.Sprintf(`{"name":"%s","intro":"%s"}`, name, intro)
  41. requestBody := []byte(jsonStr)
  42. // 创建一个新的请求
  43. req1, err := http.NewRequest("PUT", url, bytes.NewBuffer(requestBody))
  44. if err != nil {
  45. fmt.Printf("Error creating request: %v\n", err)
  46. return
  47. }
  48. // 设置请求头
  49. req1.Header.Set("Content-Type", "application/json")
  50. req1.Header.Set("Cookie", fmt.Sprintf("fastgpt_token=%s", token))
  51. // 使用 http.Client 发送请求
  52. client := &http.Client{}
  53. resp1, err := client.Do(req1)
  54. if err != nil {
  55. fmt.Printf("Error sending request: %v\n", err)
  56. return
  57. }
  58. defer resp1.Body.Close()
  59. return
  60. }