49 lines
1018 B
Go
49 lines
1018 B
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/silenceper/wechat/v2/cache"
|
|
mconfig "github.com/silenceper/wechat/v2/miniprogram/config"
|
|
mctx "github.com/silenceper/wechat/v2/miniprogram/context"
|
|
|
|
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
|
)
|
|
|
|
type WechatApi struct {
|
|
auth *auth.Auth
|
|
}
|
|
|
|
func (w *WechatApi) GetAccessToken(_ context.Context) (string, error) {
|
|
return w.auth.GetAccessToken()
|
|
}
|
|
|
|
func (w *WechatApi) Code2Session(_ context.Context, code, _ string) (session *Code2SessionResponse, err error) {
|
|
var r auth.ResCode2Session
|
|
r, err = w.auth.Code2Session(code)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if r.ErrCode != 0 {
|
|
err = errors.New(r.ErrMsg)
|
|
return
|
|
}
|
|
session = &Code2SessionResponse{
|
|
OpenID: r.OpenID,
|
|
UnionID: r.UnionID,
|
|
}
|
|
return
|
|
}
|
|
|
|
func NewWechatApi(appId, appSecret string, cache cache.Cache) *WechatApi {
|
|
return &WechatApi{
|
|
auth: auth.NewAuth(&mctx.Context{
|
|
Config: &mconfig.Config{
|
|
AppID: appId,
|
|
AppSecret: appSecret,
|
|
Cache: cache,
|
|
},
|
|
}),
|
|
}
|
|
}
|