update:对接code2Session新接口

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

View File

@ -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,25 @@ 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 []byte, 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() // 关闭连接
data, err = io.ReadAll(res.Body)
if err != nil {
return
}
return
}
// PostJson 封装公共的请求方法
func (d *DouYinOpenApi) PostJson(api string, params any, response any) (err error) {
body, err := util.PostJSON(api, params)
@ -78,11 +92,15 @@ type Code2SessionParams struct {
Code string `json:"code,omitempty"`
}
// Code2SessionResponse 小程序登录返回值
type Code2SessionResponse struct {
ErrNo int `json:"err_no,omitempty"`
ErrTips string `json:"err_tips,omitempty"`
Data Code2SessionResponseData `json:"data,omitempty"`
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Message string `json:"message,omitempty"`
AnonymousOpenid string `json:"anonymous_openid"`
Error int `json:"error"`
Openid string `json:"openid"`
SessionKey string `json:"session_key"`
Unionid string `json:"unionid"`
}
type Code2SessionResponseData struct {
@ -100,14 +118,15 @@ func (d *DouYinOpenApi) Code2Session(code, anonymousCode string) (code2SessionRe
AnonymousCode: anonymousCode,
Code: code,
}
err = d.PostJson(d.GetApiUrl(code2Session), params, &code2SessionResponse)
b, err := d.Get(code2Session, params)
if err != nil {
return
}
if code2SessionResponse.ErrNo != 0 {
return code2SessionResponse, fmt.Errorf("小程序登录错误: %s %d", code2SessionResponse.ErrTips, code2SessionResponse.ErrNo)
err = json.Unmarshal(b, &code2SessionResponse)
if err != nil {
return
}
return
return code2SessionResponse, nil
}
type Record struct {
@ -194,3 +213,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()
}

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
}