diff --git a/wechat/param.go b/wechat/param.go index 5ab473e8..e000da55 100644 --- a/wechat/param.go +++ b/wechat/param.go @@ -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 文件路径 diff --git a/wechat/v3/cert.go b/wechat/v3/cert.go index 9b8f7560..f62852b4 100644 --- a/wechat/v3/cert.go +++ b/wechat/v3/cert.go @@ -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())) diff --git a/wechat/v3/client.go b/wechat/v3/client.go index 321cb425..84bf2f56 100644 --- a/wechat/v3/client.go +++ b/wechat/v3/client.go @@ -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 +} diff --git a/wechat/v3/request.go b/wechat/v3/request.go index 0f0bd411..c7951c2f 100644 --- a/wechat/v3/request.go +++ b/wechat/v3/request.go @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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())