mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-10 07:42:26 +08:00
[feature] Format the code and improve Mini Program authorization to o… (#473)
* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session) * [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html) * upgrade json error * upgrade json error Co-authored-by: houseme <houseme@outlook.com>
This commit is contained in:
@@ -11,19 +11,19 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
//AccessTokenURL 获取access_token的接口
|
||||
// AccessTokenURL 获取access_token的接口
|
||||
accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
|
||||
//AccessTokenURL 企业微信获取access_token的接口
|
||||
// AccessTokenURL 企业微信获取access_token的接口
|
||||
workAccessTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"
|
||||
//CacheKeyOfficialAccountPrefix 微信公众号cache key前缀
|
||||
// CacheKeyOfficialAccountPrefix 微信公众号cache key前缀
|
||||
CacheKeyOfficialAccountPrefix = "gowechat_officialaccount_"
|
||||
//CacheKeyMiniProgramPrefix 小程序cache key前缀
|
||||
// CacheKeyMiniProgramPrefix 小程序cache key前缀
|
||||
CacheKeyMiniProgramPrefix = "gowechat_miniprogram_"
|
||||
//CacheKeyWorkPrefix 企业微信cache key前缀
|
||||
// CacheKeyWorkPrefix 企业微信cache key前缀
|
||||
CacheKeyWorkPrefix = "gowechat_work_"
|
||||
)
|
||||
|
||||
//DefaultAccessToken 默认AccessToken 获取
|
||||
// DefaultAccessToken 默认AccessToken 获取
|
||||
type DefaultAccessToken struct {
|
||||
appID string
|
||||
appSecret string
|
||||
@@ -32,7 +32,7 @@ type DefaultAccessToken struct {
|
||||
accessTokenLock *sync.Mutex
|
||||
}
|
||||
|
||||
//NewDefaultAccessToken new DefaultAccessToken
|
||||
// NewDefaultAccessToken new DefaultAccessToken
|
||||
func NewDefaultAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.Cache) AccessTokenHandle {
|
||||
if cache == nil {
|
||||
panic("cache is ineed")
|
||||
@@ -46,7 +46,7 @@ func NewDefaultAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.
|
||||
}
|
||||
}
|
||||
|
||||
//ResAccessToken struct
|
||||
// ResAccessToken struct
|
||||
type ResAccessToken struct {
|
||||
util.CommonError
|
||||
|
||||
@@ -54,7 +54,7 @@ type ResAccessToken struct {
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
}
|
||||
|
||||
//GetAccessToken 获取access_token,先从cache中获取,没有则从服务端获取
|
||||
// GetAccessToken 获取access_token,先从cache中获取,没有则从服务端获取
|
||||
func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
// 先从cache中取
|
||||
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
||||
@@ -62,7 +62,7 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
return val.(string), nil
|
||||
}
|
||||
|
||||
//加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||
// 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||
ak.accessTokenLock.Lock()
|
||||
defer ak.accessTokenLock.Unlock()
|
||||
|
||||
@@ -71,7 +71,7 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
return val.(string), nil
|
||||
}
|
||||
|
||||
//cache失效,从微信服务器获取
|
||||
// cache失效,从微信服务器获取
|
||||
var resAccessToken ResAccessToken
|
||||
resAccessToken, err = GetTokenFromServer(fmt.Sprintf(accessTokenURL, ak.appID, ak.appSecret))
|
||||
if err != nil {
|
||||
@@ -87,7 +87,7 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//WorkAccessToken 企业微信AccessToken 获取
|
||||
// WorkAccessToken 企业微信AccessToken 获取
|
||||
type WorkAccessToken struct {
|
||||
CorpID string
|
||||
CorpSecret string
|
||||
@@ -96,7 +96,7 @@ type WorkAccessToken struct {
|
||||
accessTokenLock *sync.Mutex
|
||||
}
|
||||
|
||||
//NewWorkAccessToken new WorkAccessToken
|
||||
// NewWorkAccessToken new WorkAccessToken
|
||||
func NewWorkAccessToken(corpID, corpSecret, cacheKeyPrefix string, cache cache.Cache) AccessTokenHandle {
|
||||
if cache == nil {
|
||||
panic("cache the not exist")
|
||||
@@ -110,9 +110,9 @@ func NewWorkAccessToken(corpID, corpSecret, cacheKeyPrefix string, cache cache.C
|
||||
}
|
||||
}
|
||||
|
||||
//GetAccessToken 企业微信获取access_token,先从cache中获取,没有则从服务端获取
|
||||
// GetAccessToken 企业微信获取access_token,先从cache中获取,没有则从服务端获取
|
||||
func (ak *WorkAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
//加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||
// 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||
ak.accessTokenLock.Lock()
|
||||
defer ak.accessTokenLock.Unlock()
|
||||
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.CorpID)
|
||||
@@ -122,7 +122,7 @@ func (ak *WorkAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//cache失效,从微信服务器获取
|
||||
// cache失效,从微信服务器获取
|
||||
var resAccessToken ResAccessToken
|
||||
resAccessToken, err = GetTokenFromServer(fmt.Sprintf(workAccessTokenURL, ak.CorpID, ak.CorpSecret))
|
||||
if err != nil {
|
||||
@@ -138,7 +138,7 @@ func (ak *WorkAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//GetTokenFromServer 强制从微信服务器获取token
|
||||
// GetTokenFromServer 强制从微信服务器获取token
|
||||
func GetTokenFromServer(url string) (resAccessToken ResAccessToken, err error) {
|
||||
var body []byte
|
||||
body, err = util.HTTPGet(url)
|
||||
|
||||
Reference in New Issue
Block a user