59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
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)
|
|
}
|