update:对接code2Session新接口

This commit is contained in:
xiabin 2025-01-15 13:37:52 +08:00
parent 75d43bc3b7
commit dab4e3e30c
2 changed files with 90 additions and 9 deletions

View File

@ -13,8 +13,8 @@ import (
) )
const ( const (
code2Session = "/api/apps/v2/jscode2session" // 小程序登录地址 code2Session = "https://minigame.zijieapi.com/mgplatform/api/apps/jscode2session" // 小程序登录地址
getEcpm = "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 获取ECPM getEcpm = "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 获取ECPM
) )
// DouYinOpenApiConfig 实例化配置 // DouYinOpenApiConfig 实例化配置
@ -42,13 +42,8 @@ func NewDouYinOpenApi(config DouYinOpenApiConfig) *DouYinOpenApi {
if config.AccessToken == nil { if config.AccessToken == nil {
config.AccessToken = accessToken.NewDefaultAccessToken(config.AppId, config.AppSecret, config.Cache, config.IsSandbox) 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{ return &DouYinOpenApi{
Config: config, Config: config,
BaseApi: BaseApi,
} }
} }
@ -57,6 +52,29 @@ func (d *DouYinOpenApi) GetApiUrl(url string) string {
return fmt.Sprintf("%s%s", d.BaseApi, url) 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 封装公共的请求方法 // PostJson 封装公共的请求方法
func (d *DouYinOpenApi) PostJson(api string, params any, response any) (err error) { func (d *DouYinOpenApi) PostJson(api string, params any, response any) (err error) {
body, err := util.PostJSON(api, params) body, err := util.PostJSON(api, params)
@ -83,6 +101,10 @@ type Code2SessionResponse struct {
ErrNo int `json:"err_no,omitempty"` ErrNo int `json:"err_no,omitempty"`
ErrTips string `json:"err_tips,omitempty"` ErrTips string `json:"err_tips,omitempty"`
Data Code2SessionResponseData `json:"data,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 { type Code2SessionResponseData struct {
@ -100,7 +122,7 @@ func (d *DouYinOpenApi) Code2Session(code, anonymousCode string) (code2SessionRe
AnonymousCode: anonymousCode, AnonymousCode: anonymousCode,
Code: code, Code: code,
} }
err = d.PostJson(d.GetApiUrl(code2Session), params, &code2SessionResponse) err = d.Get(code2Session, params, &code2SessionResponse)
if err != nil { if err != nil {
return return
} }
@ -194,3 +216,8 @@ func (d *DouYinOpenApi) GetEcpm(params GetEcpmParams) (list []Record, err error)
params.PageNo++ params.PageNo++
} }
} }
// GetAccessToken 获取accessToken
func (d *DouYinOpenApi) GetAccessToken() (string, error) {
return d.Config.AccessToken.GetAccessToken()
}

54
util/conv.go Normal file
View File

@ -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
}