-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
支持使用代理访问微信接口 #556
Open
Caisin
wants to merge
3
commits into
silenceper:v2
Choose a base branch
from
Caisin:v2
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
支持使用代理访问微信接口 #556
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/imroc/req/v3" | ||
"github.com/silenceper/wechat/v2/cache" | ||
) | ||
|
||
// Config .config for 微信公众号 | ||
type Config struct { | ||
AppID string `json:"app_id"` // appid | ||
AppSecret string `json:"app_secret"` // appsecret | ||
ProxyUrl string `json:"proxy_url"` // 代理url | ||
Cache cache.Cache | ||
} | ||
|
||
func (c *Config) Req() *req.Request { | ||
client := req.C() | ||
if c.ProxyUrl != "" { | ||
client.SetProxyURL(c.ProxyUrl) | ||
} | ||
return client.R() | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package credential | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/imroc/req/v3" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -27,19 +27,21 @@ const ( | |
type DefaultAccessToken struct { | ||
appID string | ||
appSecret string | ||
proxyUrl string | ||
cacheKeyPrefix string | ||
cache cache.Cache | ||
accessTokenLock *sync.Mutex | ||
} | ||
|
||
// NewDefaultAccessToken new DefaultAccessToken | ||
func NewDefaultAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.Cache) AccessTokenHandle { | ||
func NewDefaultAccessToken(appID, appSecret, proxyUrl, cacheKeyPrefix string, cache cache.Cache) AccessTokenHandle { | ||
if cache == nil { | ||
panic("cache is ineed") | ||
} | ||
return &DefaultAccessToken{ | ||
appID: appID, | ||
appSecret: appSecret, | ||
proxyUrl: proxyUrl, | ||
cache: cache, | ||
cacheKeyPrefix: cacheKeyPrefix, | ||
accessTokenLock: new(sync.Mutex), | ||
|
@@ -73,7 +75,7 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) { | |
|
||
// cache失效,从微信服务器获取 | ||
var resAccessToken ResAccessToken | ||
resAccessToken, err = GetTokenFromServer(fmt.Sprintf(accessTokenURL, ak.appID, ak.appSecret)) | ||
resAccessToken, err = GetTokenFromServer(fmt.Sprintf(accessTokenURL, ak.appID, ak.appSecret), ak.proxyUrl) | ||
if err != nil { | ||
return | ||
} | ||
|
@@ -91,6 +93,7 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) { | |
type WorkAccessToken struct { | ||
CorpID string | ||
CorpSecret string | ||
ProxyUrl string `json:"proxyUrl"` // 代理url | ||
cacheKeyPrefix string | ||
cache cache.Cache | ||
accessTokenLock *sync.Mutex | ||
|
@@ -124,7 +127,7 @@ func (ak *WorkAccessToken) GetAccessToken() (accessToken string, err error) { | |
|
||
// cache失效,从微信服务器获取 | ||
var resAccessToken ResAccessToken | ||
resAccessToken, err = GetTokenFromServer(fmt.Sprintf(workAccessTokenURL, ak.CorpID, ak.CorpSecret)) | ||
resAccessToken, err = GetTokenFromServer(fmt.Sprintf(workAccessTokenURL, ak.CorpID, ak.CorpSecret), ak.cacheKeyPrefix) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个不是传入 |
||
if err != nil { | ||
return | ||
} | ||
|
@@ -139,13 +142,16 @@ func (ak *WorkAccessToken) GetAccessToken() (accessToken string, err error) { | |
} | ||
|
||
// GetTokenFromServer 强制从微信服务器获取token | ||
func GetTokenFromServer(url string) (resAccessToken ResAccessToken, err error) { | ||
var body []byte | ||
body, err = util.HTTPGet(url) | ||
func GetTokenFromServer(url, proxyUrl string) (resAccessToken ResAccessToken, err error) { | ||
client := req.C() | ||
if proxyUrl != "" { | ||
client.SetProxyURL(proxyUrl) | ||
} | ||
resp, err := client.R().Get(url) | ||
Comment on lines
+146
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这部分可以直接封装在 |
||
if err != nil { | ||
return | ||
} | ||
err = json.Unmarshal(body, &resAccessToken) | ||
err = resp.Unmarshal(&resAccessToken) | ||
if err != nil { | ||
return | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/silenceper/wechat/v2/cache" | ||
"github.com/silenceper/wechat/v2/core" | ||
) | ||
|
||
// Config .config for 微信公众号 | ||
type Config struct { | ||
AppID string `json:"app_id"` // appid | ||
AppSecret string `json:"app_secret"` // appsecret | ||
*core.Config | ||
Token string `json:"token"` // token | ||
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey | ||
Cache cache.Cache | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/silenceper/wechat/v2/cache" | ||
"github.com/silenceper/wechat/v2/core" | ||
) | ||
|
||
// Config .config for 微信开放平台 | ||
type Config struct { | ||
AppID string `json:"app_id"` // appid | ||
AppSecret string `json:"app_secret"` // appsecret | ||
*core.Config | ||
Token string `json:"token"` // token | ||
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey | ||
Cache cache.Cache | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个core我理解是抽象公共的配置字段吧,是ok的
目前只有
ProxyUrl
,Cache
两个字段是公共的