mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-12 16:52:28 +08:00
将小程序获取ak的方式也抽象出来
This commit is contained in:
@@ -15,6 +15,8 @@ const (
|
|||||||
accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
||||||
//CacheKeyOfficialAccountPrefix 微信公众号cache key前缀
|
//CacheKeyOfficialAccountPrefix 微信公众号cache key前缀
|
||||||
CacheKeyOfficialAccountPrefix = "gowechat_officialaccount_"
|
CacheKeyOfficialAccountPrefix = "gowechat_officialaccount_"
|
||||||
|
//CacheKeyMiniProgramPrefix 小程序cache key前缀
|
||||||
|
CacheKeyMiniProgramPrefix = "gowechat_miniprogram_"
|
||||||
)
|
)
|
||||||
|
|
||||||
//DefaultAccessToken 默认AccessToken 获取
|
//DefaultAccessToken 默认AccessToken 获取
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
//AccessTokenURL 获取access_token的接口
|
|
||||||
AccessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
|
||||||
//CacheKeyPrefix cache前缀
|
|
||||||
CacheKeyPrefix = "gowechat_miniprogram_"
|
|
||||||
)
|
|
||||||
|
|
||||||
//ResAccessToken struct
|
|
||||||
type ResAccessToken struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
ExpiresIn int64 `json:"expires_in"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAccessTokenFunc 获取 access token 的函数签名
|
|
||||||
type GetAccessTokenFunc func(ctx *Context) (accessToken string, err error)
|
|
||||||
|
|
||||||
//SetAccessTokenLock 设置读写锁(一个appID一个读写锁)
|
|
||||||
func (ctx *Context) SetAccessTokenLock(l *sync.RWMutex) {
|
|
||||||
ctx.accessTokenLock = l
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetGetAccessTokenFunc 设置自定义获取accessToken的方式, 需要自己实现缓存
|
|
||||||
func (ctx *Context) SetGetAccessTokenFunc(f GetAccessTokenFunc) {
|
|
||||||
ctx.accessTokenFunc = f
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAccessToken 获取access_token
|
|
||||||
func (ctx *Context) GetAccessToken() (accessToken string, err error) {
|
|
||||||
ctx.accessTokenLock.Lock()
|
|
||||||
defer ctx.accessTokenLock.Unlock()
|
|
||||||
|
|
||||||
if ctx.accessTokenFunc != nil {
|
|
||||||
return ctx.accessTokenFunc(ctx)
|
|
||||||
}
|
|
||||||
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", CacheKeyPrefix, ctx.AppID)
|
|
||||||
val := ctx.Cache.Get(accessTokenCacheKey)
|
|
||||||
if val != nil {
|
|
||||||
accessToken = val.(string)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//从微信服务器获取
|
|
||||||
var resAccessToken ResAccessToken
|
|
||||||
resAccessToken, err = ctx.GetAccessTokenFromServer()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
accessToken = resAccessToken.AccessToken
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAccessTokenFromServer 强制从微信服务器获取token
|
|
||||||
func (ctx *Context) GetAccessTokenFromServer() (resAccessToken ResAccessToken, err error) {
|
|
||||||
url := fmt.Sprintf("%s?grant_type=client_credential&appid=%s&secret=%s", AccessTokenURL, ctx.AppID, ctx.AppSecret)
|
|
||||||
var body []byte
|
|
||||||
body, err = util.HTTPGet(url)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(body, &resAccessToken)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if resAccessToken.ErrMsg != "" {
|
|
||||||
err = fmt.Errorf("get access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrCode, resAccessToken.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", CacheKeyPrefix, ctx.AppID)
|
|
||||||
expires := resAccessToken.ExpiresIn - 1500
|
|
||||||
err = ctx.Cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,12 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context struct
|
// Context struct
|
||||||
type Context struct {
|
type Context struct {
|
||||||
*config.Config
|
*config.Config
|
||||||
|
credential.AccessTokenHandle
|
||||||
//accessTokenLock 读写锁 同一个AppID一个
|
|
||||||
accessTokenLock *sync.RWMutex
|
|
||||||
|
|
||||||
//accessTokenFunc 自定义获取 access token 的方法
|
|
||||||
accessTokenFunc GetAccessTokenFunc
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package miniprogram
|
package miniprogram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/analysis"
|
"github.com/silenceper/wechat/v2/miniprogram/analysis"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/basic"
|
"github.com/silenceper/wechat/v2/miniprogram/basic"
|
||||||
@@ -19,16 +18,19 @@ type MiniProgram struct {
|
|||||||
|
|
||||||
//NewMiniProgram 实例化小程序API
|
//NewMiniProgram 实例化小程序API
|
||||||
func NewMiniProgram(cfg *config.Config) *MiniProgram {
|
func NewMiniProgram(cfg *config.Config) *MiniProgram {
|
||||||
if cfg.Cache == nil {
|
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
|
||||||
panic("cache未设置")
|
|
||||||
}
|
|
||||||
ctx := &context.Context{
|
ctx := &context.Context{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
|
AccessTokenHandle: defaultAkHandle,
|
||||||
}
|
}
|
||||||
ctx.SetAccessTokenLock(new(sync.RWMutex))
|
|
||||||
return &MiniProgram{ctx}
|
return &MiniProgram{ctx}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//SetAccessTokenHandle 自定义access_token获取方式
|
||||||
|
func (miniProgram *MiniProgram) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
|
||||||
|
miniProgram.ctx.AccessTokenHandle = accessTokenHandle
|
||||||
|
}
|
||||||
|
|
||||||
// GetContext get Context
|
// GetContext get Context
|
||||||
func (miniProgram *MiniProgram) GetContext() *context.Context {
|
func (miniProgram *MiniProgram) GetContext() *context.Context {
|
||||||
return miniProgram.ctx
|
return miniProgram.ctx
|
||||||
|
|||||||
@@ -24,10 +24,10 @@ type OfficialAccount struct {
|
|||||||
|
|
||||||
//NewOfficialAccount 实例化公众号API
|
//NewOfficialAccount 实例化公众号API
|
||||||
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
|
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
|
||||||
defaultAK := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
|
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
|
||||||
ctx := &context.Context{
|
ctx := &context.Context{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
AccessTokenHandle: defaultAK,
|
AccessTokenHandle: defaultAkHandle,
|
||||||
}
|
}
|
||||||
return &OfficialAccount{ctx: ctx}
|
return &OfficialAccount{ctx: ctx}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user