From dab4e3e30c48e4c6146dc6b9d62de09ec8ae9a5c Mon Sep 17 00:00:00 2001 From: xiabin Date: Wed, 15 Jan 2025 13:37:52 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=AF=B9=E6=8E=A5code2Session=E6=96=B0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- douyin_openapi.go | 45 +++++++++++++++++++++++++++++++-------- util/conv.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 util/conv.go diff --git a/douyin_openapi.go b/douyin_openapi.go index 40203cf..efe9f60 100644 --- a/douyin_openapi.go +++ b/douyin_openapi.go @@ -13,8 +13,8 @@ import ( ) const ( - code2Session = "/api/apps/v2/jscode2session" // 小程序登录地址 - getEcpm = "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 获取ECPM + code2Session = "https://minigame.zijieapi.com/mgplatform/api/apps/jscode2session" // 小程序登录地址 + getEcpm = "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 获取ECPM ) // DouYinOpenApiConfig 实例化配置 @@ -42,13 +42,8 @@ func NewDouYinOpenApi(config DouYinOpenApiConfig) *DouYinOpenApi { if config.AccessToken == nil { config.AccessToken = accessToken.NewDefaultAccessToken(config.AppId, config.AppSecret, config.Cache, config.IsSandbox) } - BaseApi := "https://developer.toutiao.com" - if config.IsSandbox { - BaseApi = "https://open-sandbox.douyin.com" - } return &DouYinOpenApi{ - Config: config, - BaseApi: BaseApi, + Config: config, } } @@ -57,6 +52,29 @@ func (d *DouYinOpenApi) GetApiUrl(url string) string { return fmt.Sprintf("%s%s", d.BaseApi, url) } +// Get 获取数据 +func (d *DouYinOpenApi) Get(url string, params any, data any) (err error) { + paramsStr, err := util.StructToQueryParams(params) + if err != nil { + return + } + fullURL := fmt.Sprintf("%s?%s", url, paramsStr) + res, err := http.Get(fullURL) + if err != nil { + return + } + defer res.Body.Close() // 关闭连接 + body, err := io.ReadAll(res.Body) + if err != nil { + return + } + err = json.Unmarshal(body, data) + if err != nil { + return + } + return +} + // PostJson 封装公共的请求方法 func (d *DouYinOpenApi) PostJson(api string, params any, response any) (err error) { body, err := util.PostJSON(api, params) @@ -83,6 +101,10 @@ type Code2SessionResponse struct { ErrNo int `json:"err_no,omitempty"` ErrTips string `json:"err_tips,omitempty"` Data Code2SessionResponseData `json:"data,omitempty"` + ErrCode int `json:"errcode,omitempty"` + ErrMsg string `json:"errmsg,omitempty"` + Error int `json:"error,omitempty"` + Message string `json:"message,omitempty"` } type Code2SessionResponseData struct { @@ -100,7 +122,7 @@ func (d *DouYinOpenApi) Code2Session(code, anonymousCode string) (code2SessionRe AnonymousCode: anonymousCode, Code: code, } - err = d.PostJson(d.GetApiUrl(code2Session), params, &code2SessionResponse) + err = d.Get(code2Session, params, &code2SessionResponse) if err != nil { return } @@ -194,3 +216,8 @@ func (d *DouYinOpenApi) GetEcpm(params GetEcpmParams) (list []Record, err error) params.PageNo++ } } + +// GetAccessToken 获取accessToken +func (d *DouYinOpenApi) GetAccessToken() (string, error) { + return d.Config.AccessToken.GetAccessToken() +} diff --git a/util/conv.go b/util/conv.go new file mode 100644 index 0000000..2ee7ed6 --- /dev/null +++ b/util/conv.go @@ -0,0 +1,54 @@ +package util + +import ( + "errors" + "fmt" + "net/url" + "reflect" + "strings" +) + +// StructToQueryParams 将struct转换为queryString +func StructToQueryParams(s interface{}) (string, error) { + params := url.Values{} + val := reflect.ValueOf(s) + + // 确保传入的是一个结构体指针 + if val.Kind() == reflect.Ptr { + val = val.Elem() + } + + // 确保传入的是一个结构体 + if val.Kind() != reflect.Struct { + return "", fmt.Errorf("input must be a struct or struct pointer") + } + typ := val.Type() + for i := 0; i < val.NumField(); i++ { + field := val.Field(i) + fieldName := typ.Field(i).Name + if tag := typ.Field(i).Tag.Get("json"); fieldName != "" { + arr := strings.Split(tag, ",") + if len(arr) > 1 { + fieldName = arr[0] + if arr[1] == "omitempty" && field.IsZero() { + continue + } + } + } + // 对于基本类型,添加到 url.Values 中 + switch field.Kind() { + case reflect.String: + params.Add(fieldName, field.String()) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + params.Add(fieldName, fmt.Sprint(field.Int())) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + params.Add(fieldName, fmt.Sprint(field.Uint())) + case reflect.Float32, reflect.Float64: + params.Add(fieldName, fmt.Sprint(field.Float())) + // 可以根据需要添加更多的类型支持 + default: + return "", errors.New("unsupport type") + } + } + return params.Encode(), nil +}