package utils import ( "errors" "go-account-register/config" "log" "github.com/go-resty/resty/v2" ) type AdsPowerRequest struct { client *resty.Client } func NewAdsPowerRequest() *AdsPowerRequest { appConfig, _ := config.LoadConfig() client := resty.New() client.SetBaseURL(appConfig.AdsPower.ApiUrl) // 响应拦截器 (类似 axios 拦截器) client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error { if resp.StatusCode() != 200 { log.Println("请求失败,检查网络") return errors.New("request failed") } return nil }) return &AdsPowerRequest{client: client} } // Request 发送通用请求 func (h *AdsPowerRequest) Request(method, url string, body interface{}, queryParams map[string]string) (*resty.Response, error) { req := h.client.R(). SetHeader("Content-Type", "application/json") if body != nil { req.SetBody(body) } if queryParams != nil { req.SetQueryParams(queryParams) } resp, err := req.Execute(method, url) if err != nil { log.Println("请求失败了:", err) return nil, err } return resp, nil } /** * 打开浏览器 */ func (h *AdsPowerRequest) OpenBrowser(data map[string]interface{}) (*resty.Response, error) { return h.Request("POST", "/api/v2/browser-profile/start", data, nil) } /** * @description 关闭浏览器 * @param {String} id * @returns {*resty.Response, error} */ func (h *AdsPowerRequest) CloseBrowser(id string) (*resty.Response, error) { return h.Request("POST", "/api/v2/browser-profile/stop", map[string]interface{}{"profile_id": id}, nil) } /** * @description 创建浏览器 * @param {Object} data * @returns {*resty.Response, error} */ func (h *AdsPowerRequest) CreateBrowser(data map[string]interface{}) (*resty.Response, error) { return h.Request("POST", "/api/v2/browser-profile/create", data, nil) } /** * @description 查询浏览器运行状态 * @param {String} id * @returns {*resty.Response, error} */ func (h *AdsPowerRequest) GetRunStatus(id string) (*resty.Response, error) { return h.Request("GET", "/api/v2/browser-profile/active", nil, map[string]string{"profile_id": id}) } /** * @description 删除浏览器 * @param {String} id * @returns {*resty.Response, error} */ func (h *AdsPowerRequest) DeleteBrowser(id string) (*resty.Response, error) { return h.Request("POST", "/api/v2/browser-profile/delete", map[string]interface{}{"profile_id": []string{id}}, nil) } /** * @description 更新浏览器配置 * @param {Object} data * @returns {*resty.Response, error} */ func (h *AdsPowerRequest) UpdateBrowser(data map[string]interface{}) (*resty.Response, error) { return h.Request("POST", "/api/v2/browser-profile/update", data, nil) }