-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
59 lines (49 loc) · 1.8 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package wechatoffice
import (
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/golog"
"github.com/dtapps/go-library/utils/gorequest"
)
// 缓存前缀
// wechat_office:wechat_access_token:
// wechat_office:wechat_jsapi_ticket:
type redisCachePrefixFun func() (wechatAccessToken, wechatJsapiTicket string)
// ClientConfig 实例配置
type ClientConfig struct {
AppId string `json:"app_id"` // 小程序唯一凭证,即 appId
AppSecret string // 小程序唯一凭证密钥,即 appSecret
RedisClient *dorm.RedisClient // 缓存数据库
RedisCachePrefixFun redisCachePrefixFun // 缓存前缀
}
// Client 实例
type Client struct {
requestClient *gorequest.App // 请求服务
requestClientStatus bool // 请求服务状态
config struct {
appId string // 小程序唯一凭证,即 appId
appSecret string // 小程序唯一凭证密钥,即 appSecret
accessToken string // 接口调用凭证
jsapiTicket string // 签名凭证
}
cache struct {
redisClient *dorm.RedisClient // 缓存数据库
wechatAccessTokenPrefix string // AccessToken
wechatJsapiTicketPrefix string // JsapiTicket
}
slog struct {
status bool // 状态
client *golog.ApiSLog // 日志服务
}
}
// NewClient 创建实例化
func NewClient(config *ClientConfig) (*Client, error) {
c := &Client{}
c.config.appId = config.AppId
c.config.appSecret = config.AppSecret
c.cache.redisClient = config.RedisClient
c.cache.wechatAccessTokenPrefix, c.cache.wechatJsapiTicketPrefix = config.RedisCachePrefixFun()
if c.cache.wechatAccessTokenPrefix == "" || c.cache.wechatJsapiTicketPrefix == "" {
return nil, redisCachePrefixNoConfig
}
return c, nil
}