first commit
This commit is contained in:
commit
1490b262b8
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
147
douyin.go
Normal file
147
douyin.go
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
package douyin_openapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
douyinopenapi "gitea.youtukeji.com.cn/youtu/openapi-helper/douyin"
|
||||||
|
"gitea.youtukeji.com.cn/youtu/openapi-helper/douyin/access-token"
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DouYinOpenApiClient struct {
|
||||||
|
m *sync.Map
|
||||||
|
}
|
||||||
|
|
||||||
|
type DouYinApi struct {
|
||||||
|
Api *douyinopenapi.DouYinOpenApi // 抖音openapi客户端
|
||||||
|
ecpmValue uint32 // ECPM值
|
||||||
|
viewCount uint32 //浏览数
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetEcpmParams struct {
|
||||||
|
AppId string `json:"app_id" form:"app_id"`
|
||||||
|
OpenId string `json:"open_id" form:"open_id"`
|
||||||
|
AccessToken string `json:"access_token" form:"access_token"`
|
||||||
|
DateHour string `json:"date_hour" form:"date_hour"`
|
||||||
|
PageSize int `json:"page_size" form:"page_size"`
|
||||||
|
PageNo int `json:"page_no" form:"page_no"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDouYinOpenApiClient() *DouYinOpenApiClient {
|
||||||
|
//DouYinOpenApi = douyinopenapi.NewDouYinOpenApi(douyinopenapi.DouYinOpenApiConfig{
|
||||||
|
// AppId: "1259819191",
|
||||||
|
// AppSecret: "0b7d7c0d0c8c0a0b0a0c0d0e0f0f0e0d0c0b0a090807060504030201000",
|
||||||
|
// IsSandbox: true,
|
||||||
|
// AccessToken: nil,
|
||||||
|
// Cache: nil,
|
||||||
|
//})
|
||||||
|
return &DouYinOpenApiClient{
|
||||||
|
m: &sync.Map{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDouYinOpenApi 获取抖音client
|
||||||
|
// appId: 小程序id
|
||||||
|
func (d *DouYinOpenApiClient) GetDouYinOpenApi(appId string) (api *DouYinApi, err error) {
|
||||||
|
if v, ok := d.m.Load(appId); !ok {
|
||||||
|
err = errors.New("未找到抖音client")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
api = v.(*DouYinApi)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDouYinOpenApi 存储抖音client
|
||||||
|
// appId: 小程序id
|
||||||
|
func (d *DouYinOpenApiClient) SetDouYinOpenApi(appId string, api *DouYinApi) {
|
||||||
|
d.m.Store(appId, api)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAndStoreDouYinOpenApi 创建抖音client并存储
|
||||||
|
// appId: 小程序id
|
||||||
|
// appSecret: 小程序secret
|
||||||
|
// cache: 缓存
|
||||||
|
func (d *DouYinOpenApiClient) NewAndStoreDouYinOpenApi(appId, appSecret string, ecpmValue uint32, viewCount uint32, cache cache.Cache) {
|
||||||
|
token := access_token.NewDefaultAccessToken(appId, appSecret, cache, false)
|
||||||
|
api := douyinopenapi.NewDouYinOpenApi(douyinopenapi.DouYinOpenApiConfig{
|
||||||
|
AppId: appId,
|
||||||
|
AppSecret: appSecret,
|
||||||
|
AccessToken: token,
|
||||||
|
Cache: cache,
|
||||||
|
IsSandbox: false,
|
||||||
|
})
|
||||||
|
d.SetDouYinOpenApi(appId, &DouYinApi{
|
||||||
|
Api: api,
|
||||||
|
ecpmValue: ecpmValue,
|
||||||
|
viewCount: viewCount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpmValue 获取ECPM值
|
||||||
|
// appId: 小程序id,map的key
|
||||||
|
func (d *DouYinOpenApiClient) GetEcpmValue(appId string) uint32 {
|
||||||
|
douyin, err := d.GetDouYinOpenApi(appId)
|
||||||
|
if err != nil || douyin == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return douyin.ecpmValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpmViewCount 获取ECPM浏览数
|
||||||
|
// appId: 小程序id,map的key
|
||||||
|
func (d *DouYinOpenApiClient) GetEcpmViewCount(appId string) uint32 {
|
||||||
|
douyin, err := d.GetDouYinOpenApi(appId)
|
||||||
|
if err != nil || douyin == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return douyin.viewCount
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpmData 获取ECPM数据
|
||||||
|
// appId: 小程序id
|
||||||
|
// openId: 抖音openId
|
||||||
|
// dateHour: 日期
|
||||||
|
func (d *DouYinOpenApiClient) GetEcpmData(appId, openId, dateHour string) (list []douyinopenapi.Record, err error) {
|
||||||
|
douyin, err := d.GetDouYinOpenApi(appId)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取accessToken
|
||||||
|
accessToken, err := douyin.Api.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
list, err = douyin.Api.GetEcpm(douyinopenapi.GetEcpmParams{
|
||||||
|
AppId: appId,
|
||||||
|
OpenId: openId,
|
||||||
|
AccessToken: accessToken,
|
||||||
|
DateHour: dateHour,
|
||||||
|
PageSize: 500,
|
||||||
|
PageNo: 1,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpm 计算ECPM
|
||||||
|
// https://bytedance.larkoffice.com/docx/Vg4yd0RDSovZINxJDyIc6THhnod
|
||||||
|
func (d *DouYinOpenApiClient) GetEcpm(res []douyinopenapi.Record) (ecpm float64, err error) {
|
||||||
|
// 计算 ECPM
|
||||||
|
totalCost := 0
|
||||||
|
totalRecords := len(res)
|
||||||
|
|
||||||
|
for _, record := range res {
|
||||||
|
totalCost += record.Cost
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有记录,则返回错误
|
||||||
|
if totalRecords == 0 {
|
||||||
|
err = errors.New("未找到记录,无法计算 ECPM")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 总 cost / 100000 * 1000 / 总记录数
|
||||||
|
ecpm = float64(totalCost) / 100000 * 1000 / float64(totalRecords)
|
||||||
|
return
|
||||||
|
}
|
125
douyin/access-token/access_token.go
Normal file
125
douyin/access-token/access_token.go
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
package access_token
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"gitea.youtukeji.com.cn/youtu/openapi-helper/douyin/util"
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 正式地址
|
||||||
|
const accessTokenURL = "https://developer.toutiao.com/api/apps/v2/token"
|
||||||
|
|
||||||
|
// AccessToken 管理AccessToken 的基础接口
|
||||||
|
type AccessToken interface {
|
||||||
|
GetCacheKey() string // 获取缓存的key
|
||||||
|
SetCacheKey(key string) // 设置缓存key
|
||||||
|
GetAccessToken() (string, error) // 获取token
|
||||||
|
}
|
||||||
|
|
||||||
|
// accessTokenLock 初始化全局锁防止并发获取token
|
||||||
|
var accessTokenLock = new(sync.Mutex)
|
||||||
|
|
||||||
|
// DefaultAccessToken 默认的token管理类
|
||||||
|
type DefaultAccessToken struct {
|
||||||
|
AppId string // app_id string 是 小程序的 app_id
|
||||||
|
AppSecret string // app_secret string 是 小程序的密钥
|
||||||
|
GrantType string // grant_type string 是 固定值“client_credentials”
|
||||||
|
Cache cache.Cache // 缓存组件
|
||||||
|
accessTokenLock *sync.Mutex // 读写锁
|
||||||
|
accessTokenCacheKey string // 缓存的key
|
||||||
|
SandBox bool // 是否沙盒地址 默认 false 线上地址
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDefaultAccessToken 实例化默认的token管理类
|
||||||
|
func NewDefaultAccessToken(appId, appSecret string, cache cache.Cache, IsSandbox bool) *DefaultAccessToken {
|
||||||
|
if cache == nil {
|
||||||
|
panic(any("cache is need"))
|
||||||
|
}
|
||||||
|
token := &DefaultAccessToken{
|
||||||
|
AppId: appId,
|
||||||
|
AppSecret: appSecret,
|
||||||
|
GrantType: "client_credential",
|
||||||
|
Cache: cache,
|
||||||
|
accessTokenCacheKey: fmt.Sprintf("douyin_openapi_access_token_%s", appId),
|
||||||
|
accessTokenLock: accessTokenLock,
|
||||||
|
SandBox: IsSandbox,
|
||||||
|
}
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCacheKey 获取缓存key
|
||||||
|
func (dd *DefaultAccessToken) GetCacheKey() string {
|
||||||
|
return dd.accessTokenCacheKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCacheKey 设置缓存key
|
||||||
|
func (dd *DefaultAccessToken) SetCacheKey(key string) {
|
||||||
|
dd.accessTokenCacheKey = key
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAccessToken 获取token
|
||||||
|
func (dd *DefaultAccessToken) GetAccessToken() (string, error) {
|
||||||
|
// 先尝试从缓存中获取如果不存在就调用接口获取
|
||||||
|
if val := dd.Cache.Get(dd.GetCacheKey()); val != nil {
|
||||||
|
return val.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加锁防止并发获取接口
|
||||||
|
dd.accessTokenLock.Lock()
|
||||||
|
defer dd.accessTokenLock.Unlock()
|
||||||
|
|
||||||
|
// 双捡防止重复获取
|
||||||
|
if val := dd.Cache.Get(dd.GetCacheKey()); val != nil {
|
||||||
|
return val.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始调用接口获取token
|
||||||
|
reqAccessToken, err := GetTokenFromServer(accessTokenURL, dd.AppId, dd.AppSecret)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// 设置缓存
|
||||||
|
expires := reqAccessToken.Data.ExpiresIn - 1500
|
||||||
|
err = dd.Cache.Set(dd.GetCacheKey(), reqAccessToken.Data.AccessToken, time.Duration(expires)*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return reqAccessToken.Data.AccessToken, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResAccessToken 获取token的返回结构体
|
||||||
|
type ResAccessToken struct {
|
||||||
|
ErrNo int `json:"err_no,omitempty"`
|
||||||
|
ErrTips string `json:"err_tips,omitempty"`
|
||||||
|
Data ResAccessTokenData `json:"data,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResAccessTokenData struct {
|
||||||
|
AccessToken string `json:"access_token,omitempty"`
|
||||||
|
ExpiresIn int `json:"expires_in,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTokenFromServer 从抖音服务器获取token
|
||||||
|
func GetTokenFromServer(apiUrl string, appId, appSecret string) (resAccessToken ResAccessToken, err error) {
|
||||||
|
params := map[string]interface{}{
|
||||||
|
"appid": appId,
|
||||||
|
"secret": appSecret,
|
||||||
|
"grant_type": "client_credential",
|
||||||
|
}
|
||||||
|
body, err := util.PostJSON(apiUrl, params)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(body, &resAccessToken)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if resAccessToken.ErrNo != 0 {
|
||||||
|
err = fmt.Errorf("get access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrTips, resAccessToken.ErrNo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
220
douyin/douyin_openapi.go
Normal file
220
douyin/douyin_openapi.go
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
package douyin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
accessToken "gitea.youtukeji.com.cn/youtu/openapi-helper/douyin/access-token"
|
||||||
|
util2 "gitea.youtukeji.com.cn/youtu/openapi-helper/douyin/util"
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
code2Session = "https://minigame.zijieapi.com/mgplatform/api/apps/jscode2session" // 小程序登录地址
|
||||||
|
getEcpm = "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 获取ECPM
|
||||||
|
)
|
||||||
|
|
||||||
|
// DouYinOpenApiConfig 实例化配置
|
||||||
|
type DouYinOpenApiConfig struct {
|
||||||
|
AppId string
|
||||||
|
AppSecret string
|
||||||
|
AccessToken accessToken.AccessToken
|
||||||
|
Cache cache.Cache
|
||||||
|
IsSandbox bool
|
||||||
|
Token string
|
||||||
|
Salt string
|
||||||
|
}
|
||||||
|
|
||||||
|
// DouYinOpenApi 基类
|
||||||
|
type DouYinOpenApi struct {
|
||||||
|
Config DouYinOpenApiConfig
|
||||||
|
BaseApi string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDouYinOpenApi 实例化一个抖音openapi实例
|
||||||
|
func NewDouYinOpenApi(config DouYinOpenApiConfig) *DouYinOpenApi {
|
||||||
|
if config.Cache == nil {
|
||||||
|
config.Cache = cache.NewMemory()
|
||||||
|
}
|
||||||
|
if config.AccessToken == nil {
|
||||||
|
config.AccessToken = accessToken.NewDefaultAccessToken(config.AppId, config.AppSecret, config.Cache, config.IsSandbox)
|
||||||
|
}
|
||||||
|
return &DouYinOpenApi{
|
||||||
|
Config: config,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetApiUrl 获取api地址
|
||||||
|
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 := util2.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 := util2.PostJSON(api, params)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(body, &response)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code2SessionParams 小程序登录 所需参数
|
||||||
|
type Code2SessionParams struct {
|
||||||
|
Appid string `json:"appid,omitempty"`
|
||||||
|
Secret string `json:"secret,omitempty"`
|
||||||
|
AnonymousCode string `json:"anonymous_code,omitempty"`
|
||||||
|
Code string `json:"code,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Code2SessionResponse struct {
|
||||||
|
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 {
|
||||||
|
SessionKey string `json:"session_key,omitempty"`
|
||||||
|
Openid string `json:"openid,omitempty"`
|
||||||
|
AnonymousOpenid string `json:"anonymous_openid,omitempty"`
|
||||||
|
UnionId string `json:"unionid,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code2Session 小程序登录
|
||||||
|
func (d *DouYinOpenApi) Code2Session(code, anonymousCode string) (code2SessionResponse Code2SessionResponse, err error) {
|
||||||
|
params := Code2SessionParams{
|
||||||
|
Appid: d.Config.AppId,
|
||||||
|
Secret: d.Config.AppSecret,
|
||||||
|
AnonymousCode: anonymousCode,
|
||||||
|
Code: code,
|
||||||
|
}
|
||||||
|
b, err := d.Get(code2Session, params)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(b, &code2SessionResponse)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return code2SessionResponse, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Record struct {
|
||||||
|
Aid string `json:"aid"`
|
||||||
|
Cost int `json:"cost"`
|
||||||
|
Did string `json:"did"`
|
||||||
|
EventName string `json:"event_name"`
|
||||||
|
EventTime string `json:"event_time"`
|
||||||
|
OpenID string `json:"open_id"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpmParams 获取ECPM参数
|
||||||
|
type GetEcpmParams struct {
|
||||||
|
AppId string `json:"app_id" form:"app_id"`
|
||||||
|
OpenId string `json:"open_id" form:"open_id"`
|
||||||
|
AccessToken string `json:"access_token" form:"access_token"`
|
||||||
|
DateHour string `json:"date_hour" form:"date_hour"`
|
||||||
|
PageSize int `json:"page_size" form:"page_size"`
|
||||||
|
PageNo int `json:"page_no" form:"page_no"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpmResponseData 获取ECPM响应数据
|
||||||
|
type GetEcpmResponseData struct {
|
||||||
|
BaseResp struct {
|
||||||
|
StatusCode int `json:"StatusCode"`
|
||||||
|
StatusMessage string `json:"StatusMessage"`
|
||||||
|
} `json:"BaseResp"`
|
||||||
|
Data struct {
|
||||||
|
Records []Record `json:"records"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
} `json:"data"`
|
||||||
|
ErrMsg string `json:"err_msg"`
|
||||||
|
ErrNo int `json:"err_no"`
|
||||||
|
LogID string `json:"log_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEcpm 获取ECPM
|
||||||
|
// https://bytedance.larkoffice.com/docx/Vg4yd0RDSovZINxJDyIc6THhnod
|
||||||
|
// 根据分页大小循环获取,聚合总数返回
|
||||||
|
func (d *DouYinOpenApi) GetEcpm(params GetEcpmParams) (list []Record, err error) {
|
||||||
|
fullURL := fmt.Sprintf("%s?open_id=%s&mp_id=%s&access_token=%s&date_hour=%s&page_size=500&page_no=", getEcpm, params.OpenId, params.AppId, params.AccessToken, params.DateHour)
|
||||||
|
for {
|
||||||
|
fullURL += strconv.Itoa(params.PageNo)
|
||||||
|
var responseBody []byte
|
||||||
|
func() {
|
||||||
|
// 调用抖音 API
|
||||||
|
var resp *http.Response
|
||||||
|
resp, err = http.Get(fullURL)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.New("调用抖音 API 失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
// 读取抖音 API 响应数据
|
||||||
|
responseBody, err = io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.New("读取抖音 API 响应失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 解析抖音 API 响应数据
|
||||||
|
var apiResponse GetEcpmResponseData
|
||||||
|
err = json.Unmarshal(responseBody, &apiResponse)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.New("解析 API 响应数据失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查 API 是否返回错误
|
||||||
|
if apiResponse.ErrNo != 0 {
|
||||||
|
err = fmt.Errorf("抖音 API 返回错误: %s (错误码: %d)", apiResponse.ErrMsg, apiResponse.ErrNo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
list = append(list, apiResponse.Data.Records...)
|
||||||
|
|
||||||
|
// 当页数据小于总页数时,无更多数据,返回
|
||||||
|
if len(apiResponse.Data.Records) <= params.PageSize {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params.PageNo++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAccessToken 获取accessToken
|
||||||
|
func (d *DouYinOpenApi) GetAccessToken() (string, error) {
|
||||||
|
return d.Config.AccessToken.GetAccessToken()
|
||||||
|
}
|
90
douyin/douyin_openapi_test.go
Normal file
90
douyin/douyin_openapi_test.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package douyin
|
||||||
|
|
||||||
|
import (
|
||||||
|
accessToken "gitea.youtukeji.com.cn/youtu/openapi-helper/douyin/access-token"
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 声明测试常量
|
||||||
|
const (
|
||||||
|
AppId = "tt8b32fd8f14071db707"
|
||||||
|
AppSecret = "44018e80b1bde34395a52de67ce1e0c37c572d80"
|
||||||
|
Token = ""
|
||||||
|
Salt = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
// 声明一个缓存实例
|
||||||
|
var Cache cache.Cache
|
||||||
|
|
||||||
|
// 声明全局openApi实例
|
||||||
|
var OpenApi *DouYinOpenApi
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Cache = cache.NewMemory()
|
||||||
|
OpenApi = NewDouYinOpenApi(DouYinOpenApiConfig{
|
||||||
|
AppId: AppId,
|
||||||
|
AppSecret: AppSecret,
|
||||||
|
IsSandbox: false,
|
||||||
|
Token: Token,
|
||||||
|
Salt: Salt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试获取新的token
|
||||||
|
func TestDouyinOpenapi_NewDefaultAccessToken(t *testing.T) {
|
||||||
|
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
|
||||||
|
getAccessToken, err := token.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("got a error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Logf("got a value: %s", getAccessToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基准测试看获取token的次数?
|
||||||
|
func BenchmarkDouyinOpenapi_NewDefaultAccessToken(b *testing.B) {
|
||||||
|
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
getAccessToken, err := token.GetAccessToken()
|
||||||
|
b.Logf("get token: %s %+v", getAccessToken, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试小程序登录
|
||||||
|
func TestDouYinOpenApi_Code2Session(t *testing.T) {
|
||||||
|
session, err := OpenApi.Code2Session("1111", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("got a error %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Logf("got a value %+v", session)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDouYinOpenApi_GetEcpm(t *testing.T) {
|
||||||
|
|
||||||
|
// 获取动态的 access_token
|
||||||
|
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
|
||||||
|
|
||||||
|
at, err := token.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("got a error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
list, err := OpenApi.GetEcpm(GetEcpmParams{
|
||||||
|
AppId: AppId,
|
||||||
|
OpenId: "_000aG3MQTzVW-yCUH-ndwv90JIyvdTSD0gf",
|
||||||
|
AccessToken: at,
|
||||||
|
DateHour: time.Now().Format(time.DateOnly),
|
||||||
|
PageSize: 500,
|
||||||
|
PageNo: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("got a error %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Logf("got a value %+v", list)
|
||||||
|
}
|
54
douyin/util/conv.go
Normal file
54
douyin/util/conv.go
Normal 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
|
||||||
|
}
|
27
douyin/util/http.go
Normal file
27
douyin/util/http.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PostJSON post json 数据请求
|
||||||
|
func PostJSON(uri string, obj any) ([]byte, error) {
|
||||||
|
marshal, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
response, err := http.Post(uri, "application/json;charset=utf-8", bytes.NewBuffer(marshal))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
if response.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
|
||||||
|
}
|
||||||
|
return io.ReadAll(response.Body)
|
||||||
|
}
|
25
go.mod
Normal file
25
go.mod
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
module gitea.youtukeji.com.cn/youtu/openapi-helper
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
|
|
||||||
|
require github.com/silenceper/wechat/v2 v2.1.7
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/alicebob/miniredis/v2 v2.34.0 // indirect
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||||
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
github.com/onsi/gomega v1.27.10 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||||
|
github.com/spf13/cast v1.7.1 // indirect
|
||||||
|
github.com/stretchr/testify v1.10.0 // indirect
|
||||||
|
github.com/tidwall/gjson v1.18.0 // indirect
|
||||||
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
|
golang.org/x/crypto v0.32.0 // indirect
|
||||||
|
golang.org/x/net v0.33.0 // indirect
|
||||||
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
|
)
|
172
go.sum
Normal file
172
go.sum
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
|
||||||
|
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO85qeSydJtItA4T55Pw6BtAejd0APRJOCE=
|
||||||
|
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
|
||||||
|
github.com/alicebob/miniredis/v2 v2.30.0/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
|
||||||
|
github.com/alicebob/miniredis/v2 v2.34.0 h1:mBFWMaJSNL9RwdGRyEDoAAv8OQc5UlEhLDQggTglU/0=
|
||||||
|
github.com/alicebob/miniredis/v2 v2.34.0/go.mod h1:kWShP4b58T1CW0Y5dViCd5ztzrDqRWqM3nksiyXk5s8=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 h1:N7oVaKyGp8bttX0bfZGmcGkjz7DLQXhAn3DNd3T0ous=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874/go.mod h1:r5xuitiExdLAJ09PR7vBVENGvp4ZuTBeWTGtxuX3K+c=
|
||||||
|
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||||
|
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
|
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||||
|
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||||
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
|
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||||
|
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
|
||||||
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||||
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
|
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||||
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
|
||||||
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
|
||||||
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
|
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
|
||||||
|
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||||
|
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||||
|
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||||
|
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
|
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
||||||
|
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
|
||||||
|
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||||
|
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
|
||||||
|
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
|
||||||
|
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
||||||
|
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||||
|
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
|
||||||
|
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
|
||||||
|
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
|
||||||
|
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
|
github.com/silenceper/wechat/v2 v2.1.7 h1:v4AC4pa6NRm7Pa2FJnmWABOxZ9hx3IIo20xKT4t1msY=
|
||||||
|
github.com/silenceper/wechat/v2 v2.1.7/go.mod h1:7Iu3EhQYVtDUJAj+ZVRy8yom75ga7aDWv8RurLkVm0s=
|
||||||
|
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||||
|
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
||||||
|
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
|
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||||
|
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||||
|
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
|
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
||||||
|
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||||
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
||||||
|
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
|
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||||
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||||
|
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||||
|
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||||
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
|
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
|
||||||
|
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
58
wechat.go
Normal file
58
wechat.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package douyin_openapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/silenceper/wechat/v2"
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram"
|
||||||
|
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WechatApi struct {
|
||||||
|
m *sync.Map
|
||||||
|
wc *wechat.Wechat
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWechatOpenApiClient() *WechatApi {
|
||||||
|
return &WechatApi{
|
||||||
|
m: &sync.Map{},
|
||||||
|
wc: wechat.NewWechat(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetWechatOpenApi 获取微信client
|
||||||
|
// appId: 小程序id
|
||||||
|
func (d *WechatApi) GetWechatOpenApi(appId string) (api *miniprogram.MiniProgram, err error) {
|
||||||
|
if v, ok := d.m.Load(appId); !ok {
|
||||||
|
err = errors.New("not found wechat open api")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
api = v.(*miniprogram.MiniProgram)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWechatOpenApi 存储微信client
|
||||||
|
// appId: 小程序id
|
||||||
|
func (d *WechatApi) SetWechatOpenApi(appId string, api *miniprogram.MiniProgram) {
|
||||||
|
d.m.Store(appId, api)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAndStoreWechatOpenApi 创建微信client并存储
|
||||||
|
// appId: 小程序id
|
||||||
|
// appSecret: 小程序secret
|
||||||
|
// cache: 缓存
|
||||||
|
func (d *WechatApi) NewAndStoreWechatOpenApi(appId, appSecret string, cache cache.Cache) {
|
||||||
|
cfg := &miniConfig.Config{
|
||||||
|
AppID: appId,
|
||||||
|
AppSecret: appSecret,
|
||||||
|
//Token: "xxx",
|
||||||
|
// EncodingAESKey: "xxxx",
|
||||||
|
Cache: cache,
|
||||||
|
}
|
||||||
|
|
||||||
|
mini := d.wc.GetMiniProgram(cfg)
|
||||||
|
|
||||||
|
d.SetWechatOpenApi(appId, mini)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user