Skip to content
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

微信v2Client新增SetProxyUrl设置代理地址;v3新增SetProxyUrl修改全局变量proxyUrl,设置代理地址 #447

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions wechat/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func (w *Client) SetCountry(country Country) (client *Client) {
return w
}

// SetProxyUrl 设置代理 Url
// 使用场景:
// 1. 部署环境无法访问互联网,可以通过代理服务器访问
func (w *Client) SetProxyUrl(proxyUrl string) (client *Client) {
w.mu.Lock()
w.BaseURL = proxyUrl
w.mu.Unlock()
return w
}

// 添加微信pem证书文件路径
// certFilePath:apiclient_cert.pem 文件路径
// keyFilePath:apiclient_key.pem 文件路径
Expand Down
3 changes: 3 additions & 0 deletions wechat/v3/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func GetPlatformCerts(ctx context.Context, mchid, apiV3Key, serialNo, privateKey
authorization := Authorization + ` mchid="` + mchid + `",nonce_str="` + nonceStr + `",timestamp="` + ts + `",serial_no="` + serialNo + `",signature="` + sign + `"`
// Request
var url = v3BaseUrlCh + uri
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + uri
}
hc := xhttp.NewClient().Req()
hc.Header.Add(HeaderAuthorization, authorization)
hc.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix()))
Expand Down
22 changes: 22 additions & 0 deletions wechat/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,25 @@ func (c *ClientV3) SetLogger(logger xlog.XLogger) {
c.logger = logger
}
}

// SetProxyUrl 设置代理URL
// 使用场景:
// 1. 部署环境无法访问互联网,可以通过代理服务器访问
var (
proxyUrl string
mu sync.Mutex
)

// GetProxyUrl 返回当前的 ProxyUrl
func GetProxyUrl() string {
mu.Lock()
defer mu.Unlock()
return proxyUrl
}

// SetProxyUrl 设置新的 ProxyUrl
func SetProxyUrl(newProxyUrl string) {
mu.Lock()
defer mu.Unlock()
proxyUrl = newProxyUrl
}
21 changes: 21 additions & 0 deletions wechat/v3/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (d *requestIdFunc) RequestId() string {

func (c *ClientV3) doProdPostWithHeader(ctx context.Context, headerMap map[string]string, bm gopay.BodyMap, path, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + path
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down Expand Up @@ -86,6 +89,9 @@ func (c *ClientV3) doProdPostWithHost(ctx context.Context, bm gopay.BodyMap, hos

func (c *ClientV3) doProdPost(ctx context.Context, bm gopay.BodyMap, path, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + path
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down Expand Up @@ -117,6 +123,9 @@ func (c *ClientV3) doProdPost(ctx context.Context, bm gopay.BodyMap, path, autho

func (c *ClientV3) doProdGet(ctx context.Context, uri, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + uri
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + uri
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down Expand Up @@ -147,6 +156,9 @@ func (c *ClientV3) doProdGet(ctx context.Context, uri, authorization string) (re

func (c *ClientV3) doProdPut(ctx context.Context, bm gopay.BodyMap, path, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + path
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down Expand Up @@ -178,6 +190,9 @@ func (c *ClientV3) doProdPut(ctx context.Context, bm gopay.BodyMap, path, author

func (c *ClientV3) doProdDelete(ctx context.Context, bm gopay.BodyMap, path, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + path
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down Expand Up @@ -209,6 +224,9 @@ func (c *ClientV3) doProdDelete(ctx context.Context, bm gopay.BodyMap, path, aut

func (c *ClientV3) doProdPostFile(ctx context.Context, bm gopay.BodyMap, path, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + path
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + path
}
req := c.hc.Req(xhttp.TypeMultipartFormData)
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down Expand Up @@ -240,6 +258,9 @@ func (c *ClientV3) doProdPostFile(ctx context.Context, bm gopay.BodyMap, path, a

func (c *ClientV3) doProdPatch(ctx context.Context, bm gopay.BodyMap, path, authorization string) (res *http.Response, si *SignInfo, bs []byte, err error) {
var url = v3BaseUrlCh + path
if v3ProxyUrl := GetProxyUrl(); v3ProxyUrl != "" {
url = v3ProxyUrl + path
}
req := c.hc.Req() // default json
req.Header.Add(HeaderAuthorization, authorization)
req.Header.Add(HeaderRequestID, c.requestIdFunc.RequestId())
Expand Down
Loading