Skip to content

Commit 9ab9df6

Browse files
committed
InitHTTPClients 换到 MchMgr 中,这样在初始化可以报错
1 parent ac04e31 commit 9ab9df6

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

wechat.go

+7-24
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ package gowechat
33

44
import (
55
"fmt"
6-
"net/http"
76
"sync"
87

98
"github.com/astaxie/beego/cache"
10-
"github.com/yaotian/gowechat/util"
119
"github.com/yaotian/gowechat/wxcontext"
1210
)
1311

14-
//MemCache if wxcontext.Config no cache, this will give a default memory cache.
15-
var MemCache cache.Cache
12+
//memCache if wxcontext.Config no cache, this will give a default memory cache.
13+
var memCache cache.Cache
1614

1715
// Wechat struct
1816
type Wechat struct {
@@ -28,33 +26,16 @@ func NewWechat(cfg wxcontext.Config) *Wechat {
2826

2927
func initContext(cfg *wxcontext.Config, context *wxcontext.Context) {
3028
if cfg.Cache == nil {
31-
if MemCache == nil {
32-
MemCache, _ = cache.NewCache("memory", `{"interval":60}`)
29+
if memCache == nil {
30+
memCache, _ = cache.NewCache("memory", `{"interval":60}`)
3331
}
34-
cfg.Cache = MemCache
32+
cfg.Cache = memCache
3533
}
3634
context.Config = cfg
3735

3836
context.SetAccessTokenLock(new(sync.RWMutex))
3937
context.SetJsAPITicketLock(new(sync.RWMutex))
4038

41-
//create http client
42-
if cfg.SslCertFilePath != "" && cfg.SslKeyFilePath != "" {
43-
if client, err := util.NewTLSHttpClient(cfg.SslCertFilePath, cfg.SslKeyFilePath); err == nil {
44-
context.SHTTPClient = client
45-
} else {
46-
fmt.Print(err)
47-
}
48-
}
49-
50-
if cfg.SslCertContent != "" && cfg.SslKeyContent != "" {
51-
if client, err := util.NewTLSHttpClientFromContent(cfg.SslCertContent, cfg.SslKeyContent); err == nil {
52-
context.SHTTPClient = client
53-
} else {
54-
fmt.Print(err)
55-
}
56-
}
57-
context.HTTPClient = http.DefaultClient
5839
}
5940

6041
//MchMgr 商户平台
@@ -110,5 +91,7 @@ func (wc *Wechat) checkCfgMch() (err error) {
11091
if wc.Context.SslKeyFilePath == "" && wc.Context.SslKeyContent == "" {
11192
return fmt.Errorf("%s", "配置中没有SslKey")
11293
}
94+
//初始化 http client, 有错误会出错误
95+
err = wc.Context.InitHTTPClients()
11396
return
11497
}

wxcontext/context.go

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package wxcontext
33
import (
44
"net/http"
55
"sync"
6+
7+
"github.com/yaotian/gowechat/util"
68
)
79

810
// Context struct
@@ -46,3 +48,25 @@ func (ctx *Context) SetJsAPITicketLock(lock *sync.RWMutex) {
4648
func (ctx *Context) GetJsAPITicketLock() *sync.RWMutex {
4749
return ctx.jsAPITicketLock
4850
}
51+
52+
//InitHTTPClients Context中初始化 httpclient httpsclient
53+
func (ctx *Context) InitHTTPClients() (err error) {
54+
//create http client
55+
if ctx.SslCertFilePath != "" && ctx.SslKeyFilePath != "" {
56+
if client, err := util.NewTLSHttpClient(ctx.SslCertFilePath, ctx.SslKeyFilePath); err == nil {
57+
ctx.SHTTPClient = client
58+
} else {
59+
return err
60+
}
61+
}
62+
63+
if ctx.SslCertContent != "" && ctx.SslKeyContent != "" {
64+
if client, err := util.NewTLSHttpClientFromContent(ctx.SslCertContent, ctx.SslKeyContent); err == nil {
65+
ctx.SHTTPClient = client
66+
} else {
67+
return err
68+
}
69+
}
70+
ctx.HTTPClient = http.DefaultClient
71+
return
72+
}

0 commit comments

Comments
 (0)